generateblocks
/
src
/
blocks
/
query-loop
/
components
/
inspector-controls
/
parameter-list
/
ControlBuilder.js
generateblocks
/
src
/
blocks
/
query-loop
/
components
/
inspector-controls
/
parameter-list
Last commit date
ControlBuilder.js
3 years ago
ParameterControl.js
3 years ago
RemoveButton.js
4 years ago
index.js
3 years ago
ControlBuilder.js
89 lines
| 1 | import { ToggleControl } from '@wordpress/components'; |
| 2 | import { __ } from '@wordpress/i18n'; |
| 3 | import SelectPostType from '../../../../../extend/dynamic-content/components/SelectPostType'; |
| 4 | import SimpleSelect from '../../../../../components/simple-select'; |
| 5 | import AuthorsSelect from '../../../../../components/authors-select'; |
| 6 | import { CategoriesSelect, TagsSelect } from '../../../../../components/taxonomies-select'; |
| 7 | import RemoveButton from './RemoveButton'; |
| 8 | import TaxonomyParameterControl from '../controls/TaxonomyParameterControl'; |
| 9 | import PostTypeRecordsSelect from '../../../../../components/post-type-records-select'; |
| 10 | import DateTimePicker from '../controls/DateTimePicker'; |
| 11 | import DebouncedTextControl from '../../../../../components/debounced-text-control'; |
| 12 | import SimpleMultiSelect from '../../../../../components/simple-multi-select'; |
| 13 | import { isArray, isObject } from 'lodash'; |
| 14 | |
| 15 | const getParameterControl = ( parameterType ) => { |
| 16 | switch ( parameterType ) { |
| 17 | case 'text': |
| 18 | case 'number': |
| 19 | return DebouncedTextControl; |
| 20 | case 'postTypeSelect': |
| 21 | return SelectPostType; |
| 22 | case 'select': |
| 23 | return SimpleSelect; |
| 24 | case 'multiSelect': |
| 25 | return SimpleMultiSelect; |
| 26 | case 'authorsSelect': |
| 27 | return AuthorsSelect; |
| 28 | case 'categoriesSelect': |
| 29 | return CategoriesSelect; |
| 30 | case 'tagsSelect': |
| 31 | return TagsSelect; |
| 32 | case 'taxonomySelect': |
| 33 | return TaxonomyParameterControl; |
| 34 | case 'postsSelect': |
| 35 | return PostTypeRecordsSelect; |
| 36 | case 'dateTimePicker': |
| 37 | return DateTimePicker; |
| 38 | case 'toggleControl': |
| 39 | return ToggleControl; |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | export default function ControlBuilder( props ) { |
| 44 | const { |
| 45 | id, |
| 46 | type, |
| 47 | label, |
| 48 | description, |
| 49 | selectOptions = [], |
| 50 | isSticky, |
| 51 | value, |
| 52 | default: defaultValue, |
| 53 | onChange, |
| 54 | onClickRemove, |
| 55 | dependencies, |
| 56 | placeholder, |
| 57 | } = props; |
| 58 | |
| 59 | const Control = getParameterControl( type ); |
| 60 | let controlDescription = description; |
| 61 | |
| 62 | if ( 'per_page' === id && ( '-1' === value || value > 50 ) ) { |
| 63 | controlDescription += ' ' + __( 'Editor only: A maximum of 50 posts can be previewed in the editor.', 'generateblocks' ); |
| 64 | } |
| 65 | |
| 66 | const defaultValuePlaceholder = !! defaultValue && ( ! isArray( defaultValue ) || ! isObject( defaultValue ) ) |
| 67 | ? defaultValue |
| 68 | : undefined; |
| 69 | |
| 70 | const controlPlaceholder = placeholder || defaultValuePlaceholder; |
| 71 | |
| 72 | return ( |
| 73 | <div className={ 'gblocks-parameter-component' }> |
| 74 | <Control |
| 75 | id={ id } |
| 76 | type={ type } |
| 77 | label={ label } |
| 78 | help={ controlDescription } |
| 79 | options={ selectOptions } |
| 80 | value={ value } |
| 81 | placeholder={ controlPlaceholder } |
| 82 | onChange={ onChange } |
| 83 | { ...dependencies } |
| 84 | /> |
| 85 | { ! isSticky && <RemoveButton id={ id } onClick={ onClickRemove } /> } |
| 86 | </div> |
| 87 | ); |
| 88 | } |
| 89 |