RepeaterWithState.js
2 years ago
repeater.add.new.js
2 years ago
repeater.add.or.js
2 years ago
repeater.js
2 years ago
repeater.state.js
2 years ago
safe.delete.toggle.js
2 years ago
repeater.js
202 lines
| 1 | import RepeaterHeadContext from '../context/repeater.custom.item.head'; |
| 2 | import RepeaterButtonsContext from '../context/repeater.custom.item.buttons'; |
| 3 | import useRepeaterState from '../../hooks/useRepeaterState'; |
| 4 | import RepeaterItemContext from '../context/repeater.item'; |
| 5 | import RepeaterStateContext from '../context/repeater.state'; |
| 6 | import MacrosButtonTemplate |
| 7 | from '../../macros.button/components/MacrosButtonTemplate'; |
| 8 | import PopoverContext from '../../macros.button/context/PopoverContext'; |
| 9 | |
| 10 | const { |
| 11 | Card, |
| 12 | ButtonGroup, |
| 13 | Button, |
| 14 | CardHeader, |
| 15 | CardBody, |
| 16 | } = wp.components; |
| 17 | const { |
| 18 | useContext, |
| 19 | } = wp.element; |
| 20 | const { |
| 21 | __, |
| 22 | } = wp.i18n; |
| 23 | |
| 24 | /** |
| 25 | * @param props |
| 26 | * @returns {JSX.Element} |
| 27 | * @constructor |
| 28 | */ |
| 29 | function Repeater( props ) { |
| 30 | const { |
| 31 | items, |
| 32 | onSetState, |
| 33 | functions, |
| 34 | children, |
| 35 | } = props; |
| 36 | |
| 37 | const { |
| 38 | cloneItem, |
| 39 | moveUp, |
| 40 | moveDown, |
| 41 | toggleVisible, |
| 42 | changeCurrentItem, |
| 43 | removeOption, |
| 44 | } = functions |
| 45 | ?? useRepeaterState( onSetState ) |
| 46 | ?? useContext( RepeaterStateContext ); |
| 47 | |
| 48 | const { |
| 49 | isSupported: isSupportedHeader, |
| 50 | render: CustomHeader, |
| 51 | } = useContext( RepeaterHeadContext ); |
| 52 | const { |
| 53 | edit: supportEdit, |
| 54 | move: supportMove, |
| 55 | clone: supportClone, |
| 56 | delete: supportDelete, |
| 57 | } = useContext( RepeaterButtonsContext ); |
| 58 | |
| 59 | const RepeaterHeader = ( { currentItem, index } ) => { |
| 60 | if ( isSupportedHeader( currentItem ) ) { |
| 61 | return <CustomHeader |
| 62 | currentItem={ currentItem } |
| 63 | index={ index } |
| 64 | />; |
| 65 | } |
| 66 | |
| 67 | return <span |
| 68 | className={ 'repeater-item-title' } |
| 69 | > |
| 70 | { `#${ index + 1 }` } |
| 71 | </span>; |
| 72 | }; |
| 73 | |
| 74 | return <div |
| 75 | className={ 'jet-form-builder__repeater-component' } |
| 76 | key={ 'jet-form-builder-repeater' } |
| 77 | > |
| 78 | { items.map( ( currentItem, index ) => <Card |
| 79 | size="small" |
| 80 | elevation={ 2 } |
| 81 | className={ 'jet-form-builder__repeater-component-item' } |
| 82 | key={ `jet-form-builder__repeater-component-item-${ index }` } |
| 83 | > |
| 84 | <CardHeader className={ 'repeater__item__header' }> |
| 85 | <div className="repeater-item__left-heading"> |
| 86 | <ButtonGroup className={ 'repeater-action-buttons' }> |
| 87 | { ( |
| 88 | !supportEdit || supportEdit( currentItem ) |
| 89 | ) && <Button |
| 90 | variant="tertiary" |
| 91 | isSmall |
| 92 | icon={ currentItem.__visible ? 'no-alt' : 'edit' } |
| 93 | onClick={ () => toggleVisible( index ) } |
| 94 | className={ 'repeater-action-button jet-fb-is-thick' } |
| 95 | /> } |
| 96 | { ( |
| 97 | !supportMove || supportMove( currentItem ) |
| 98 | ) && <Button |
| 99 | variant="tertiary" |
| 100 | isSmall |
| 101 | isSecondary |
| 102 | disabled={ !Boolean( index ) } |
| 103 | icon={ 'arrow-up-alt2' } |
| 104 | onClick={ () => moveUp( index ) } |
| 105 | className={ 'repeater-action-button jet-fb-is-thick' } |
| 106 | /> } |
| 107 | { ( |
| 108 | !supportMove || supportMove( currentItem ) |
| 109 | ) && <Button |
| 110 | variant="tertiary" |
| 111 | isSmall |
| 112 | isSecondary |
| 113 | disabled={ !( |
| 114 | index < items.length - 1 |
| 115 | ) } |
| 116 | icon={ 'arrow-down-alt2' } |
| 117 | onClick={ () => moveDown( index ) } |
| 118 | className={ 'repeater-action-button jet-fb-is-thick' } |
| 119 | /> } |
| 120 | </ButtonGroup> |
| 121 | <RepeaterHeader |
| 122 | currentItem={ currentItem } |
| 123 | index={ index } |
| 124 | /> |
| 125 | </div> |
| 126 | <ButtonGroup> |
| 127 | { ( |
| 128 | !supportClone || supportClone( currentItem ) |
| 129 | ) && <Button |
| 130 | variant="tertiary" |
| 131 | isSmall |
| 132 | isSecondary |
| 133 | onClick={ () => cloneItem( index ) } |
| 134 | className={ 'jet-fb-is-thick' } |
| 135 | icon={ 'admin-page' } |
| 136 | /> } |
| 137 | { ( |
| 138 | !supportDelete || supportDelete( currentItem ) |
| 139 | ) && <MacrosButtonTemplate |
| 140 | icon={ 'trash' } |
| 141 | isDestructive |
| 142 | > |
| 143 | <PopoverContext.Consumer> |
| 144 | { ( { setShowPopover } ) => <div |
| 145 | style={ { |
| 146 | padding: '0.5em', |
| 147 | width: 'max-content', |
| 148 | } } |
| 149 | > |
| 150 | <span>{ __( |
| 151 | 'Delete this item?', |
| 152 | 'jet-form-builder', |
| 153 | ) }</span> |
| 154 | |
| 155 | <Button |
| 156 | isLink |
| 157 | isDestructive |
| 158 | onClick={ () => removeOption( index ) } |
| 159 | > |
| 160 | { __( 'Yes', 'jet-form-builder' ) } |
| 161 | </Button> |
| 162 | { ' / ' } |
| 163 | <Button |
| 164 | isLink |
| 165 | onClick={ () => setShowPopover( false ) } |
| 166 | > |
| 167 | { __( 'No', 'jet-form-builder' ) } |
| 168 | </Button> |
| 169 | </div> } |
| 170 | </PopoverContext.Consumer> |
| 171 | </MacrosButtonTemplate> } |
| 172 | </ButtonGroup> |
| 173 | </CardHeader> |
| 174 | { currentItem.__visible && <CardBody |
| 175 | className={ 'repeater-item__content' } |
| 176 | key={ `jet-form-builder__card-body-${ index }` } |
| 177 | > |
| 178 | { ( |
| 179 | () => { |
| 180 | const context = { |
| 181 | currentItem, |
| 182 | changeCurrentItem: data => changeCurrentItem( |
| 183 | data, index ), |
| 184 | currentIndex: index, |
| 185 | }; |
| 186 | |
| 187 | return <RepeaterItemContext.Provider value={ context }> |
| 188 | { !children && |
| 189 | 'Set up your Repeater Template, please.' } |
| 190 | { 'function' === typeof children |
| 191 | ? children( context ) |
| 192 | : children |
| 193 | } |
| 194 | </RepeaterItemContext.Provider>; |
| 195 | } |
| 196 | )() } |
| 197 | </CardBody> } |
| 198 | </Card> ) } |
| 199 | </div>; |
| 200 | } |
| 201 | |
| 202 | export default Repeater; |