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