index.js
88 lines
| 1 | /** |
| 2 | * Vendor dependencies |
| 3 | */ |
| 4 | import PropTypes from 'prop-types'; |
| 5 | import Select from 'react-select'; |
| 6 | |
| 7 | /** |
| 8 | * WordPress dependencies |
| 9 | */ |
| 10 | const {useInstanceId} = wp.compose; |
| 11 | const {BaseControl} = wp.components; |
| 12 | import { __ } from '@wordpress/i18n' |
| 13 | |
| 14 | /** |
| 15 | * Styles |
| 16 | */ |
| 17 | import './style.scss'; |
| 18 | |
| 19 | const MultiSelectControl = ({ |
| 20 | name, |
| 21 | label, |
| 22 | help, |
| 23 | className, |
| 24 | value, |
| 25 | placeholder, |
| 26 | hideLabelFromVision, |
| 27 | isLoading, |
| 28 | isDisabled, |
| 29 | onChange, |
| 30 | options, |
| 31 | }) => { |
| 32 | const instanceId = useInstanceId(MultiSelectControl); |
| 33 | const id = `give-multi-select-control-${name}-${instanceId}`; |
| 34 | |
| 35 | if (options && options.length < 1) { |
| 36 | return null; |
| 37 | } |
| 38 | return ( |
| 39 | <BaseControl label={label} hideLabelFromVision={hideLabelFromVision} id={id} help={help} className={className}> |
| 40 | <Select |
| 41 | isLoading={isLoading} |
| 42 | inputId={id} |
| 43 | value={value} |
| 44 | onChange={(selectedOptions) => onChange(selectedOptions)} |
| 45 | options={options} |
| 46 | maxMenuHeight="200px" |
| 47 | isDisabled={isDisabled} |
| 48 | placeholder={placeholder} |
| 49 | isMulti={true} |
| 50 | theme={(theme) => ({ |
| 51 | ...theme, |
| 52 | colors: { |
| 53 | ...theme.colors, |
| 54 | primary: '#007cba', |
| 55 | primary75: '#31a6e0', |
| 56 | primary50: '#5dbae8', |
| 57 | primary25: '#9edaf7', |
| 58 | }, |
| 59 | })} |
| 60 | /> |
| 61 | </BaseControl> |
| 62 | ); |
| 63 | }; |
| 64 | |
| 65 | MultiSelectControl.propTypes = { |
| 66 | label: PropTypes.string, |
| 67 | value: PropTypes.any.isRequired, |
| 68 | onChange: PropTypes.func, |
| 69 | options: PropTypes.array.isRequired, |
| 70 | name: PropTypes.string.isRequired, |
| 71 | help: PropTypes.string, |
| 72 | className: PropTypes.string, |
| 73 | hideLabelFromVision: PropTypes.bool, |
| 74 | isLoading: PropTypes.bool, |
| 75 | isDisabled: PropTypes.bool, |
| 76 | placeholder: PropTypes.string, |
| 77 | }; |
| 78 | |
| 79 | MultiSelectControl.defaultProps = { |
| 80 | label: null, |
| 81 | value: null, |
| 82 | onChange: null, |
| 83 | placeholder: `${__('Select', 'give')}...`, |
| 84 | options: null, |
| 85 | }; |
| 86 | |
| 87 | export default MultiSelectControl; |
| 88 |