edit.js
181 lines
| 1 | import preview from './preview'; |
| 2 | |
| 3 | const { |
| 4 | ToolBarFields, |
| 5 | BlockLabel, |
| 6 | BlockName, |
| 7 | BlockDescription, |
| 8 | BlockAdvancedValue, |
| 9 | AdvancedFields, |
| 10 | FieldWrapper, |
| 11 | FieldSettingsWrapper, |
| 12 | AdvancedInspectorControl, |
| 13 | ClientSideMacros, |
| 14 | ValidationToggleGroup, |
| 15 | ValidationBlockMessage, |
| 16 | AttributeHelp, |
| 17 | } = JetFBComponents; |
| 18 | const { |
| 19 | useInsertMacro, |
| 20 | useIsAdvancedValidation, |
| 21 | useUniqueNameOnDuplicate, |
| 22 | } = JetFBHooks; |
| 23 | const { |
| 24 | __, |
| 25 | } = wp.i18n; |
| 26 | const { |
| 27 | InspectorControls, |
| 28 | useBlockProps, |
| 29 | } = wp.blockEditor; |
| 30 | const { |
| 31 | TextControl, |
| 32 | ToggleControl, |
| 33 | PanelBody, |
| 34 | __experimentalInputControl, |
| 35 | ExternalLink, |
| 36 | } = wp.components; |
| 37 | |
| 38 | let { InputControl } = wp.components; |
| 39 | |
| 40 | if ( typeof InputControl === 'undefined' ) { |
| 41 | InputControl = __experimentalInputControl; |
| 42 | } |
| 43 | |
| 44 | export default function DateEdit( props ) { |
| 45 | const blockProps = useBlockProps(); |
| 46 | |
| 47 | const [ minInput, updateMin ] = useInsertMacro( 'min' ); |
| 48 | const [ maxInput, updateMax ] = useInsertMacro( 'max' ); |
| 49 | |
| 50 | const { |
| 51 | attributes, |
| 52 | isSelected, |
| 53 | setAttributes, |
| 54 | editProps: { uniqKey, blockName, attrHelp }, |
| 55 | } = props; |
| 56 | |
| 57 | const isAdvancedValidation = useIsAdvancedValidation(); |
| 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 | const help = <> |
| 71 | { __( |
| 72 | 'Plain date should be in yyyy-mm-dd format.', |
| 73 | 'jet-form-builder', |
| 74 | ) } |
| 75 | |
| 76 | { __( 'Or you can use', 'jet-form-builder' ) } |
| 77 | |
| 78 | <ExternalLink |
| 79 | href="https://github.com/Crocoblock/jetformbuilder/wiki/Frontend-Macros---External-Macros#ctcurrentdate" |
| 80 | > |
| 81 | { __( 'macros', 'jet-form-builder' ) } |
| 82 | </ExternalLink>. |
| 83 | </>; |
| 84 | |
| 85 | return [ |
| 86 | <ToolBarFields |
| 87 | key={ uniqKey( 'JetForm-toolbar' ) } |
| 88 | { ...props } |
| 89 | />, |
| 90 | isSelected && <InspectorControls |
| 91 | key={ uniqKey( 'InspectorControls' ) } |
| 92 | > |
| 93 | <PanelBody title={ __( 'General', 'jet-form-builder' ) }> |
| 94 | <BlockLabel/> |
| 95 | <BlockName/> |
| 96 | <BlockDescription/> |
| 97 | </PanelBody> |
| 98 | <PanelBody title={ __( 'Value', 'jet-form-builder' ) }> |
| 99 | <BlockAdvancedValue |
| 100 | help={ help } |
| 101 | style={ { marginBottom: '1em' } } |
| 102 | /> |
| 103 | <ClientSideMacros> |
| 104 | <AdvancedInspectorControl |
| 105 | value={ attributes.min } |
| 106 | label={ __( 'Starting from date', 'jet-form-builder' ) } |
| 107 | onChangePreset={ min => setAttributes( { min } ) } |
| 108 | onChangeMacros={ updateMin } |
| 109 | > |
| 110 | { ( { instanceId } ) => <> |
| 111 | <TextControl |
| 112 | id={ instanceId } |
| 113 | ref={ minInput } |
| 114 | className="jet-fb m-unset" |
| 115 | value={ attributes.min } |
| 116 | onChange={ min => setAttributes( { min } ) } |
| 117 | /> |
| 118 | <AttributeHelp>{ help }</AttributeHelp> |
| 119 | </> } |
| 120 | </AdvancedInspectorControl> |
| 121 | <AdvancedInspectorControl |
| 122 | value={ attributes.max } |
| 123 | label={ __( 'Limit dates to', 'jet-form-builder' ) } |
| 124 | onChangePreset={ max => setAttributes( { max } ) } |
| 125 | onChangeMacros={ updateMax } |
| 126 | > |
| 127 | { ( { instanceId } ) => <> |
| 128 | <TextControl |
| 129 | id={ instanceId } |
| 130 | ref={ maxInput } |
| 131 | className="jet-fb m-unset" |
| 132 | value={ attributes.max } |
| 133 | onChange={ max => setAttributes( { max } ) } |
| 134 | /> |
| 135 | <AttributeHelp>{ help }</AttributeHelp> |
| 136 | </> } |
| 137 | </AdvancedInspectorControl> |
| 138 | </ClientSideMacros> |
| 139 | </PanelBody> |
| 140 | <FieldSettingsWrapper { ...props }> |
| 141 | <ToggleControl |
| 142 | key="is_timestamp" |
| 143 | label={ __( 'Is Timestamp' ) } |
| 144 | checked={ attributes.is_timestamp } |
| 145 | help={ attrHelp( 'is_timestamp' ) } |
| 146 | onChange={ ( newValue ) => { |
| 147 | setAttributes( { is_timestamp: Boolean( newValue ) } ); |
| 148 | } } |
| 149 | /> |
| 150 | </FieldSettingsWrapper> |
| 151 | <PanelBody |
| 152 | title={ __( 'Validation', 'jet-form-builder' ) } |
| 153 | > |
| 154 | <ValidationToggleGroup/> |
| 155 | { isAdvancedValidation && <> |
| 156 | { Boolean( attributes.min ) && <> |
| 157 | <ValidationBlockMessage name="date_min"/> |
| 158 | </> } |
| 159 | { Boolean( attributes.max ) && <> |
| 160 | <ValidationBlockMessage name="date_max"/> |
| 161 | </> } |
| 162 | <ValidationBlockMessage name="empty"/> |
| 163 | </> } |
| 164 | </PanelBody> |
| 165 | <AdvancedFields/> |
| 166 | </InspectorControls>, |
| 167 | <div { ...blockProps } key={ uniqKey( 'viewBlock' ) }> |
| 168 | <FieldWrapper |
| 169 | key={ uniqKey( 'FieldWrapper' ) } |
| 170 | { ...props } |
| 171 | > |
| 172 | <TextControl |
| 173 | onChange={ () => { |
| 174 | } } |
| 175 | key={ `place_holder_block_${ blockName }` } |
| 176 | placeholder={ 'Input type="date"' } |
| 177 | /> |
| 178 | </FieldWrapper> |
| 179 | </div>, |
| 180 | ]; |
| 181 | } |