deprecations
2 years ago
condition.group.item.js
2 years ago
conditional.model.context.js
2 years ago
conditions.is.empty.js
2 years ago
conditions.list.js
2 years ago
conditions.modal.content.js
2 years ago
conditions.modal.js
2 years ago
edit.js
2 years ago
index.js
2 years ago
options.js
2 years ago
preview.js
2 years ago
save.js
2 years ago
edit.js
187 lines
| 1 | import ConditionsModal from './conditions.modal'; |
| 2 | import ConditionsList from './conditions.list'; |
| 3 | import ConditionalModalContext from './conditional.model.context'; |
| 4 | import preview from './preview'; |
| 5 | |
| 6 | const { |
| 7 | getCurrentInnerBlocks, |
| 8 | } = JetFBActions; |
| 9 | |
| 10 | const { |
| 11 | __, |
| 12 | } = wp.i18n; |
| 13 | const { |
| 14 | useSelect, |
| 15 | } = wp.data; |
| 16 | |
| 17 | const { |
| 18 | BlockControls, |
| 19 | InnerBlocks, |
| 20 | useBlockProps, |
| 21 | InspectorControls, |
| 22 | } = wp.blockEditor; |
| 23 | |
| 24 | const { |
| 25 | Button, |
| 26 | ToolbarGroup, |
| 27 | TextControl, |
| 28 | PanelBody, |
| 29 | Tip, |
| 30 | } = wp.components; |
| 31 | |
| 32 | const { |
| 33 | useState, |
| 34 | useEffect, |
| 35 | RawHTML, |
| 36 | } = wp.element; |
| 37 | |
| 38 | function hasFormBreakField( blocks ) { |
| 39 | return blocks.some( ( { name } ) => name.includes( 'form-break-field' ) ); |
| 40 | } |
| 41 | |
| 42 | export default function ConditionalBlockEdit( props ) { |
| 43 | |
| 44 | const blockProps = useBlockProps(); |
| 45 | |
| 46 | const { |
| 47 | setAttributes, |
| 48 | attributes, |
| 49 | clientId, |
| 50 | editProps: { uniqKey }, |
| 51 | } = props; |
| 52 | |
| 53 | useEffect( () => { |
| 54 | if ( !attributes.name ) { |
| 55 | setAttributes( { name: clientId } ); |
| 56 | } |
| 57 | }, [] ); |
| 58 | |
| 59 | const blocks = getCurrentInnerBlocks(); |
| 60 | const blockNames = blocks.reduce( |
| 61 | ( prev, { name } ) => prev + name, '', |
| 62 | ); |
| 63 | |
| 64 | const [ showModal, setShowModal ] = useState( false ); |
| 65 | const [ showMultistep, setShowMultistep ] = useState( |
| 66 | () => hasFormBreakField( blocks ), |
| 67 | ); |
| 68 | |
| 69 | useEffect( () => { |
| 70 | setShowMultistep( hasFormBreakField( blocks ) ); |
| 71 | }, [ blockNames ] ); |
| 72 | |
| 73 | const functionDisplay = useSelect( select => ( |
| 74 | select( 'jet-forms/block-conditions' ).getFunctionDisplay( |
| 75 | attributes.func_type || 'show', |
| 76 | ) |
| 77 | ), [ attributes.func_type ] ); |
| 78 | |
| 79 | const conditionsIcon = attributes?.conditions?.length ? <span |
| 80 | className="dashicon dashicons dashicons-randomize" |
| 81 | data-count={ attributes.conditions.reduce( |
| 82 | ( prev, current ) => current?.or_operator ? prev : prev + 1, |
| 83 | 0, |
| 84 | ) } |
| 85 | /> : <span className="dashicon dashicons dashicons-randomize"/>; |
| 86 | |
| 87 | // jet-form-builder--hidden |
| 88 | |
| 89 | if ( attributes.isPreview ) { |
| 90 | return <div style={ { |
| 91 | width: '100%', |
| 92 | display: 'flex', |
| 93 | justifyContent: 'center', |
| 94 | } }> |
| 95 | { preview } |
| 96 | </div>; |
| 97 | } |
| 98 | |
| 99 | return <> |
| 100 | <div { ...blockProps }> |
| 101 | <div className="jet-form-builder__conditional"> |
| 102 | <InnerBlocks |
| 103 | key={ uniqKey( 'conditional-fields' ) } |
| 104 | /> |
| 105 | </div> |
| 106 | </div> |
| 107 | { showModal && <ConditionsModal |
| 108 | key={ uniqKey( 'ConditionsModal' ) } |
| 109 | setShowModal={ setShowModal } |
| 110 | /> } |
| 111 | <BlockControls> |
| 112 | <ToolbarGroup key={ uniqKey( 'ToolbarGroup' ) }> |
| 113 | <Button |
| 114 | className={ 'jet-fb-button' } |
| 115 | key={ uniqKey( 'randomize' ) } |
| 116 | isTertiary |
| 117 | isSmall |
| 118 | icon={ conditionsIcon } |
| 119 | onClick={ () => setShowModal( true ) } |
| 120 | /> |
| 121 | </ToolbarGroup> |
| 122 | </BlockControls> |
| 123 | <InspectorControls> |
| 124 | <PanelBody |
| 125 | title={ __( 'Conditions', 'jet-form-builder' ) } |
| 126 | initialOpen |
| 127 | > |
| 128 | <p className={ 'jet-fb flex ai-center gap-05em' }> |
| 129 | <b>{ functionDisplay }</b> |
| 130 | <Button |
| 131 | isTertiary |
| 132 | isSmall |
| 133 | icon={ 'edit' } |
| 134 | onClick={ () => setShowModal( true ) } |
| 135 | /> |
| 136 | </p> |
| 137 | <ConditionalModalContext.Provider value={ { |
| 138 | showModal, setShowModal, |
| 139 | } }> |
| 140 | <ConditionsList attributes={ attributes }/> |
| 141 | </ConditionalModalContext.Provider> |
| 142 | </PanelBody> |
| 143 | { showMultistep && <PanelBody |
| 144 | title={ __( 'Multistep', 'jet-form-builder' ) } |
| 145 | > |
| 146 | <TextControl |
| 147 | label={ __( 'Last Page Name', 'jet-form-builder' ) } |
| 148 | key={ uniqKey( 'last_page_name' ) } |
| 149 | value={ attributes.last_page_name } |
| 150 | help={ __( |
| 151 | 'The value of this field will be set as the name of the last page with the "Progress Bar" block.', |
| 152 | 'jet-form-builder' ) } |
| 153 | onChange={ last_page_name => setAttributes( |
| 154 | { last_page_name }, |
| 155 | ) } |
| 156 | /> |
| 157 | </PanelBody> } |
| 158 | </InspectorControls> |
| 159 | <InspectorControls group={ 'advanced' }> |
| 160 | <div style={ { marginBottom: '1.5em' } }> |
| 161 | <Tip> |
| 162 | <RawHTML> |
| 163 | { __( |
| 164 | `Add the CSS class <code>jet-form-builder--hidden</code> to make |
| 165 | this block hidden by default.`, |
| 166 | 'jet-form-builder', |
| 167 | ) } |
| 168 | </RawHTML> |
| 169 | </Tip> |
| 170 | </div> |
| 171 | <TextControl |
| 172 | label={ __( |
| 173 | 'Additional CSS class(es)', |
| 174 | 'jet-form-builder', |
| 175 | ) } |
| 176 | help={ __( |
| 177 | 'Separate multiple classes with spaces.', |
| 178 | 'jet-form-builder', |
| 179 | ) } |
| 180 | value={ attributes.class_name } |
| 181 | onChange={ class_name => setAttributes( |
| 182 | { class_name }, |
| 183 | ) } |
| 184 | /> |
| 185 | </InspectorControls> |
| 186 | </>; |
| 187 | } |