edit.js
191 lines
| 1 | import preview from './preview'; |
| 2 | |
| 3 | const { |
| 4 | BlockLabel, |
| 5 | BlockDescription, |
| 6 | BlockAdvancedValue, |
| 7 | BlockName, |
| 8 | AdvancedFields, |
| 9 | FieldWrapper, |
| 10 | ValidationToggleGroup, |
| 11 | ValidationBlockMessage, |
| 12 | AdvancedInspectorControl, |
| 13 | AttributeHelp, |
| 14 | } = JetFBComponents; |
| 15 | const { |
| 16 | useIsAdvancedValidation, |
| 17 | useUniqueNameOnDuplicate, |
| 18 | } = JetFBHooks; |
| 19 | const { |
| 20 | __, |
| 21 | } = wp.i18n; |
| 22 | const { |
| 23 | InspectorControls, |
| 24 | useBlockProps, |
| 25 | } = wp.blockEditor; |
| 26 | const { |
| 27 | TextControl, |
| 28 | PanelBody, |
| 29 | __experimentalNumberControl, |
| 30 | __experimentalInputControl, |
| 31 | } = wp.components; |
| 32 | const { |
| 33 | useState, |
| 34 | } = wp.element; |
| 35 | |
| 36 | let { NumberControl, InputControl } = wp.components; |
| 37 | |
| 38 | if ( typeof NumberControl === 'undefined' ) { |
| 39 | NumberControl = __experimentalNumberControl; |
| 40 | } |
| 41 | |
| 42 | if ( typeof InputControl === 'undefined' ) { |
| 43 | InputControl = __experimentalInputControl; |
| 44 | } |
| 45 | |
| 46 | export default function RangeEdit( props ) { |
| 47 | |
| 48 | const blockProps = useBlockProps(); |
| 49 | const isAdvancedValidation = useIsAdvancedValidation(); |
| 50 | |
| 51 | useUniqueNameOnDuplicate(); |
| 52 | |
| 53 | const [ rangeValue, setRangeValue ] = useState( 50 ); |
| 54 | |
| 55 | const { |
| 56 | attributes, |
| 57 | setAttributes, |
| 58 | editProps: { uniqKey, attrHelp }, |
| 59 | } = props; |
| 60 | |
| 61 | if ( attributes.isPreview ) { |
| 62 | return <div style={ { |
| 63 | width: '100%', |
| 64 | display: 'flex', |
| 65 | justifyContent: 'center', |
| 66 | } }> |
| 67 | { preview } |
| 68 | </div>; |
| 69 | } |
| 70 | |
| 71 | return [ |
| 72 | props.isSelected && ( |
| 73 | <InspectorControls |
| 74 | key={ uniqKey( 'InspectorControls' ) } |
| 75 | > |
| 76 | <PanelBody title={ __( 'General', 'jet-form-builder' ) }> |
| 77 | <BlockLabel/> |
| 78 | <BlockName/> |
| 79 | <BlockDescription/> |
| 80 | </PanelBody> |
| 81 | <PanelBody title={ __( 'Value', 'jet-form-builder' ) }> |
| 82 | <BlockAdvancedValue/> |
| 83 | </PanelBody> |
| 84 | <PanelBody |
| 85 | title={ __( 'Field', 'jet-form-builder' ) } |
| 86 | key={ uniqKey( 'PanelBody' ) } |
| 87 | > |
| 88 | <AdvancedInspectorControl |
| 89 | value={ attributes.min } |
| 90 | label={ __( 'Min Value', 'jet-form-builder' ) } |
| 91 | onChangePreset={ min => setAttributes( { min } ) } |
| 92 | > |
| 93 | { ( { instanceId } ) => <TextControl |
| 94 | id={ instanceId } |
| 95 | className="jet-fb m-unset" |
| 96 | value={ attributes.min } |
| 97 | onChange={ min => setAttributes( { min } ) } |
| 98 | /> } |
| 99 | </AdvancedInspectorControl> |
| 100 | <AttributeHelp name="min"/> |
| 101 | <AdvancedInspectorControl |
| 102 | value={ attributes.max } |
| 103 | label={ __( 'Max Value', 'jet-form-builder' ) } |
| 104 | onChangePreset={ max => setAttributes( { max } ) } |
| 105 | > |
| 106 | { ( { instanceId } ) => <TextControl |
| 107 | id={ instanceId } |
| 108 | className="jet-fb m-unset" |
| 109 | value={ attributes.max } |
| 110 | onChange={ max => setAttributes( { max } ) } |
| 111 | /> } |
| 112 | </AdvancedInspectorControl> |
| 113 | <AttributeHelp name="max"/> |
| 114 | <AdvancedInspectorControl |
| 115 | value={ attributes.step } |
| 116 | label={ __( 'Step', 'jet-form-builder' ) } |
| 117 | onChangePreset={ step => setAttributes( { step } ) } |
| 118 | > |
| 119 | { ( { instanceId } ) => <TextControl |
| 120 | id={ instanceId } |
| 121 | className="jet-fb m-unset" |
| 122 | value={ attributes.step } |
| 123 | onChange={ step => setAttributes( { step } ) } |
| 124 | /> } |
| 125 | </AdvancedInspectorControl> |
| 126 | <AttributeHelp name="step"/> |
| 127 | <TextControl |
| 128 | key="prefix" |
| 129 | label={ __( 'Value prefix' ) } |
| 130 | value={ attributes.prefix } |
| 131 | help={ attrHelp( 'prefix_suffix' ) } |
| 132 | onChange={ ( newValue ) => { |
| 133 | setAttributes( { prefix: newValue } ); |
| 134 | } } |
| 135 | /> |
| 136 | <TextControl |
| 137 | key="suffix" |
| 138 | label={ __( 'Value suffix' ) } |
| 139 | value={ attributes.suffix } |
| 140 | help={ attrHelp( 'prefix_suffix' ) } |
| 141 | onChange={ ( newValue ) => { |
| 142 | setAttributes( { suffix: newValue } ); |
| 143 | } } |
| 144 | /> |
| 145 | |
| 146 | </PanelBody> |
| 147 | <PanelBody |
| 148 | title={ __( 'Validation', 'jet-form-builder' ) } |
| 149 | > |
| 150 | <ValidationToggleGroup/> |
| 151 | { isAdvancedValidation && <> |
| 152 | <ValidationBlockMessage name="empty"/> |
| 153 | <ValidationBlockMessage name="number_max"/> |
| 154 | <ValidationBlockMessage name="number_min"/> |
| 155 | </> } |
| 156 | </PanelBody> |
| 157 | <AdvancedFields |
| 158 | key={ uniqKey( 'AdvancedFields' ) } |
| 159 | { ...props } |
| 160 | /> |
| 161 | </InspectorControls> |
| 162 | ), |
| 163 | <div { ...blockProps } key={ uniqKey( 'viewBlock' ) }> |
| 164 | <FieldWrapper |
| 165 | key={ uniqKey( 'FieldWrapper' ) } |
| 166 | wrapClasses={ [ |
| 167 | 'range-wrap', |
| 168 | ] } |
| 169 | { ...props } |
| 170 | > |
| 171 | <div className="range-flex-wrap"> |
| 172 | <InputControl |
| 173 | key={ uniqKey( 'placeholder_block' ) } |
| 174 | type={ 'range' } |
| 175 | min={ attributes.min || 0 } |
| 176 | max={ attributes.max || 100 } |
| 177 | step={ attributes.step || 1 } |
| 178 | onChange={ setRangeValue } |
| 179 | /> |
| 180 | <div className={ 'jet-form-builder__field-value' }> |
| 181 | <span |
| 182 | className={ 'jet-form-builder__field-value-prefix' }>{ attributes.prefix }</span> |
| 183 | <span>{ rangeValue }</span> |
| 184 | <span |
| 185 | className={ 'jet-form-builder__field-value-suffix' }>{ attributes.suffix }</span> |
| 186 | </div> |
| 187 | </div> |
| 188 | </FieldWrapper> |
| 189 | </div>, |
| 190 | ]; |
| 191 | } |