AdvancedFields.js
2 years ago
AttributeHelp.js
2 years ago
BlockAddPrevButton.js
2 years ago
BlockAdvancedValue.js
2 years ago
BlockClassName.js
2 years ago
BlockDefaultValue.js
2 years ago
BlockDescription.js
2 years ago
BlockLabel.js
2 years ago
BlockName.js
2 years ago
BlockPlaceholder.js
2 years ago
BlockPrevButtonLabel.js
2 years ago
BlockRequired.js
2 years ago
BlockVisibility.js
2 years ago
FieldControl.js
2 years ago
FieldSettingsWrapper.js
2 years ago
FieldWrapper.js
2 years ago
GeneralFields.js
2 years ago
SelectVariations.js
2 years ago
ToggleGroupVariations.js
2 years ago
ToolBarDefault.js
2 years ago
ToolBarFields.js
2 years ago
FieldControl.js
198 lines
| 1 | import ControlsSettings from '../helpers/ControlsSettings'; |
| 2 | |
| 3 | const { |
| 4 | useBlockProps, |
| 5 | } = wp.blockEditor; |
| 6 | const { |
| 7 | TextControl, |
| 8 | SelectControl, |
| 9 | ToggleControl, |
| 10 | BaseControl, |
| 11 | __experimentalNumberControl, |
| 12 | |
| 13 | } = wp.components; |
| 14 | |
| 15 | let { NumberControl } = wp.components; |
| 16 | |
| 17 | if ( typeof NumberControl === 'undefined' ) { |
| 18 | NumberControl = __experimentalNumberControl; |
| 19 | } |
| 20 | |
| 21 | function useControls( { |
| 22 | type, |
| 23 | attributes, |
| 24 | attrsSettings = {}, |
| 25 | } ) { |
| 26 | const blockProps = useBlockProps(); |
| 27 | const blockName = blockProps[ 'data-type' ]; |
| 28 | const controls = ControlsSettings(); |
| 29 | |
| 30 | if ( !controls[ type ] ) { |
| 31 | return []; |
| 32 | } |
| 33 | |
| 34 | const currentControl = controls[ type ]; |
| 35 | |
| 36 | const isValidCondition = ( attr ) => { |
| 37 | if ( !attr.condition ) { |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | if ( blockName && attr.condition.blockName ) { |
| 42 | if ( 'string' === typeof attr.condition.blockName && blockName !== |
| 43 | attr.condition.blockName ) { |
| 44 | return false; |
| 45 | } |
| 46 | if ( 'object' === typeof attr.condition.blockName |
| 47 | && attr.condition.blockName.length |
| 48 | && !attr.condition.blockName.includes( blockName ) |
| 49 | ) { |
| 50 | return false; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | const objectNotMatch = ( |
| 55 | function () { |
| 56 | if ( 'object' !== typeof attr.condition.attr ) { |
| 57 | return true; |
| 58 | } |
| 59 | const { operator = 'and', items = {} } = attr.condition.attr; |
| 60 | |
| 61 | if ( 'or' === operator.toLowerCase() ) { |
| 62 | for ( const attrToCompare in items ) { |
| 63 | const valueCompare = items[ attrToCompare ]; |
| 64 | |
| 65 | if ( valueCompare === attributes[ attrToCompare ] ) { |
| 66 | return true; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if ( 'and' === operator.toLowerCase() ) { |
| 72 | return ( |
| 73 | function () { |
| 74 | for ( const attrToCompare in items ) { |
| 75 | if ( items[ attrToCompare ] !== |
| 76 | attributes[ attrToCompare ] ) { |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | )(); |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | )(); |
| 88 | |
| 89 | if ( !objectNotMatch |
| 90 | || ( |
| 91 | 'string' === typeof attr.condition.attr |
| 92 | && attr.condition.attr |
| 93 | && !attributes[ attr.condition.attr ] |
| 94 | ) |
| 95 | || ( |
| 96 | 'string' === typeof attr.condition |
| 97 | && !attributes[ attr.condition ] |
| 98 | ) |
| 99 | ) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | return true; |
| 104 | }; |
| 105 | |
| 106 | return currentControl.attrs.filter( |
| 107 | ( { help = '', attrName, label, ...attr } ) => { |
| 108 | const isRegisterAttribute = ( |
| 109 | attrName in attributes |
| 110 | ); |
| 111 | const validCondition = isValidCondition( attr ); |
| 112 | const isHidden = ( |
| 113 | attrName in attrsSettings |
| 114 | && 'show' in attrsSettings[ attrName ] |
| 115 | && false === attrsSettings[ attrName ].show |
| 116 | ); |
| 117 | |
| 118 | return ( |
| 119 | isRegisterAttribute && validCondition && !isHidden |
| 120 | ); |
| 121 | } ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @deprecated 3.0.0 |
| 126 | * |
| 127 | * @param props |
| 128 | * @return {unknown[]} |
| 129 | * @constructor |
| 130 | */ |
| 131 | function FieldControl( props ) { |
| 132 | let { |
| 133 | setAttributes, |
| 134 | attributes, |
| 135 | } = props; |
| 136 | |
| 137 | const fieldControls = useControls( props ); |
| 138 | |
| 139 | const onChangeValue = ( value, key ) => { |
| 140 | setAttributes( { [ key ]: value } ); |
| 141 | }; |
| 142 | |
| 143 | return fieldControls.map( ( { help = '', attrName, label, ...attr } ) => { |
| 144 | switch ( attr.type ) { |
| 145 | case 'text': |
| 146 | return <TextControl |
| 147 | key={ `${ attr.type }-${ attrName }-TextControl` } |
| 148 | label={ label } |
| 149 | help={ help } |
| 150 | value={ attributes[ attrName ] } |
| 151 | onChange={ newVal => onChangeValue( newVal, attrName ) } |
| 152 | />; |
| 153 | |
| 154 | case 'select': |
| 155 | return <SelectControl |
| 156 | key={ `${ attr.type }-${ attrName }-SelectControl` } |
| 157 | label={ label } |
| 158 | help={ help } |
| 159 | value={ attributes[ attrName ] } |
| 160 | options={ attr.options } |
| 161 | onChange={ newVal => { |
| 162 | onChangeValue( newVal, attrName ); |
| 163 | } } |
| 164 | />; |
| 165 | case 'toggle': |
| 166 | return <ToggleControl |
| 167 | key={ `${ attr.type }-${ attrName }-ToggleControl` } |
| 168 | label={ label } |
| 169 | help={ help } |
| 170 | checked={ attributes[ attrName ] } |
| 171 | onChange={ newVal => { |
| 172 | onChangeValue( newVal, attrName ); |
| 173 | } } |
| 174 | />; |
| 175 | case 'number': |
| 176 | return <BaseControl |
| 177 | key={ `${ attr.type }-${ attrName }-BaseControl` } |
| 178 | label={ label } |
| 179 | > |
| 180 | <NumberControl |
| 181 | key={ `${ attr.type }-${ attrName }-NumberControl` } |
| 182 | value={ attributes[ attrName ] } |
| 183 | onChange={ newVal => { |
| 184 | onChangeValue( Number( newVal ), attrName ); |
| 185 | } } |
| 186 | /> |
| 187 | <p className={ 'components-base-control__help' } |
| 188 | style={ { |
| 189 | color: 'rgb(117, 117, 117)', |
| 190 | } }>{ help }</p> |
| 191 | </BaseControl>; |
| 192 | } |
| 193 | |
| 194 | } ); |
| 195 | |
| 196 | } |
| 197 | |
| 198 | export default FieldControl; |