index.js
80 lines
| 1 | |
| 2 | import Select from 'react-select'; |
| 3 | import CreatableSelect from 'react-select/creatable'; |
| 4 | import LabelAndHelpWrapper from '../LabelAndHelpWrapper'; |
| 5 | import './editor.scss'; |
| 6 | |
| 7 | export default ( props ) => { |
| 8 | const customStyles = { |
| 9 | indicatorSeparator: () => ( { |
| 10 | display: 'none', |
| 11 | } ), |
| 12 | |
| 13 | indicatorsContainer: ( provided ) => ( { |
| 14 | ...provided, |
| 15 | maxHeight: '30px', |
| 16 | } ), |
| 17 | |
| 18 | menuPortal: ( base ) => ( { |
| 19 | ...base, |
| 20 | zIndex: 999999, |
| 21 | } ), |
| 22 | |
| 23 | control: ( base ) => ( { |
| 24 | ...base, |
| 25 | marginBottom: '8px', |
| 26 | } ), |
| 27 | valueContainer: ( base ) => ( { |
| 28 | ...base, |
| 29 | padding: '0 6px', |
| 30 | } ), |
| 31 | input: ( base ) => ( { |
| 32 | ...base, |
| 33 | margin: 0, |
| 34 | padding: 0, |
| 35 | } ), |
| 36 | }; |
| 37 | |
| 38 | const customTheme = ( provided ) => ( { |
| 39 | borderRadius: 2, |
| 40 | colors: { |
| 41 | ...provided.colors, |
| 42 | primary: 'var(--wp-admin-theme-color)', |
| 43 | neutral20: '#757575', |
| 44 | neutral30: '#757575', |
| 45 | }, |
| 46 | spacing: { |
| 47 | controlHeight: 30, |
| 48 | baseUnit: 3, |
| 49 | menuGutter: 3, |
| 50 | }, |
| 51 | } ); |
| 52 | |
| 53 | const defaultProps = { |
| 54 | className: 'generate-advanced-select', |
| 55 | classNamePrefix: 'generate-advanced-select', |
| 56 | isSearchable: false, |
| 57 | styles: customStyles, |
| 58 | instanceId: 'input-field', |
| 59 | maxMenuHeight: 250, |
| 60 | theme: customTheme, |
| 61 | menuPortalTarget: document.querySelector( 'body' ), |
| 62 | }; |
| 63 | |
| 64 | const wrapperStyles = Object.assign( {}, { |
| 65 | marginBottom: '24px', |
| 66 | }, props?.wrapperStyles ); |
| 67 | |
| 68 | const SelectComponent = props?.isCreatable ? CreatableSelect : Select; |
| 69 | |
| 70 | const finalProps = Object.assign( {}, defaultProps, props ); |
| 71 | |
| 72 | return ( |
| 73 | <div style={ wrapperStyles }> |
| 74 | <LabelAndHelpWrapper label={ finalProps.label } htmlFor={ finalProps.id } help={ finalProps.help } > |
| 75 | <SelectComponent { ...finalProps } /> |
| 76 | </LabelAndHelpWrapper> |
| 77 | </div> |
| 78 | ); |
| 79 | }; |
| 80 |