edit.js
157 lines
| 1 | import preview from './preview'; |
| 2 | |
| 3 | const { |
| 4 | ToolBarFields, |
| 5 | BlockLabel, |
| 6 | BlockDescription, |
| 7 | BlockAdvancedValue, |
| 8 | BlockName, |
| 9 | AdvancedFields, |
| 10 | FieldWrapper, |
| 11 | FieldSettingsWrapper, |
| 12 | ValidationToggleGroup, |
| 13 | ValidationBlockMessage, |
| 14 | AdvancedInspectorControl, |
| 15 | AttributeHelp, |
| 16 | } = JetFBComponents; |
| 17 | const { |
| 18 | useIsAdvancedValidation, |
| 19 | useUniqueNameOnDuplicate, |
| 20 | } = JetFBHooks; |
| 21 | const { |
| 22 | __, |
| 23 | } = wp.i18n; |
| 24 | const { |
| 25 | InspectorControls, |
| 26 | useBlockProps, |
| 27 | } = wp.blockEditor; |
| 28 | const { |
| 29 | PanelBody, |
| 30 | TextControl, |
| 31 | __experimentalInputControl, |
| 32 | __experimentalNumberControl, |
| 33 | } = wp.components; |
| 34 | |
| 35 | let { InputControl, NumberControl } = wp.components; |
| 36 | |
| 37 | if ( typeof InputControl === 'undefined' ) { |
| 38 | InputControl = __experimentalInputControl; |
| 39 | } |
| 40 | |
| 41 | if ( typeof NumberControl === 'undefined' ) { |
| 42 | NumberControl = __experimentalNumberControl; |
| 43 | } |
| 44 | |
| 45 | export default function NumberEdit( props ) { |
| 46 | const blockProps = useBlockProps(); |
| 47 | const isAdvancedValidation = useIsAdvancedValidation(); |
| 48 | useUniqueNameOnDuplicate(); |
| 49 | |
| 50 | const { |
| 51 | attributes, |
| 52 | setAttributes, |
| 53 | isSelected, |
| 54 | editProps: { uniqKey }, |
| 55 | } = props; |
| 56 | |
| 57 | if ( attributes.isPreview ) { |
| 58 | return <div style={ { |
| 59 | width: '100%', |
| 60 | display: 'flex', |
| 61 | justifyContent: 'center', |
| 62 | } }> |
| 63 | { preview } |
| 64 | </div>; |
| 65 | } |
| 66 | |
| 67 | return [ |
| 68 | <ToolBarFields |
| 69 | key={ uniqKey( 'ToolBarFields' ) } |
| 70 | { ...props } |
| 71 | />, |
| 72 | 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 | <FieldSettingsWrapper { ...props }> |
| 85 | <AdvancedInspectorControl |
| 86 | value={ attributes.min } |
| 87 | label={ __( 'Min Value', 'jet-form-builder' ) } |
| 88 | onChangePreset={ min => setAttributes( { min } ) } |
| 89 | > |
| 90 | { ( { instanceId } ) => <TextControl |
| 91 | id={ instanceId } |
| 92 | className="jet-fb m-unset" |
| 93 | value={ attributes.min } |
| 94 | onChange={ min => setAttributes( { min } ) } |
| 95 | /> } |
| 96 | </AdvancedInspectorControl> |
| 97 | <AttributeHelp name="min"/> |
| 98 | <AdvancedInspectorControl |
| 99 | value={ attributes.max } |
| 100 | label={ __( 'Max Value', 'jet-form-builder' ) } |
| 101 | onChangePreset={ max => setAttributes( { max } ) } |
| 102 | > |
| 103 | { ( { instanceId } ) => <TextControl |
| 104 | id={ instanceId } |
| 105 | className="jet-fb m-unset" |
| 106 | value={ attributes.max } |
| 107 | onChange={ max => setAttributes( { max } ) } |
| 108 | /> } |
| 109 | </AdvancedInspectorControl> |
| 110 | <AttributeHelp name="max"/> |
| 111 | <AdvancedInspectorControl |
| 112 | value={ attributes.step } |
| 113 | label={ __( 'Step', 'jet-form-builder' ) } |
| 114 | onChangePreset={ step => setAttributes( { step } ) } |
| 115 | > |
| 116 | { ( { instanceId } ) => <TextControl |
| 117 | id={ instanceId } |
| 118 | className="jet-fb m-unset" |
| 119 | value={ attributes.step } |
| 120 | onChange={ step => setAttributes( { step } ) } |
| 121 | /> } |
| 122 | </AdvancedInspectorControl> |
| 123 | <AttributeHelp name="step"/> |
| 124 | </FieldSettingsWrapper> |
| 125 | <PanelBody |
| 126 | title={ __( 'Validation', 'jet-form-builder' ) } |
| 127 | > |
| 128 | <ValidationToggleGroup/> |
| 129 | { isAdvancedValidation && <> |
| 130 | { null !== attributes.min && ( |
| 131 | <ValidationBlockMessage name="number_min"/> |
| 132 | ) } |
| 133 | { null !== attributes.max && ( |
| 134 | <ValidationBlockMessage name="number_max"/> |
| 135 | ) } |
| 136 | <ValidationBlockMessage name="empty"/> |
| 137 | </> } |
| 138 | </PanelBody> |
| 139 | <AdvancedFields/> |
| 140 | </InspectorControls> |
| 141 | ), |
| 142 | <div { ...blockProps } key={ uniqKey( 'viewBlock' ) }> |
| 143 | <FieldWrapper |
| 144 | key={ uniqKey( 'FieldWrapper' ) } |
| 145 | { ...props } |
| 146 | > |
| 147 | <NumberControl |
| 148 | placeholder={ attributes.placeholder } |
| 149 | key={ uniqKey( 'place_holder_block' ) } |
| 150 | min={ attributes.min || 0 } |
| 151 | max={ attributes.max || 1000 } |
| 152 | step={ attributes.step || 1 } |
| 153 | /> |
| 154 | </FieldWrapper> |
| 155 | </div>, |
| 156 | ]; |
| 157 | } |