PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.2.0
GenerateBlocks v2.2.0
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / blocks / query / components / ControlBuilder.jsx
generateblocks / src / blocks / query / components Last commit date
AddQueryParameterButton.jsx 1 year ago BlockSettings.jsx 1 year ago ControlBuilder.jsx 1 year ago DateQueryControl.jsx 1 year ago DateTimeControl.jsx 1 year ago ParameterControl.jsx 1 year ago ParameterList.jsx 1 year ago QueryInspectorControls.jsx 1 year ago SelectQueryParameter.jsx 1 year ago TaxonomyParameterControl.jsx 1 year ago editor.scss 1 year ago
ControlBuilder.jsx
163 lines
1 import { ToggleControl, Button, Tooltip, ComboboxControl, TextControl } from '@wordpress/components';
2 import { sprintf, __ } from '@wordpress/i18n';
3
4 import { isArray, isObject } from 'lodash';
5 import { SelectPostType, SelectPost, MultiSelect, SelectUser } from '@edge22/components';
6
7 import { TaxonomyParameterControl } from './TaxonomyParameterControl';
8 import { DateQueryControl } from './DateQueryControl';
9
10 import getIcon from '@utils/get-icon';
11
12 function ControlComponent( props ) {
13 const {
14 postType,
15 queryClient,
16 ...standardProps
17 } = props;
18
19 switch ( props?.type ) {
20 case 'text':
21 return <TextControl { ...standardProps } />;
22 case 'number':
23 return (
24 <TextControl
25 { ...standardProps }
26 value={ standardProps.value }
27 type="number"
28 min="-1"
29 onChange={ ( newValue ) => {
30 if ( parseInt( newValue, 10 ) < -1 ) {
31 standardProps.onChange( -1 );
32 return;
33 }
34
35 standardProps.onChange( newValue );
36 } }
37 />
38 );
39 case 'postTypeSelect':
40 return <SelectPostType multiple={ true } { ...standardProps } />;
41 case 'select':
42 return <ComboboxControl { ...standardProps } />;
43 case 'multiSelect':
44 return <MultiSelect { ...standardProps } />;
45 case 'authorsSelect':
46 return (
47 <SelectUser
48 { ...standardProps }
49 multiple={ true }
50 currentLabel={ __( 'Current author', 'generateblocks' ) }
51 queryClient={ queryClient }
52 />
53 );
54 case 'taxonomySelect':
55 return <TaxonomyParameterControl postType={ postType } { ...standardProps } />;
56 case 'includePosts':
57 return (
58 <SelectPost
59 { ...standardProps }
60 includeCurrent={ false }
61 multiple={ true }
62 postType={ postType }
63 queryClient={ queryClient }
64 />
65 );
66 case 'includeParent':
67 case 'excludeParent':
68 return (
69 <SelectPost
70 { ...standardProps }
71 multiple={ true }
72 postType={ postType }
73 queryClient={ queryClient }
74 includeCurrent={ true }
75 />
76 );
77 case 'excludePosts':
78 return (
79 <SelectPost
80 { ...standardProps }
81 multiple={ true }
82 postType={ postType }
83 queryClient={ queryClient }
84 />
85 );
86 case 'dateQuery':
87 return <DateQueryControl { ...standardProps } />;
88 case 'toggleControl':
89 return <ToggleControl { ...standardProps } />;
90 }
91 }
92
93 export function ControlBuilder( props ) {
94 const {
95 id,
96 type,
97 label,
98 description,
99 selectOptions = [],
100 isSticky,
101 value,
102 default: defaultValue,
103 onChange,
104 onClickRemove,
105 dependencies,
106 placeholder,
107 postType,
108 queryClient,
109 } = props;
110
111 let controlDescription = description;
112
113 if ( 'posts_per_page' === id && ( '-1' === value || parseInt( value ) > parseInt( generateBlocksInfo.queryLoopEditorPostsCap ) ) ) {
114 controlDescription += ' ' + sprintf(
115 'Editor only: A maximum of %s posts can be previewed in the editor.',
116 generateBlocksInfo.queryLoopEditorPostsCap
117 );
118 }
119
120 const defaultValuePlaceholder = !! defaultValue && ( ! isArray( defaultValue ) || ! isObject( defaultValue ) )
121 ? defaultValue
122 : undefined;
123
124 const controlPlaceholder = placeholder || defaultValuePlaceholder;
125 const isPostsPerPage = 'number' === type && 'posts_per_page' === id;
126
127 const controlProps = {
128 id,
129 type,
130 label,
131 help: controlDescription,
132 options: selectOptions,
133 value,
134 placeholder: controlPlaceholder,
135 onChange,
136 min: isPostsPerPage ? -1 : undefined,
137 postType,
138 queryClient,
139 ...dependencies,
140 };
141
142 return (
143 <div className={ 'gb-parameter-component' }>
144 <ControlComponent { ...controlProps } />
145 { ! isSticky && (
146 <Tooltip text={ __( 'Delete parameter', 'generateblocks-pro' ) }>
147 <Button
148 className="gb-remove-parameter"
149 onClick={ () => {
150 // eslint-disable-next-line
151 if ( window.confirm( __( 'This will permanently delete this parameter.', 'generateblocks' ) ) ) {
152 onClickRemove( id );
153 }
154 } }
155 icon={ getIcon( 'trash' ) }
156 />
157 </Tooltip>
158 ) }
159 </div>
160 );
161 }
162
163