edit.js
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import { __ } from '@wordpress/i18n'; |
| 5 | import { ToggleControl, SelectControl } from '@wordpress/components'; |
| 6 | import { InspectorControls, RichText } from '@wordpress/block-editor'; |
| 7 | import { useEffect } from '@wordpress/element'; |
| 8 | import InspectorTabs from '@Components/inspector-tabs/InspectorTabs.js'; |
| 9 | import InspectorTab, { |
| 10 | SRFMTabs, |
| 11 | } from '@Components/inspector-tabs/InspectorTab.js'; |
| 12 | import SRFMAdvancedPanelBody from '@Components/advanced-panel-body'; |
| 13 | import SRFMTextControl from '@Components/text-control'; |
| 14 | import { useGetCurrentFormId } from '../../blocks-attributes/getFormId'; |
| 15 | import { InputComponent } from './components/default.js'; |
| 16 | import Range from '@Components/range/Range.js'; |
| 17 | import AddInitialAttr from '@Controls/addInitialAttr'; |
| 18 | import { compose } from '@wordpress/compose'; |
| 19 | import widthOptions from '../width-options.json'; |
| 20 | import { FieldsPreview } from '../FieldsPreview.jsx'; |
| 21 | import { decodeHtmlEntities } from '@Blocks/util'; |
| 22 | |
| 23 | const Edit = ( { clientId, attributes, setAttributes } ) => { |
| 24 | const { |
| 25 | label, |
| 26 | fieldWidth, |
| 27 | placeholder, |
| 28 | help, |
| 29 | required, |
| 30 | block_id, |
| 31 | defaultValue, |
| 32 | errorMsg, |
| 33 | textLength, |
| 34 | isUnique, |
| 35 | duplicateMsg, |
| 36 | formId, |
| 37 | preview, |
| 38 | } = attributes; |
| 39 | |
| 40 | const currentFormId = useGetCurrentFormId( clientId ); |
| 41 | |
| 42 | useEffect( () => { |
| 43 | if ( formId !== currentFormId ) { |
| 44 | setAttributes( { formId: currentFormId } ); |
| 45 | } |
| 46 | }, [ formId, setAttributes, currentFormId ] ); |
| 47 | |
| 48 | // show the block preview on hover. |
| 49 | if ( preview ) { |
| 50 | const fieldName = srfm_fields_preview.input_preview; |
| 51 | return <FieldsPreview fieldName={ fieldName } />; |
| 52 | } |
| 53 | |
| 54 | return ( |
| 55 | <> |
| 56 | <InspectorControls> |
| 57 | <InspectorTabs |
| 58 | tabs={ [ 'general', 'advance' ] } |
| 59 | defaultTab={ 'general' } |
| 60 | > |
| 61 | <InspectorTab { ...SRFMTabs.general }> |
| 62 | <SRFMAdvancedPanelBody |
| 63 | title={ __( 'Attributes', 'sureforms' ) } |
| 64 | initialOpen={ true } |
| 65 | > |
| 66 | <SelectControl |
| 67 | label={ __( 'Field Width', 'sureforms' ) } |
| 68 | value={ fieldWidth } |
| 69 | options={ widthOptions } |
| 70 | onChange={ ( value ) => |
| 71 | setAttributes( { |
| 72 | fieldWidth: Number( value ), |
| 73 | } ) |
| 74 | } |
| 75 | __nextHasNoMarginBottom |
| 76 | /> |
| 77 | <SRFMTextControl |
| 78 | label={ __( 'Label', 'sureforms' ) } |
| 79 | value={ label } |
| 80 | data={ { |
| 81 | value: label, |
| 82 | label: 'label', |
| 83 | } } |
| 84 | onChange={ ( value ) => |
| 85 | setAttributes( { label: value } ) |
| 86 | } |
| 87 | /> |
| 88 | <SRFMTextControl |
| 89 | label={ __( 'Placeholder', 'sureforms' ) } |
| 90 | value={ placeholder } |
| 91 | data={ { |
| 92 | value: placeholder, |
| 93 | label: 'placeholder', |
| 94 | } } |
| 95 | onChange={ ( value ) => |
| 96 | setAttributes( { placeholder: value } ) |
| 97 | } |
| 98 | /> |
| 99 | <SRFMTextControl |
| 100 | label={ __( 'Default Value', 'sureforms' ) } |
| 101 | className="srfm-with-dropdown" |
| 102 | value={ defaultValue } |
| 103 | withSmartTagDropdown={ true } |
| 104 | data={ { |
| 105 | value: defaultValue, |
| 106 | label: 'defaultValue', |
| 107 | } } |
| 108 | onChange={ ( value ) => |
| 109 | setAttributes( { defaultValue: value } ) |
| 110 | } |
| 111 | /> |
| 112 | <ToggleControl |
| 113 | label={ __( 'Required', 'sureforms' ) } |
| 114 | checked={ required } |
| 115 | onChange={ ( checked ) => |
| 116 | setAttributes( { required: checked } ) |
| 117 | } |
| 118 | /> |
| 119 | { required && ( |
| 120 | <SRFMTextControl |
| 121 | label={ __( 'Error message', 'sureforms' ) } |
| 122 | value={ errorMsg } |
| 123 | data={ { |
| 124 | value: errorMsg, |
| 125 | label: 'errorMsg', |
| 126 | } } |
| 127 | onChange={ ( value ) => |
| 128 | setAttributes( { errorMsg: value } ) |
| 129 | } |
| 130 | /> |
| 131 | ) } |
| 132 | <ToggleControl |
| 133 | label={ __( |
| 134 | 'Validate as unique', |
| 135 | 'sureforms' |
| 136 | ) } |
| 137 | checked={ isUnique } |
| 138 | onChange={ ( checked ) => |
| 139 | setAttributes( { isUnique: checked } ) |
| 140 | } |
| 141 | /> |
| 142 | { isUnique && ( |
| 143 | <SRFMTextControl |
| 144 | label={ __( |
| 145 | 'Validation Message for Duplicate ', |
| 146 | 'sureforms' |
| 147 | ) } |
| 148 | value={ duplicateMsg } |
| 149 | data={ { |
| 150 | value: duplicateMsg, |
| 151 | label: 'duplicateMsg', |
| 152 | } } |
| 153 | onChange={ ( value ) => |
| 154 | setAttributes( { duplicateMsg: value } ) |
| 155 | } |
| 156 | /> |
| 157 | ) } |
| 158 | <SRFMTextControl |
| 159 | label={ __( 'Help', 'sureforms' ) } |
| 160 | value={ help } |
| 161 | data={ { |
| 162 | value: help, |
| 163 | label: 'help', |
| 164 | } } |
| 165 | onChange={ ( value ) => |
| 166 | setAttributes( { help: value } ) |
| 167 | } |
| 168 | /> |
| 169 | <Range |
| 170 | label={ __( |
| 171 | 'Maximum text length', |
| 172 | 'sureforms' |
| 173 | ) } |
| 174 | displayUnit={ false } |
| 175 | value={ textLength } |
| 176 | min={ 0 } |
| 177 | max={ 1000 } |
| 178 | data={ { |
| 179 | value: textLength, |
| 180 | label: 'textLength', |
| 181 | } } |
| 182 | onChange={ ( value ) => |
| 183 | setAttributes( { |
| 184 | textLength: Number( value ), |
| 185 | } ) |
| 186 | } |
| 187 | /> |
| 188 | </SRFMAdvancedPanelBody> |
| 189 | </InspectorTab> |
| 190 | <InspectorTab { ...SRFMTabs.style }></InspectorTab> |
| 191 | </InspectorTabs> |
| 192 | </InspectorControls> |
| 193 | <> |
| 194 | <InputComponent |
| 195 | blockID={ block_id } |
| 196 | setAttributes={ setAttributes } |
| 197 | attributes={ attributes } |
| 198 | /> |
| 199 | |
| 200 | { help !== '' && ( |
| 201 | <RichText |
| 202 | tagName="label" |
| 203 | value={ help } |
| 204 | onChange={ ( value ) => { |
| 205 | setAttributes( { |
| 206 | help: decodeHtmlEntities( value ), |
| 207 | } ); |
| 208 | } } |
| 209 | className="srfm-description" |
| 210 | multiline={ false } |
| 211 | id={ block_id } |
| 212 | allowedFormats={ [] } |
| 213 | /> |
| 214 | ) } |
| 215 | </> |
| 216 | </> |
| 217 | ); |
| 218 | }; |
| 219 | |
| 220 | export default compose( AddInitialAttr )( Edit ); |
| 221 |