FieldSettings.js
2 years ago
FooterControls.js
2 years ago
HeaderControls.js
2 years ago
edit.js
2 years ago
index.js
2 years ago
preview.js
2 years ago
edit.js
129 lines
| 1 | import FieldSettings from './FieldSettings'; |
| 2 | import preview from './preview'; |
| 3 | |
| 4 | const { __ } = wp.i18n; |
| 5 | |
| 6 | const { |
| 7 | AdvancedFields, |
| 8 | FieldSettingsWrapper, |
| 9 | BlockLabel, |
| 10 | BlockDescription, |
| 11 | BlockAdvancedValue, |
| 12 | BlockName, |
| 13 | } = JetFBComponents; |
| 14 | |
| 15 | const { |
| 16 | useBlockAttributes, |
| 17 | useUniqueNameOnDuplicate, |
| 18 | } = JetFBHooks; |
| 19 | |
| 20 | const { |
| 21 | InspectorControls, |
| 22 | useBlockProps, |
| 23 | RichText, |
| 24 | } = wp.blockEditor; |
| 25 | |
| 26 | const { |
| 27 | Card, |
| 28 | CardHeader, |
| 29 | CardBody, |
| 30 | PanelBody, |
| 31 | } = wp.components; |
| 32 | |
| 33 | const { |
| 34 | useEffect, |
| 35 | } = wp.element; |
| 36 | |
| 37 | export default function HiddenEdit( props ) { |
| 38 | |
| 39 | const { |
| 40 | attributes, |
| 41 | setAttributes, |
| 42 | isSelected, |
| 43 | editProps: { uniqKey }, |
| 44 | } = props; |
| 45 | |
| 46 | const blockProps = useBlockProps(); |
| 47 | useUniqueNameOnDuplicate(); |
| 48 | |
| 49 | const [ , setAttrs ] = useBlockAttributes(); |
| 50 | |
| 51 | const resetRender = () => { |
| 52 | if ( 'referer_url' === attributes.field_value ) { |
| 53 | setAttributes( { render: true } ); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | useEffect( resetRender, [] ); |
| 58 | useEffect( resetRender, [ attributes.field_value ] ); |
| 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 { label = 'Please set `Field Value`' } = JetFormHiddenField.sources.find( |
| 71 | option => option.value === attributes.field_value, |
| 72 | ); |
| 73 | |
| 74 | const resultLabel = [ label ]; |
| 75 | |
| 76 | switch ( attributes.field_value ) { |
| 77 | case 'post_meta': |
| 78 | case 'user_meta': |
| 79 | resultLabel.push( attributes.hidden_value_field ); |
| 80 | break; |
| 81 | case 'query_var': |
| 82 | resultLabel.push( attributes.query_var_key ); |
| 83 | break; |
| 84 | case 'current_date': |
| 85 | resultLabel.push( attributes.date_format ); |
| 86 | break; |
| 87 | case 'manual_input': |
| 88 | resultLabel.push( attributes.hidden_value ); |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | return [ |
| 93 | isSelected && ( |
| 94 | <InspectorControls |
| 95 | key={ uniqKey( 'InspectorControls' ) } |
| 96 | > |
| 97 | <PanelBody title={ __( 'General', 'jet-form-builder' ) }> |
| 98 | <BlockLabel/> |
| 99 | <BlockName/> |
| 100 | <BlockDescription/> |
| 101 | </PanelBody> |
| 102 | <PanelBody title={ __( 'Value', 'jet-form-builder' ) }> |
| 103 | <BlockAdvancedValue/> |
| 104 | </PanelBody> |
| 105 | <FieldSettingsWrapper { ...props }> |
| 106 | <FieldSettings/> |
| 107 | </FieldSettingsWrapper> |
| 108 | <AdvancedFields/> |
| 109 | </InspectorControls> |
| 110 | ), |
| 111 | <div { ...blockProps } key={ uniqKey( 'viewBlock' ) }> |
| 112 | <Card elevation={ 2 }> |
| 113 | <CardHeader> |
| 114 | <RichText |
| 115 | placeholder="hidden_field_name..." |
| 116 | allowedFormats={ [] } |
| 117 | value={ attributes.name } |
| 118 | onChange={ name => setAttrs( { name } ) } |
| 119 | /> |
| 120 | </CardHeader> |
| 121 | <CardBody> |
| 122 | { isSelected && <FieldSettings/> } |
| 123 | { !isSelected && resultLabel.join( ': ' ) } |
| 124 | </CardBody> |
| 125 | </Card> |
| 126 | </div>, |
| 127 | ]; |
| 128 | }; |
| 129 |