PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.4.1
Block Animations, Motion & Scroll Effects – Ghost Kit v3.4.1
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.4.1, at gutenberg/blocks/form/fields/select/edit.js

148 lines 3.1 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 __nextHasNoMarginBottom
95 />
96 </PanelBody>
97 </InspectorControls>
98 <div {...blockProps}>
99 <FieldLabel {...props} />
100
101 {isSelected ? (
102 <FieldOptions
103 options={options}
104 multiple={multiple}
105 onChange={(val) => setAttributes({ options: val })}
106 />
107 ) : (
108 <SelectControl
109 {...getFieldAttributes(attributes)}
110 value={selectVal}
111 options={(() => {
112 if (!multiple) {
113 let addNullOption = true;
114
115 Object.keys(options).forEach((data) => {
116 if (data.selected) {
117 addNullOption = false;
118 }
119 });
120
121 if (addNullOption) {
122 return [
123 {
124 label: __(
125 '--- Select ---',
126 'ghostkit'
127 ),
128 value: '',
129 selected: true,
130 },
131 ...options,
132 ];
133 }
134 }
135
136 return options;
137 })()}
138 __next40pxDefaultSize
139 __nextHasNoMarginBottom
140 />
141 )}
142
143 <FieldDescription {...props} />
144 </div>
145 </>
146 );
147 }
148