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