AdvancedRuleItem.js
2 years ago
AdvancedRuleModalItem.js
2 years ago
AdvancedRulesIsEmpty.js
2 years ago
AdvancedRulesList.js
2 years ago
AdvancedRulesModal.js
2 years ago
ChooseRelatedField.js
2 years ago
EditAdvancedRulesButton.js
2 years ago
EditRulesModalContext.js
2 years ago
HumanReadableRule.js
2 years ago
ValidationBlockMessage.js
2 years ago
ValidationMetaMessage.js
2 years ago
ValidationToggleGroup.js
2 years ago
AdvancedRuleModalItem.js
188 lines
| 1 | import Tools from '../../tools'; |
| 2 | import RepeaterItemContext from '../../repeater/context/repeater.item'; |
| 3 | import BaseHelp from '../../components/BaseHelp'; |
| 4 | import AdvancedModalControl from '../../components/AdvancedModalControl'; |
| 5 | import ChooseRelatedField from './ChooseRelatedField'; |
| 6 | |
| 7 | const { |
| 8 | SelectControl, |
| 9 | TextareaControl, |
| 10 | TextControl, |
| 11 | withFilters, |
| 12 | } = wp.components; |
| 13 | const { |
| 14 | useContext, |
| 15 | useState, |
| 16 | useEffect, |
| 17 | } = wp.element; |
| 18 | const { |
| 19 | __, |
| 20 | } = wp.i18n; |
| 21 | const { |
| 22 | addFilter, |
| 23 | } = wp.hooks; |
| 24 | const { |
| 25 | rule_types, |
| 26 | ssr_callbacks, |
| 27 | } = window.jetFormValidation; |
| 28 | |
| 29 | const ssr_callbacks_keys = ssr_callbacks.map( ( { value } ) => value ); |
| 30 | |
| 31 | function getLabel( type ) { |
| 32 | const indexRule = rule_types.findIndex( ( { value } ) => value === type ); |
| 33 | const fallback = __( 'Enter value', 'jet-form-builder' ); |
| 34 | |
| 35 | if ( -1 === indexRule ) { |
| 36 | return fallback; |
| 37 | } |
| 38 | |
| 39 | return rule_types[ indexRule ]?.control_label ?? fallback; |
| 40 | } |
| 41 | |
| 42 | function RuleSpecificControls( { |
| 43 | currentItem, |
| 44 | changeCurrentItem, |
| 45 | } ) { |
| 46 | const [ label, setLabel ] = useState( () => getLabel( currentItem.type ) ); |
| 47 | |
| 48 | useEffect( () => { |
| 49 | setLabel( getLabel( currentItem.type ) ); |
| 50 | }, [ currentItem.type ] ); |
| 51 | |
| 52 | switch ( currentItem.type ) { |
| 53 | case 'equal': |
| 54 | case 'contain': |
| 55 | case 'contain_not': |
| 56 | case 'regexp': |
| 57 | case 'regexp_not': |
| 58 | return <> |
| 59 | <ChooseRelatedField/> |
| 60 | { !Boolean( currentItem.field ) && <AdvancedModalControl |
| 61 | value={ currentItem.value } |
| 62 | label={ label } |
| 63 | onChangePreset={ value => changeCurrentItem( { value } ) } |
| 64 | onChangeMacros={ name => changeCurrentItem( { |
| 65 | value: ( |
| 66 | currentItem.value ?? '' |
| 67 | ) + name, |
| 68 | } ) } |
| 69 | > |
| 70 | { ( { instanceId } ) => <TextareaControl |
| 71 | id={ instanceId } |
| 72 | value={ currentItem.value } |
| 73 | onChange={ value => changeCurrentItem( { value } ) } |
| 74 | /> } |
| 75 | </AdvancedModalControl> } |
| 76 | </>; |
| 77 | default: |
| 78 | return null; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | function withPref( name ) { |
| 83 | if ( ! name?.length || 'jet_fb_v_' === name ) { |
| 84 | return ''; |
| 85 | } |
| 86 | return 'jet_fb_v_' + ( |
| 87 | name.replace( /^jet_fb_v_/, '' ) |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | addFilter( |
| 92 | 'jet.fb.advanced.rule.controls', |
| 93 | 'jet-form-builder', |
| 94 | ( DefaultControls ) => ( props ) => { |
| 95 | const { currentItem, changeCurrentItem } = props; |
| 96 | const [ showDetails, setShowDetails ] = useState( false ); |
| 97 | |
| 98 | if ( 'ssr' !== currentItem.type ) { |
| 99 | return <DefaultControls { ...props } />; |
| 100 | } |
| 101 | |
| 102 | const functionName = currentItem.value || 'custom_jfb_field_validation'; |
| 103 | |
| 104 | return <> |
| 105 | <SelectControl |
| 106 | labelPosition="side" |
| 107 | options={ Tools.withPlaceholder( |
| 108 | ssr_callbacks, |
| 109 | __( 'Custom function', 'jet-form-builder' ), |
| 110 | ) } |
| 111 | label={ __( 'Choose callback', 'jet-form-builder' ) } |
| 112 | value={ currentItem.value } |
| 113 | onChange={ value => changeCurrentItem( { value } ) } |
| 114 | /> |
| 115 | { !ssr_callbacks_keys.includes( currentItem.value ) && <> |
| 116 | <TextControl |
| 117 | label={ __( 'Function name', 'jet-form-builder' ) } |
| 118 | value={ currentItem.value } |
| 119 | onChange={ value => changeCurrentItem( { |
| 120 | value: withPref( value ) |
| 121 | } ) } |
| 122 | /> |
| 123 | <BaseHelp> |
| 124 | { __( 'Example of registering a function below.' ) + ' ' } |
| 125 | <a |
| 126 | href="javascript:void(0)" |
| 127 | onClick={ () => setShowDetails( prev => !prev ) } |
| 128 | > |
| 129 | { showDetails |
| 130 | ? __( 'Hide', 'jet-form-builder' ) |
| 131 | : __( 'Show', 'jet-form-builder' ) } |
| 132 | </a> |
| 133 | </BaseHelp> |
| 134 | { showDetails && <pre> |
| 135 | { `/** |
| 136 | * To get all the values of the fields in the form, you can use the expression: |
| 137 | * jet_fb_request_handler()->get_request() or $context->get_request() |
| 138 | * |
| 139 | * If the field is located in the middle of the repeater, then only |
| 140 | * jet_fb_request_handler()->get_request(), but $context->get_request() |
| 141 | * will return the values of all fields of the current repeater element |
| 142 | * |
| 143 | * @param $value mixed |
| 144 | * @param $context \\Jet_Form_Builder\\Request\\Parser_Context |
| 145 | * |
| 146 | * @return bool |
| 147 | */ |
| 148 | function ${ functionName }( $value, $context ): bool { |
| 149 | // your logic |
| 150 | return true; |
| 151 | }` } |
| 152 | </pre> } |
| 153 | </> } |
| 154 | </>; |
| 155 | }, |
| 156 | ); |
| 157 | |
| 158 | const RuleControls = withFilters( |
| 159 | 'jet.fb.advanced.rule.controls', |
| 160 | )( RuleSpecificControls ); |
| 161 | |
| 162 | function AdvancedRuleModalItem() { |
| 163 | const { |
| 164 | currentItem, |
| 165 | changeCurrentItem, |
| 166 | } = useContext( RepeaterItemContext ); |
| 167 | |
| 168 | return <> |
| 169 | <SelectControl |
| 170 | labelPosition="side" |
| 171 | options={ Tools.withPlaceholder( rule_types ) } |
| 172 | label={ __( 'Rule type', 'jet-form-builder' ) } |
| 173 | value={ currentItem.type } |
| 174 | onChange={ type => changeCurrentItem( { type } ) } |
| 175 | /> |
| 176 | <RuleControls |
| 177 | currentItem={ currentItem } |
| 178 | changeCurrentItem={ changeCurrentItem } |
| 179 | /> |
| 180 | <TextareaControl |
| 181 | label={ __( 'Error message', 'jet-form-builder' ) } |
| 182 | value={ currentItem.message } |
| 183 | onChange={ message => changeCurrentItem( { message } ) } |
| 184 | /> |
| 185 | </>; |
| 186 | } |
| 187 | |
| 188 | export default AdvancedRuleModalItem; |