edit.js
201 lines
| 1 | import preview from './preview'; |
| 2 | |
| 3 | const { |
| 4 | AdvancedFields, |
| 5 | FieldWrapper, |
| 6 | FieldSettingsWrapper, |
| 7 | BaseHelp, |
| 8 | ToolBarFields, |
| 9 | BlockName, |
| 10 | BlockDescription, |
| 11 | BlockLabel, |
| 12 | MacrosFields, |
| 13 | ClientSideMacros, |
| 14 | } = JetFBComponents; |
| 15 | const { |
| 16 | useUniqueNameOnDuplicate, |
| 17 | } = JetFBHooks; |
| 18 | |
| 19 | const { __ } = wp.i18n; |
| 20 | |
| 21 | const { |
| 22 | InspectorControls, |
| 23 | useBlockProps, |
| 24 | } = wp.blockEditor; |
| 25 | |
| 26 | const { |
| 27 | TextControl, |
| 28 | TextareaControl, |
| 29 | ToggleControl, |
| 30 | PanelBody, |
| 31 | Button, |
| 32 | Popover, |
| 33 | SelectControl, |
| 34 | __experimentalNumberControl, |
| 35 | } = wp.components; |
| 36 | |
| 37 | const NumberControl = __experimentalNumberControl; |
| 38 | |
| 39 | const help = { |
| 40 | calc_hidden: __( 'Check this to hide calculated field' ), |
| 41 | }; |
| 42 | |
| 43 | export default function EditCalculated( props ) { |
| 44 | const blockProps = useBlockProps(); |
| 45 | useUniqueNameOnDuplicate(); |
| 46 | |
| 47 | const { |
| 48 | attributes, |
| 49 | setAttributes, |
| 50 | isSelected, |
| 51 | editProps: { uniqKey }, |
| 52 | } = props; |
| 53 | |
| 54 | const insertMacros = ( macros ) => { |
| 55 | setAttributes( { |
| 56 | calc_formula: `${ attributes.calc_formula || '' }${ macros }`, |
| 57 | } ); |
| 58 | }; |
| 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 | <ClientSideMacros withThis> |
| 76 | <MacrosFields |
| 77 | onClick={ insertMacros } |
| 78 | /> |
| 79 | </ClientSideMacros> |
| 80 | </ToolBarFields>, |
| 81 | isSelected && <InspectorControls |
| 82 | key={ uniqKey( 'InspectorControls' ) } |
| 83 | > |
| 84 | <PanelBody title={ __( 'General', 'jet-form-builder' ) } |
| 85 | key={ 'jet-form-general-fields' }> |
| 86 | <BlockLabel/> |
| 87 | <BlockName/> |
| 88 | <BlockDescription/> |
| 89 | </PanelBody> |
| 90 | <FieldSettingsWrapper { ...props }> |
| 91 | <p |
| 92 | className={ 'components-base-control__help' } |
| 93 | style={ { marginTop: '0px', color: 'rgb(117, 117, 117)' } } |
| 94 | dangerouslySetInnerHTML={ { __html: JetFormCalculatedField.field_desc } } |
| 95 | /> |
| 96 | <SelectControl |
| 97 | label={ __( 'Value type', 'jet-form-builder' ) } |
| 98 | labelPosition="top" |
| 99 | value={ attributes.value_type } |
| 100 | onChange={ value_type => setAttributes( { value_type } ) } |
| 101 | options={ [ |
| 102 | { |
| 103 | value: 'number', |
| 104 | label: __( 'as Number', 'jet-form-builder' ), |
| 105 | }, |
| 106 | { |
| 107 | value: 'string', |
| 108 | label: __( 'as String', 'jet-form-builder' ), |
| 109 | }, |
| 110 | ] } |
| 111 | /> |
| 112 | { 'number' === attributes.value_type ? <> |
| 113 | <NumberControl |
| 114 | label={ __( 'Decimal Places Number', |
| 115 | 'jet-form-builder' ) } |
| 116 | labelPosition="top" |
| 117 | key="precision" |
| 118 | value={ attributes.precision } |
| 119 | onChange={ ( newValue ) => { |
| 120 | setAttributes( |
| 121 | { precision: parseInt( newValue ) } ); |
| 122 | } } |
| 123 | /> |
| 124 | <TextControl |
| 125 | key="calc_separate_decimals" |
| 126 | label={ __( 'Decimals separator' ) } |
| 127 | value={ attributes.separate_decimals } |
| 128 | onChange={ separate_decimals => setAttributes( |
| 129 | { separate_decimals } ) } |
| 130 | /> |
| 131 | <TextControl |
| 132 | key="calc_separate_thousands" |
| 133 | label={ __( 'Thousands separator' ) } |
| 134 | value={ attributes.separate_thousands } |
| 135 | onChange={ separate_thousands => setAttributes( |
| 136 | { separate_thousands } ) } |
| 137 | /> |
| 138 | <TextControl |
| 139 | key="calc_prefix" |
| 140 | label={ __( 'Calculated Value Prefix' ) } |
| 141 | value={ attributes.calc_prefix } |
| 142 | help={ __( |
| 143 | 'For space before or after text use: ' ) } |
| 144 | onChange={ ( newValue ) => { |
| 145 | setAttributes( { calc_prefix: newValue } ); |
| 146 | } } |
| 147 | /> |
| 148 | <TextControl |
| 149 | key="calc_suffix" |
| 150 | label={ __( 'Calculated Value Suffix' ) } |
| 151 | value={ attributes.calc_suffix } |
| 152 | help={ __( |
| 153 | 'For space before or after text use: ' ) } |
| 154 | onChange={ ( newValue ) => { |
| 155 | setAttributes( { calc_suffix: newValue } ); |
| 156 | } } |
| 157 | /> |
| 158 | </> : null } |
| 159 | <ToggleControl |
| 160 | key={ 'calc_hidden' } |
| 161 | label={ __( 'Hidden' ) } |
| 162 | checked={ attributes.calc_hidden } |
| 163 | help={ help.calc_hidden } |
| 164 | onChange={ newVal => { |
| 165 | setAttributes( { |
| 166 | calc_hidden: Boolean( newVal ), |
| 167 | } ); |
| 168 | } } |
| 169 | /> |
| 170 | </FieldSettingsWrapper> |
| 171 | <AdvancedFields |
| 172 | key={ uniqKey( 'JetForm-advanced' ) } |
| 173 | { ...props } |
| 174 | /> |
| 175 | </InspectorControls>, |
| 176 | <div { ...blockProps } key={ uniqKey( 'viewBlock' ) }> |
| 177 | <FieldWrapper |
| 178 | key={ uniqKey( 'FieldWrapper' ) } |
| 179 | { ...props } |
| 180 | > |
| 181 | <div className={ 'jet-form-builder__calculated-field' }> |
| 182 | <div |
| 183 | className={ 'calc-prefix' }>{ attributes.calc_prefix }</div> |
| 184 | <div |
| 185 | className={ 'calc-formula' }>{ attributes.calc_formula }</div> |
| 186 | <div |
| 187 | className={ 'calc-suffix' }>{ attributes.calc_suffix }</div> |
| 188 | </div> |
| 189 | { props.isSelected && <> |
| 190 | <TextareaControl |
| 191 | key="calc_formula" |
| 192 | value={ attributes.calc_formula } |
| 193 | onChange={ ( newValue ) => { |
| 194 | setAttributes( { calc_formula: newValue } ); |
| 195 | } } |
| 196 | /> |
| 197 | </> } |
| 198 | </FieldWrapper> |
| 199 | </div>, |
| 200 | ]; |
| 201 | } |