From 14a052843b0393bd9482ac108401ad6c4f34f076 Mon Sep 17 00:00:00 2001 From: John Panos Date: Thu, 1 Aug 2024 14:32:40 -0700 Subject: [PATCH 1/3] wip --- package.json | 1 + src/components/Select/Select.js | 73 -------- src/components/Select/Select.tsx | 117 +++++++++++++ yarn.lock | 281 +++++++++++++++++++++++++++++-- 4 files changed, 383 insertions(+), 89 deletions(-) delete mode 100644 src/components/Select/Select.js create mode 100644 src/components/Select/Select.tsx diff --git a/package.json b/package.json index 9a1abe05a..7717e14f0 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,7 @@ "prop-types": "^15.7.2", "react-imask": "^6.2.2", "react-resize-detector": "^4.2.3", + "react-select": "^5.8.0", "react-select-plus": "1.2.0", "react-sortable-hoc": "^1.11.0", "react-text-mask": "~5.0.2", diff --git a/src/components/Select/Select.js b/src/components/Select/Select.js deleted file mode 100644 index a5c565596..000000000 --- a/src/components/Select/Select.js +++ /dev/null @@ -1,73 +0,0 @@ -import classnames from 'classnames'; -import PropTypes from 'prop-types'; -import React, { useState, useRef } from 'react'; -import ReactSelect from 'react-select-plus'; -import Close from '../Button/Close'; -import SelectArrow from './SelectArrow'; -import SelectMultiValue from './SelectMultiValue'; -import Option from './SelectOption.js'; - -const getSelectArrow = (isOpen, arrowRenderer) => ( - -); - -const getCloseButton = () => ( - -); - -const Select = ({ - arrowRenderer, - className, - defaultValue, - inputProps, - valueComponent, - ...props -}) => { - const [value, setValue] = useState(props.value || defaultValue); - - // Store last props.value and call setValue when it changes. - // This is not correct behavior for controlled components but consumers rely on it. - const lastPropsValue = useRef(props.value); - if (!Object.is(props.value, lastPropsValue.current)) { - lastPropsValue.current = props.value; - setValue(props.value); - } - - const onChange = (newValue) => { - setValue(newValue); - props.onChange?.(newValue); - }; - - let SelectElement = ReactSelect; - if (props.loadOptions && props.creatable) { - SelectElement = ReactSelect.AsyncCreatable; - } else if (props.loadOptions) { - SelectElement = ReactSelect.Async; - } else if (props.creatable) { - SelectElement = ReactSelect.Creatable; - } - const classNames = classnames(className, { 'select-async': props.loadOptions }); - const valueComponentRenderer = valueComponent || (props.multi ? SelectMultiValue : undefined); - - return ( - getSelectArrow(isOpen, arrowRenderer)} - className={classNames} - clearRenderer={getCloseButton} - inputProps={{ name: props.name, ...inputProps }} - optionComponent={Option} - valueComponent={valueComponentRenderer} - {...props} - onChange={onChange} - value={props.value || value} - /> - ); -}; - -Select.propTypes = { - defaultValue: PropTypes.any, - ...ReactSelect.propTypes, -}; -Select.displayName = 'Select'; - -export default Select; diff --git a/src/components/Select/Select.tsx b/src/components/Select/Select.tsx new file mode 100644 index 000000000..c23b36c1c --- /dev/null +++ b/src/components/Select/Select.tsx @@ -0,0 +1,117 @@ +import React, { useState, useRef, useEffect } from 'react'; +import ReactSelect, { + components as ReactSelectComponents, + Props as ReactSelectProps, + ActionMeta, + SingleValue, + MultiValue, + ValueContainerProps, +} from 'react-select'; +import AsyncSelect from 'react-select/async'; +import AsyncCreatableSelect from 'react-select/async-creatable'; +import CreatableSelect from 'react-select/creatable'; + +export interface Option { + label: string; + value: any; +} + +interface SelectProps extends Omit, 'onChange'> { + options?: Option[]; + defaultValue?: Option | Option[] | null; + value?: Option | Option[] | null; + onChange?: (value: Option | Option[] | null, action: ActionMeta