From eb5a432e0f5a68247f41b2723e9fe5564614c3cf Mon Sep 17 00:00:00 2001 From: Ruben Thoms Date: Fri, 10 Sep 2021 15:26:04 +0200 Subject: [PATCH 1/2] Bug fix: `Select` prop `value` does not return correct type --- react/src/lib/components/Select/Select.tsx | 71 ++++++++++++++++------ 1 file changed, 54 insertions(+), 17 deletions(-) diff --git a/react/src/lib/components/Select/Select.tsx b/react/src/lib/components/Select/Select.tsx index d43d9d3a..70b06d15 100644 --- a/react/src/lib/components/Select/Select.tsx +++ b/react/src/lib/components/Select/Select.tsx @@ -8,7 +8,10 @@ import React from "react"; import PropTypes, { InferProps } from "prop-types"; -import { getPropsWithMissingValuesSetToDefault, Optionals } from "../../utils/DefaultPropsHelpers"; +import { + getPropsWithMissingValuesSetToDefault, + Optionals, +} from "../../utils/DefaultPropsHelpers"; import "./Select.css"; const propTypes = { @@ -29,14 +32,20 @@ const propTypes = { /** * The dropdown's label */ - label: PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.number.isRequired]).isRequired, + label: PropTypes.oneOfType([ + PropTypes.string.isRequired, + PropTypes.number.isRequired, + ]).isRequired, /** * The value of the dropdown. This value * corresponds to the items specified in the * `value` property. */ - value: PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.number.isRequired]).isRequired, + value: PropTypes.oneOfType([ + PropTypes.string.isRequired, + PropTypes.number.isRequired, + ]).isRequired, }).isRequired ), /** @@ -51,8 +60,11 @@ const propTypes = { PropTypes.string.isRequired, PropTypes.number.isRequired, PropTypes.arrayOf( - PropTypes.string.isRequired - ), + PropTypes.oneOfType([ + PropTypes.string.isRequired, + PropTypes.number.isRequired, + ]).isRequired + ).isRequired, ]), /** * If true, the user can select multiple values @@ -120,13 +132,17 @@ const defaultProps: Optionals> = { persistence: false, persisted_props: ["value"], persistence_type: "local", - setProps: (): void => { return; } + setProps: (): void => { + return; + }, }; /** -* Select is a dash wrapper for the html select tag. -*/ -const Select: React.FC> = (props: InferProps): JSX.Element => { + * Select is a dash wrapper for the html select tag. + */ +const Select: React.FC> = ( + props: InferProps +): JSX.Element => { const { id, parent_className, @@ -137,17 +153,27 @@ const Select: React.FC> = (props: InferProps { - const options = (e.target as HTMLSelectElement).selectedOptions; - const values: string[] = []; + const selectedOptions = [].slice.call( + (e.target as HTMLSelectElement).selectedOptions + ); + const values: (string | number)[] = []; + for (let i = 0; i < options.length; i++) { - values.push(options[i].value); + if ( + selectedOptions.some( + (el: HTMLOptionElement) => + el.value === options[i].value.toString() + ) + ) { + values.push(options[i].value); + } } setProps({ value: values }); - } + }; return (
> = (props: InferProps