PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.1.6
JetFormBuilder — Dynamic Blocks Form Builder v3.1.6
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / assets / src / package / hooks / useRepeaterState.js
jetformbuilder / assets / src / package / hooks Last commit date
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;