useIsHasAttribute.js
2 years ago
useMetaState.js
2 years ago
useRepeaterState.js
2 years ago
useSelectPostMeta.js
2 years ago
useStateLoadingClasses.js
2 years ago
useStateValidClasses.js
2 years ago
useSuccessNotice.js
2 years ago
withSelectFormFields.js
2 years ago
useRepeaterState.js
122 lines
| 1 | import SafeDeleteContext from '../repeater/context/safe.delete'; |
| 2 | |
| 3 | const { |
| 4 | useContext, |
| 5 | } = wp.element; |
| 6 | |
| 7 | const { __ } = wp.i18n; |
| 8 | |
| 9 | const onSaveDeleting = index => { |
| 10 | return confirm( __( `Are you sure you want to remove item ${ index + 1 }?`, |
| 11 | 'jet-form-builder' ) ); |
| 12 | }; |
| 13 | |
| 14 | /** |
| 15 | * |
| 16 | * @param setItemsData |
| 17 | * @returns {{ |
| 18 | * cloneItem: function, |
| 19 | * addNewItem: function, |
| 20 | * changeCurrentItem: function, |
| 21 | * toggleVisible: function, |
| 22 | * moveDown: function, |
| 23 | * moveUp: function, |
| 24 | * removeOption: function |
| 25 | * }|null} |
| 26 | */ |
| 27 | function useRepeaterState( setItemsData ) { |
| 28 | if ( 'undefined' === typeof setItemsData ) { |
| 29 | return null; |
| 30 | } |
| 31 | |
| 32 | const isSafeDeleting = useContext( SafeDeleteContext ); |
| 33 | |
| 34 | const changeCurrentItem = function ( valueToSet, index ) { |
| 35 | setItemsData( prev => { |
| 36 | const prevClone = JSON.parse( JSON.stringify( prev ) ); |
| 37 | |
| 38 | prevClone[ index ] = { |
| 39 | ...prev[ index ], |
| 40 | ...valueToSet, |
| 41 | }; |
| 42 | return prevClone; |
| 43 | } ); |
| 44 | }; |
| 45 | |
| 46 | const removeOption = function ( index ) { |
| 47 | if ( isSafeDeleting && !onSaveDeleting( index ) ) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | setItemsData( prev => { |
| 52 | const prevClone = JSON.parse( JSON.stringify( prev ) ); |
| 53 | prevClone.splice( index, 1 ); |
| 54 | |
| 55 | return prevClone; |
| 56 | } ); |
| 57 | }; |
| 58 | |
| 59 | const addNewItem = function ( value ) { |
| 60 | setItemsData( prev => [ |
| 61 | ...prev, { |
| 62 | __visible: true, |
| 63 | ...value, |
| 64 | }, |
| 65 | ] ); |
| 66 | }; |
| 67 | |
| 68 | const cloneItem = function ( index ) { |
| 69 | setItemsData( prev => { |
| 70 | const prevClone = JSON.parse( JSON.stringify( prev ) ); |
| 71 | const [ before, after ] = [ |
| 72 | prevClone.slice( 0, index + 1 ), |
| 73 | prevClone.slice( index + 1 ), |
| 74 | ]; |
| 75 | |
| 76 | return [ ...before, prevClone[ index ], ...after ]; |
| 77 | } ); |
| 78 | }; |
| 79 | |
| 80 | const moveRepeaterItem = function ( { oldIndex, newIndex } ) { |
| 81 | setItemsData( prev => { |
| 82 | const prevClone = JSON.parse( JSON.stringify( prev ) ); |
| 83 | |
| 84 | [ |
| 85 | prevClone[ newIndex ], |
| 86 | prevClone[ oldIndex ], |
| 87 | ] = [ prevClone[ oldIndex ], prevClone[ newIndex ] ]; |
| 88 | |
| 89 | return prevClone; |
| 90 | } ); |
| 91 | }; |
| 92 | |
| 93 | const moveUp = function ( index ) { |
| 94 | moveRepeaterItem( { oldIndex: index, newIndex: index - 1 } ); |
| 95 | }; |
| 96 | const moveDown = function ( index ) { |
| 97 | moveRepeaterItem( { oldIndex: index, newIndex: index + 1 } ); |
| 98 | }; |
| 99 | |
| 100 | const toggleVisible = function ( index ) { |
| 101 | setItemsData( prev => { |
| 102 | const prevClone = JSON.parse( JSON.stringify( prev ) ); |
| 103 | prevClone[ index ].__visible = !( |
| 104 | prevClone[ index ].__visible |
| 105 | ); |
| 106 | |
| 107 | return prevClone; |
| 108 | } ); |
| 109 | }; |
| 110 | |
| 111 | return { |
| 112 | changeCurrentItem, |
| 113 | toggleVisible, |
| 114 | moveDown, |
| 115 | moveUp, |
| 116 | cloneItem, |
| 117 | addNewItem, |
| 118 | removeOption, |
| 119 | }; |
| 120 | } |
| 121 | |
| 122 | export default useRepeaterState; |