Last active
December 5, 2020 17:54
-
-
Save psorensen/10f80e31ecea1870686c97eb93a79d1a to your computer and use it in GitHub Desktop.
GB Term Picker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Select from 'react-select'; | |
const { useSelect } = wp.data; | |
const { __ } = wp.i18n; | |
const TermPicker = (props) => { | |
const { taxonomy, onChange, label, attributes, attribute } = props; | |
const categories = useSelect((select) => select('core').getEntityRecords('taxonomy', taxonomy)); | |
if (categories && categories.length) { | |
// construct options for Select from retrieved categories | |
const options = categories.map((category) => ({ | |
label: category.name, | |
value: category.id, | |
})); | |
return ( | |
<div className="block-term-picker"> | |
<p>{label}</p> | |
<Select | |
multiple | |
options={options} | |
onChange={onChange} | |
isMulti | |
value={JSON.parse(attributes[attribute] || null)} | |
/> | |
</div> | |
); | |
} | |
return 'Loading...'; | |
}; | |
TermPicker.defaultProps = { | |
label: __('Select A Term', 'text-domain'), | |
}; | |
export default TermPicker; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment