ToolJet/frontend/src/_ui/Select/SelectComponent.jsx
Arpit e792e074f1
[Improvement] migrates to react-select: selection dropdown component (#2322)
* migrates to react-select: selection dropdown component

* styles fixes

* decomposing @ui/Select component into sub-components

* migrates Editor-Select component

* adds width to oauth selection dropdown

* updates datasource: oauth and authentication dropdowns (source component)

* Update frontend/src/_ui/Select/SelectComponent.jsx

Co-authored-by: Gandharv <gandharvkumargarg@gmail.com>

* resolves currentValue

* fixes scroll issue for dropdown options component

* updates queryManager react-select component

* updates rest api query to react-select

* adds options to render select menuwith portal

* adds: select menu render option for dynamic dropdowns and drodown-component-flip

* fixes: dropdown selection menu for restapi-oauth

* removes unused imports

Co-authored-by: Gandharv <gandharvkumargarg@gmail.com>
2022-04-11 17:27:07 +05:30

60 lines
1.6 KiB
JavaScript

import React from 'react';
import _ from 'lodash';
import Select from 'react-select';
import defaultStyles from './styles';
export const SelectComponent = ({ options = [], value, onChange, ...restProps }) => {
const darkMode = localStorage.getItem('darkMode') === 'true';
const {
styles,
hasSearch = true,
height,
width,
placeholder = 'Select..',
customOption = undefined,
defaultValue = null,
useMenuPortal = true,
} = restProps;
const useStyles = !_.isEmpty(styles) ? styles : defaultStyles(darkMode, width, height);
const selectOptions =
Array.isArray(options) && options.length === 0
? options
: options.map((option) => {
if (!option.hasOwnProperty('label')) {
return _.mapKeys(option, (value, key) => (key === 'value' ? key : 'label'));
}
return option;
});
const currentValue = selectOptions.find((option) => option.value === value) || value;
const handleOnChange = (newValue) => {
onChange(newValue.value);
};
const renderCustomOption = (option) => {
if (customOption) {
return customOption(option);
}
return option.label;
};
return (
<React.Fragment>
<Select
defaultValue={defaultValue}
options={selectOptions}
value={currentValue}
search={hasSearch}
onChange={handleOnChange}
placeholder={placeholder}
styles={useStyles}
formatOptionLabel={(option) => renderCustomOption(option)}
menuPortalTarget={useMenuPortal ? document.body : null}
menuPlacement="auto"
/>
</React.Fragment>
);
};