PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.3.3
Block Animations, Motion & Scroll Effects – Ghost Kit v3.3.3
3.7.2 3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / gutenberg / blocks / form / fields / select / edit.js

edit.js in Block Animations, Motion & Scroll Effects – Ghost Kit 3.3.3, at gutenberg/blocks/form/fields/select/edit.js

145 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import classnames from 'classnames/dedupe';
2
3 import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
4 import { PanelBody, SelectControl, ToggleControl } from '@wordpress/components';
5 import { applyFilters } from '@wordpress/hooks';
6 import { __ } from '@wordpress/i18n';
7
8 import {
9 FieldDefaultSettings,
10 getFieldAttributes,
11 } from '../../field-attributes';
12 import FieldDescription from '../../field-description';
13 import FieldLabel from '../../field-label';
14 import FieldOptions from '../../field-options';
15
16 /**
17 * Block Edit Class.
18 *
19 * @param props
20 */
21 export default function BlockEdit(props) {
22 const { attributes, setAttributes, isSelected } = props;
23
24 const { multiple } = attributes;
25
26 let { options } = attributes;
27
28 let { className = '' } = props;
29
30 className = classnames(
31 'ghostkit-form-field ghostkit-form-field-select',
32 className
33 );
34
35 className = applyFilters('ghostkit.editor.className', className, props);
36
37 if (!options || !options.length) {
38 options = [
39 {
40 label: '',
41 value: '',
42 selected: false,
43 },
44 ];
45 }
46
47 let selectVal = multiple ? [] : '';
48
49 options.forEach((data) => {
50 if (multiple && data.selected) {
51 selectVal.push(data.value);
52 } else if (!multiple && !selectVal && data.selected) {
53 selectVal = data.value;
54 }
55 });
56
57 const blockProps = useBlockProps({ className });
58
59 return (
60 <>
61 <InspectorControls>
62 <PanelBody>
63 <FieldDefaultSettings
64 {...props}
65 defaultCustom={' '}
66 placeholderCustom={' '}
67 />
68 <ToggleControl
69 label={__('Multiple', 'ghostkit')}
70 checked={multiple}
71 onChange={() => {
72 if (multiple) {
73 const newOptions = [...options];
74 let singleSelected = false;
75
76 newOptions.forEach((data, i) => {
77 if (data.selected) {
78 if (singleSelected) {
79 newOptions[i].selected = false;
80 }
81
82 singleSelected = true;
83 }
84 });
85
86 setAttributes({
87 multiple: !multiple,
88 options: newOptions,
89 });
90 } else {
91 setAttributes({ multiple: !multiple });
92 }
93 }}
94 />
95 </PanelBody>
96 </InspectorControls>
97 <div {...blockProps}>
98 <FieldLabel {...props} />
99
100 {isSelected ? (
101 <FieldOptions
102 options={options}
103 multiple={multiple}
104 onChange={(val) => setAttributes({ options: val })}
105 />
106 ) : (
107 <SelectControl
108 {...getFieldAttributes(attributes)}
109 value={selectVal}
110 options={(() => {
111 if (!multiple) {
112 let addNullOption = true;
113
114 Object.keys(options).forEach((data) => {
115 if (data.selected) {
116 addNullOption = false;
117 }
118 });
119
120 if (addNullOption) {
121 return [
122 {
123 label: __(
124 '--- Select ---',
125 'ghostkit'
126 ),
127 value: '',
128 selected: true,
129 },
130 ...options,
131 ];
132 }
133 }
134
135 return options;
136 })()}
137 />
138 )}
139
140 <FieldDescription {...props} />
141 </div>
142 </>
143 );
144 }
145