| 1 |
import React, { useLayoutEffect } from 'react'; |
| 2 |
import { SelectControl } from '@wordpress/components'; |
| 3 |
import styles from './editor.lazy.scss'; |
| 4 |
import PropTypes from 'prop-types'; |
| 5 |
|
| 6 |
// Use the onChange prop only if needed. |
| 7 |
// When using the onChange prop, you may skip the label KV-Pair of the data prop and the setAttributes prop. |
| 8 |
// Children can now be declared as Options or OptGroups, as in the WP Select Control. Skip the options prop in this case. |
| 9 |
|
| 10 |
const propTypes = { |
| 11 |
label: PropTypes.string, |
| 12 |
layout: PropTypes.string, |
| 13 |
options: PropTypes.array, |
| 14 |
data: PropTypes.object, |
| 15 |
setAttributes: PropTypes.func, |
| 16 |
onChange: PropTypes.func, |
| 17 |
help: PropTypes.string, |
| 18 |
}; |
| 19 |
|
| 20 |
const defaultProps = { |
| 21 |
layout: 'inline', |
| 22 |
onChange: null, |
| 23 |
}; |
| 24 |
|
| 25 |
export default function UAGSelectControl( { layout, label, options, data, setAttributes, onChange, help, children } ) { |
| 26 |
useLayoutEffect( () => { |
| 27 |
styles.use(); |
| 28 |
return () => { |
| 29 |
styles.unuse(); |
| 30 |
}; |
| 31 |
}, [] ); |
| 32 |
return ( |
| 33 |
children ? ( |
| 34 |
<div className={ `uagb-select-control uagb-select-control--layout-${ layout }` }> |
| 35 |
<SelectControl |
| 36 |
label={ label } |
| 37 |
value={ data.value } |
| 38 |
onChange={ ( value ) => ( |
| 39 |
onChange ? onChange( value ) : setAttributes( { [data.label]: value } ) |
| 40 |
) } |
| 41 |
help={ help } |
| 42 |
> |
| 43 |
{ children } |
| 44 |
</SelectControl> |
| 45 |
</div> |
| 46 |
) : ( |
| 47 |
<div className={ `uagb-select-control uagb-select-control--layout-${ layout }` }> |
| 48 |
<SelectControl |
| 49 |
label={ label } |
| 50 |
value={ data.value } |
| 51 |
onChange={ ( value ) => ( |
| 52 |
onChange ? onChange( value ) : setAttributes( { [data.label]: value } ) |
| 53 |
) } |
| 54 |
options={ options } |
| 55 |
help={ help } |
| 56 |
/> |
| 57 |
</div> |
| 58 |
) |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
UAGSelectControl.propTypes = propTypes; |
| 63 |
UAGSelectControl.defaultProps = defaultProps; |
| 64 |
|