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