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