jetformbuilder
/
assets
/
src
/
editor
/
components
/
base-select-check-radio
/
from-manual-fields.js
custom-template.js
3 years ago
from-generators-fields.js
3 years ago
from-manual-fields.js
3 years ago
from-post-terms-fields.js
3 years ago
select-radio-check-placeholder.js
3 years ago
select-radio-check.js
3 years ago
sources.js
3 years ago
from-manual-fields.js
121 lines
| 1 | const { |
| 2 | RepeaterWithState, |
| 3 | ActionModal, |
| 4 | } = JetFBComponents; |
| 5 | |
| 6 | const { |
| 7 | Button, |
| 8 | TextControl, |
| 9 | } = wp.components; |
| 10 | |
| 11 | const { __ } = wp.i18n; |
| 12 | const { useState } = wp.element; |
| 13 | |
| 14 | const addNewOption = { |
| 15 | label: '', |
| 16 | value: '', |
| 17 | calculate: '' |
| 18 | }; |
| 19 | |
| 20 | function FromManualFields( props ) { |
| 21 | |
| 22 | const [ showManualModal, setShowModal ] = useState( false ); |
| 23 | |
| 24 | const { |
| 25 | setAttributes, |
| 26 | attributes |
| 27 | } = props; |
| 28 | |
| 29 | |
| 30 | const toggleModal = () => { |
| 31 | setShowModal( toggle => ! toggle ); |
| 32 | } |
| 33 | |
| 34 | const onUpdateOptions = items => { |
| 35 | /* Remove empty options */ |
| 36 | const field_options = items.filter( option => { |
| 37 | return ( Boolean( option.value ) || Boolean( option.calculate ) ); |
| 38 | } ); |
| 39 | |
| 40 | setAttributes( { field_options } ); |
| 41 | } |
| 42 | |
| 43 | const itemHeading = ( { currentItem, index } ) => { |
| 44 | const leftPart = [ `#${ index + 1 }` ]; |
| 45 | const rightPart = []; |
| 46 | |
| 47 | if ( currentItem.label ) { |
| 48 | rightPart.push( `Label [${ currentItem.label }]` ); |
| 49 | } |
| 50 | if ( currentItem.value ) { |
| 51 | rightPart.push( `Value [${ currentItem.value }]` ); |
| 52 | } |
| 53 | if ( currentItem.calculate ) { |
| 54 | rightPart.push( `Calculate [${ currentItem.calculate }]` ); |
| 55 | } |
| 56 | leftPart.push( rightPart.join( ' | ' ) ) |
| 57 | |
| 58 | return leftPart.join( ' ' ); |
| 59 | }; |
| 60 | |
| 61 | return <React.Fragment key='jet-form/manage-manual-items'> |
| 62 | <Button |
| 63 | isSecondary |
| 64 | onClick={ toggleModal } |
| 65 | icon={ 'admin-tools' } |
| 66 | style={ { |
| 67 | marginBottom: '15px' |
| 68 | } } |
| 69 | > |
| 70 | { __( 'Manage Items' ) } |
| 71 | </Button> |
| 72 | { showManualModal && <ActionModal |
| 73 | title={ 'Edit Manual Options' } |
| 74 | onRequestClose={ toggleModal } |
| 75 | classNames={ [ 'width-60' ] } |
| 76 | > |
| 77 | { ( { actionClick, onRequestClose } ) => { |
| 78 | return <RepeaterWithState |
| 79 | items={ attributes.field_options } |
| 80 | onSaveItems={ onUpdateOptions } |
| 81 | newItem={ addNewOption } |
| 82 | onUnMount={ onRequestClose } |
| 83 | isSaveAction={ actionClick } |
| 84 | addNewButtonLabel={ __( 'Add New Option' ) } |
| 85 | ItemHeading={ itemHeading } |
| 86 | > |
| 87 | { ( { currentItem, changeCurrentItem } ) => { |
| 88 | return <> |
| 89 | <TextControl |
| 90 | key='manual_label' |
| 91 | label={ __( 'Label' ) } |
| 92 | value={ currentItem.label } |
| 93 | onChange={ newValue => { |
| 94 | changeCurrentItem( { label: newValue } ); |
| 95 | } } |
| 96 | /> |
| 97 | <TextControl |
| 98 | key='manual_value' |
| 99 | label={ __( 'Value' ) } |
| 100 | value={ currentItem.value } |
| 101 | onChange={ ( newValue ) => { |
| 102 | changeCurrentItem( { value: newValue } ); |
| 103 | } } |
| 104 | /> |
| 105 | <TextControl |
| 106 | key='manual_calculate' |
| 107 | label={ __( 'Calculate' ) } |
| 108 | value={ currentItem.calculate } |
| 109 | onChange={ ( newValue ) => { |
| 110 | changeCurrentItem( { calculate: newValue } ); |
| 111 | } } |
| 112 | /> |
| 113 | </>; |
| 114 | } } |
| 115 | </RepeaterWithState> |
| 116 | } } |
| 117 | </ActionModal> } |
| 118 | </React.Fragment>; |
| 119 | } |
| 120 | |
| 121 | export default FromManualFields; |