@flowerforce/flower-react 3.1.1 → 3.1.2-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs.js CHANGED
@@ -9,43 +9,40 @@ var reselect = require('reselect');
9
9
  var reactRedux = require('react-redux');
10
10
  var debounce = require('lodash/debounce');
11
11
 
12
- const _context = /*#__PURE__*/React.createContext({});
12
+ const _context = React.createContext({});
13
13
  const context = _context;
14
14
  const Provider = _context.Provider;
15
15
  const Consumer = _context.Consumer;
16
16
 
17
- const convertElements = nodes => {
18
- const res = flowerCore.CoreUtils.generateNodesForFlowerJson(nodes);
19
- return res;
17
+ // eslint-disable-next-line import/prefer-default-export
18
+ const convertElements = (nodes) => {
19
+ const res = flowerCore.CoreUtils.generateNodesForFlowerJson(nodes);
20
+ return res;
20
21
  };
21
22
 
22
23
  const flowerReducer = toolkit.createSlice({
23
- name: 'flower',
24
- initialState: {},
25
- reducers: flowerCore.FlowerCoreReducers
24
+ name: 'flower',
25
+ initialState: {},
26
+ reducers: flowerCore.FlowerCoreReducers
26
27
  });
27
- const {
28
- actions
29
- } = flowerReducer;
28
+ const { actions } = flowerReducer;
30
29
  const reducerFlower = {
31
- flower: flowerReducer.reducer
30
+ flower: flowerReducer.reducer
32
31
  };
33
32
 
34
- const {
35
- getAllData: mapData
36
- } = flowerCore.FlowerStateUtils;
37
- const {
38
- selectGlobal
39
- } = flowerCore.Selectors;
40
- const selectFlower = name => reselect.createSelector(selectGlobal, flowerCore.Selectors.selectFlower(name));
33
+ const { getAllData: mapData } = flowerCore.FlowerStateUtils;
34
+ const { selectGlobal } = flowerCore.Selectors;
35
+ const selectFlower = (name) => reselect.createSelector(selectGlobal, flowerCore.Selectors.selectFlower(name));
41
36
  const selectFlowerFormNode = (name, id) => reselect.createSelector(selectFlower(name), flowerCore.Selectors.selectFlowerFormNode(id));
42
- const selectFlowerHistory = name => reselect.createSelector(selectFlower(name), flowerCore.Selectors.selectFlowerHistory);
43
- const makeSelectNodesIds = name => reselect.createSelector(selectFlower(name), flowerCore.Selectors.makeSelectNodesIds);
44
- const makeSelectStartNodeId = name => reselect.createSelector(selectFlower(name), flowerCore.Selectors.makeSelectStartNodeId);
45
- const makeSelectCurrentNodeId = name => reselect.createSelector(selectFlower(name), makeSelectStartNodeId(name), flowerCore.Selectors.makeSelectCurrentNodeId);
46
- const makeSelectPrevNodeRetain = name => reselect.createSelector(makeSelectNodesIds(name), selectFlowerHistory(name), makeSelectCurrentNodeId(name), flowerCore.Selectors.makeSelectPrevNodeRetain);
47
- const makeSelectCurrentNodeDisabled = name => reselect.createSelector(makeSelectNodesIds(name), makeSelectCurrentNodeId(name), flowerCore.Selectors.makeSelectCurrentNodeDisabled);
48
- const getDataByFlow = name => reselect.createSelector(selectFlower(name), flowerCore.Selectors.getDataByFlow);
37
+ const selectFlowerHistory = (name) => reselect.createSelector(selectFlower(name), flowerCore.Selectors.selectFlowerHistory);
38
+ const makeSelectNodesIds = (name) => reselect.createSelector(selectFlower(name), flowerCore.Selectors.makeSelectNodesIds);
39
+ const makeSelectStartNodeId = (name) => reselect.createSelector(selectFlower(name), flowerCore.Selectors.makeSelectStartNodeId);
40
+ const makeSelectCurrentNodeId = (name) => reselect.createSelector(selectFlower(name), makeSelectStartNodeId(name), flowerCore.Selectors.makeSelectCurrentNodeId);
41
+ const makeSelectPrevNodeRetain = (name) => reselect.createSelector(makeSelectNodesIds(name), selectFlowerHistory(name), makeSelectCurrentNodeId(name), flowerCore.Selectors.makeSelectPrevNodeRetain);
42
+ const makeSelectCurrentNodeDisabled = (name) => reselect.createSelector(makeSelectNodesIds(name), makeSelectCurrentNodeId(name), flowerCore.Selectors.makeSelectCurrentNodeDisabled);
43
+ // dati nel flow selezionato
44
+ const getDataByFlow = (name) => reselect.createSelector(selectFlower(name), flowerCore.Selectors.getDataByFlow);
45
+ // selettore per recuperare i dati di un flow specifico e id specifico
49
46
  const getDataFromState = (name, id) => reselect.createSelector(getDataByFlow(name), flowerCore.Selectors.getDataFromState(id));
50
47
  const makeSelectNodeErrors = (name, currentNodeId) => reselect.createSelector(selectFlowerFormNode(name, currentNodeId), flowerCore.Selectors.makeSelectNodeErrors);
51
48
  const makeSelectNodeFormTouched = (name, currentNodeId) => reselect.createSelector(selectFlowerFormNode(name, currentNodeId), flowerCore.Selectors.makeSelectNodeFormTouched);
@@ -53,1101 +50,829 @@ const getAllData = reselect.createSelector(selectGlobal, mapData);
53
50
  const makeSelectFieldError = (name, id, validate) => reselect.createSelector(getAllData, flowerCore.Selectors.makeSelectFieldError(name, id, validate));
54
51
  const selectorRulesDisabled = (id, rules, keys, flowName, value, currentNode) => reselect.createSelector(getAllData, makeSelectNodeErrors(flowName, currentNode), flowerCore.Selectors.selectorRulesDisabled(id, rules, keys, flowName, value));
55
52
 
56
- const reduxContext = /*#__PURE__*/React.createContext(null);
53
+ //TODO check reduxContext type due to remove all any types
54
+ const reduxContext = React.createContext(null);
57
55
  const useDispatch = reactRedux.createDispatchHook(reduxContext);
58
56
  const useSelector = reactRedux.createSelectorHook(reduxContext);
59
57
  const useStore = reactRedux.createStoreHook(reduxContext);
60
- const store = ({
61
- enableDevtool
62
- }) => toolkit.configureStore({
63
- reducer: reducerFlower,
64
- devTools: enableDevtool ? {
65
- name: 'flower'
66
- } : false
58
+ const store = ({ enableDevtool }) => toolkit.configureStore({
59
+ reducer: reducerFlower,
60
+ devTools: enableDevtool ? { name: 'flower' } : false
67
61
  });
68
62
  class FlowerProvider extends React.PureComponent {
69
- constructor(props) {
70
- super(props);
71
- this.store = store({
72
- enableDevtool: props.enableReduxDevtool
73
- });
74
- }
75
- render() {
76
- const {
77
- children
78
- } = this.props;
79
- return /*#__PURE__*/React.createElement(reactRedux.Provider, {
80
- context: reduxContext,
81
- store: this.store
82
- }, children);
83
- }
63
+ constructor(props) {
64
+ super(props);
65
+ this.store = store({ enableDevtool: props.enableReduxDevtool });
66
+ }
67
+ render() {
68
+ const { children } = this.props;
69
+ return (React.createElement(reactRedux.Provider, { context: reduxContext, store: this.store }, children));
70
+ }
84
71
  }
85
72
 
86
- const FlowerClient = ({
87
- children,
88
- name,
89
- destroyOnUnmount: _destroyOnUnmount = true,
90
- startId: _startId = null,
91
- initialData: _initialData = {}
92
- }) => {
93
- const flowName = name;
94
- const dispatch = useDispatch();
95
- const one = React.useRef(false);
96
- const [wsDevtools, setWsDevtools] = React.useState(global.window && _get(global.window, '__FLOWER_DEVTOOLS_INITIALIZED__', false));
97
- const nodes = React.useMemo(() => convertElements(React.Children.toArray(children)), [children]);
98
- const nodeById = React.useMemo(() => _keyBy(React.Children.toArray(children), 'props.id'), [children]);
99
- const isInitialized = useSelector(makeSelectStartNodeId(name));
100
- const history = useSelector(selectFlowerHistory(name));
101
- const current = useSelector(makeSelectCurrentNodeId(flowName));
102
- const isDisabled = useSelector(makeSelectCurrentNodeDisabled(flowName));
103
- const prevFlowerNodeId = useSelector(makeSelectPrevNodeRetain(flowName));
104
- const store = useStore();
105
- React.useEffect(() => {
106
- if (nodes.length > 0 && one.current === false) {
107
- one.current = true;
108
- dispatch(actions.initNodes({
109
- name: flowName,
73
+ /* eslint-disable no-undef */
74
+ /* eslint-disable no-underscore-dangle */
75
+ /**
76
+ * FlowerClient
77
+ */
78
+ const FlowerClient = ({ children, name, destroyOnUnmount = true, startId = null, initialData = {}, initialState = {} }) => {
79
+ const flowName = name;
80
+ const dispatch = useDispatch();
81
+ const one = React.useRef(false);
82
+ const [wsDevtools, setWsDevtools] = React.useState(flowerCore.devtoolState && _get(flowerCore.devtoolState, '__FLOWER_DEVTOOLS_INITIALIZED__', false));
83
+ // TODO rivedere il giro, potremmo fare le trasformazioni in CoreUtils.generateNodesForFlowerJson
84
+ // eslint-disable-next-line react-hooks/exhaustive-deps, max-len
85
+ const nodes = React.useMemo(() => convertElements(React.Children.toArray(children)), [children]);
86
+ const nodeById = React.useMemo(() => _keyBy(React.Children.toArray(children), 'props.id'), [children]);
87
+ const isInitialized = useSelector(makeSelectStartNodeId(name));
88
+ const history = useSelector(selectFlowerHistory(name));
89
+ const current = useSelector(makeSelectCurrentNodeId(flowName));
90
+ const isDisabled = useSelector(makeSelectCurrentNodeDisabled(flowName));
91
+ const prevFlowerNodeId = useSelector(makeSelectPrevNodeRetain(flowName));
92
+ const store = useStore();
93
+ React.useEffect(() => {
94
+ if (nodes.length > 0 && one.current === false) {
95
+ one.current = true;
96
+ dispatch(actions.initNodes({
97
+ name: flowName,
98
+ // @ts-expect-error FIX ME
99
+ nodes,
100
+ startId: startId ?? '',
101
+ persist: destroyOnUnmount === false,
102
+ initialData,
103
+ initialState
104
+ }));
105
+ }
106
+ }, [
107
+ dispatch,
108
+ flowName,
110
109
  nodes,
111
- startId: _startId != null ? _startId : '',
112
- persist: _destroyOnUnmount === false,
113
- initialData: _initialData
114
- }));
115
- }
116
- }, [dispatch, flowName, nodes, _startId, _initialData, _destroyOnUnmount]);
117
- React.useEffect(() => {
118
- const eventCb = msg => {
119
- if (msg.source !== 'flower-devtool') return;
120
- if (msg.action === 'FLOWER_EXTENSION_INIT' || msg.action === 'FLOWER_DEVTOOL_WEB_INIT') {
121
- setWsDevtools(true);
122
- }
123
- if (msg.action === 'SELECTED_NODE' && msg.name === flowName) {
124
- dispatch(actions.setCurrentNode({
125
- name: msg.name,
126
- node: msg.id
127
- }));
128
- }
129
- if (msg.action === 'REPLACE_DATA' && msg.name === flowName) {
130
- dispatch(actions.replaceData({
131
- flowName: msg.name,
132
- value: msg.data
133
- }));
134
- }
135
- if (msg.action === 'ADD_DATA' && msg.name === flowName) {
136
- dispatch(actions.addData({
137
- flowName: msg.name,
138
- value: msg.data
139
- }));
140
- }
141
- };
142
- if (global.window && _get(global.window, '__FLOWER_DEVTOOLS__')) {
143
- flowerCore.Emitter.on('flower-devtool-to-client', eventCb);
144
- }
145
- return () => {
146
- if (global.window && _get(global.window, '__FLOWER_DEVTOOLS__')) {
147
- flowerCore.Emitter.off('flower-devtool-to-client', eventCb);
148
- }
149
- };
150
- }, [dispatch, flowName]);
151
- React.useEffect(() => () => {
152
- if (_destroyOnUnmount && one.current === true) {
153
- one.current = false;
154
- dispatch(actions.destroy({
155
- name: flowName
156
- }));
157
- }
158
- }, [dispatch, flowName, _destroyOnUnmount]);
159
- React.useEffect(() => {
160
- if (isInitialized && wsDevtools && global.window && _get(global.window, '__FLOWER_DEVTOOLS__')) {
161
- flowerCore.Emitter.emit('flower-devtool-from-client', {
162
- source: 'flower-client',
163
- action: 'FLOWER_CLIENT_INIT',
164
- name: flowName,
165
- time: new Date(),
166
- nodeId: isInitialized,
167
- getState: store.getState
168
- });
169
- }
170
- }, [dispatch, flowName, wsDevtools, isInitialized, store]);
171
- React.useEffect(() => {
172
- if (isInitialized && wsDevtools && global.window && _get(global.window, '__FLOWER_DEVTOOLS__')) {
173
- flowerCore.Emitter.emit('flower-devtool-from-client', {
174
- source: 'flower-client',
175
- action: 'SET_HISTORY',
176
- name: flowName,
177
- history
178
- });
179
- }
180
- }, [dispatch, flowName, history, wsDevtools, isInitialized]);
181
- React.useEffect(() => {
182
- if (!current) return;
183
- if (!isInitialized) return;
184
- if (isInitialized && wsDevtools && global.window && _get(global.window, '__FLOWER_DEVTOOLS__')) {
185
- flowerCore.Emitter.emit('flower-devtool-from-client', {
186
- source: 'flower-client',
187
- action: 'SET_CURRENT',
188
- name: flowName,
189
- current
190
- });
191
- }
192
- }, [flowName, current, wsDevtools, isInitialized]);
193
- React.useEffect(() => {
194
- if (!current) return;
195
- if (!isInitialized) return;
196
- if (isDisabled) {
197
- dispatch({
198
- type: 'flower/next',
199
- payload: {
200
- flowName,
201
- disabled: true
110
+ startId,
111
+ initialData,
112
+ destroyOnUnmount,
113
+ initialState
114
+ ]);
115
+ React.useEffect(() => {
116
+ /* istanbul ignore next */
117
+ const eventCb = (msg) => {
118
+ if (msg.source !== 'flower-devtool')
119
+ return;
120
+ if (msg.action === 'FLOWER_EXTENSION_INIT' ||
121
+ msg.action === 'FLOWER_DEVTOOL_WEB_INIT') {
122
+ setWsDevtools(true);
123
+ }
124
+ if (msg.action === 'SELECTED_NODE' && msg.name === flowName) {
125
+ dispatch(actions.setCurrentNode({ name: msg.name, node: msg.id }));
126
+ }
127
+ if (msg.action === 'REPLACE_DATA' && msg.name === flowName) {
128
+ dispatch(actions.replaceData({ flowName: msg.name, value: msg.data }));
129
+ }
130
+ if (msg.action === 'ADD_DATA' && msg.name === flowName) {
131
+ dispatch(actions.addData({ flowName: msg.name, value: msg.data }));
132
+ }
133
+ };
134
+ /* istanbul ignore next */
135
+ if (flowerCore.devtoolState && _get(flowerCore.devtoolState, '__FLOWER_DEVTOOLS__')) {
136
+ flowerCore.Emitter.on('flower-devtool-to-client', eventCb);
202
137
  }
203
- });
204
- if (wsDevtools && global.window && _get(global.window, '__FLOWER_DEVTOOLS__')) {
205
- flowerCore.Emitter.emit('flower-devtool-from-client', {
206
- source: 'flower-client',
207
- action: 'FLOWER_NAVIGATE',
208
- nodeId: current,
209
- name: flowName,
210
- time: new Date(),
211
- params: {
212
- action: 'next',
213
- payload: {
214
- flowName,
215
- disabled: true
138
+ return () => {
139
+ /* istanbul ignore next */
140
+ if (flowerCore.devtoolState && _get(flowerCore.devtoolState, '__FLOWER_DEVTOOLS__')) {
141
+ flowerCore.Emitter.off('flower-devtool-to-client', eventCb);
216
142
  }
217
- }
218
- });
219
- }
220
- return;
221
- }
222
- if (wsDevtools && global.window && _get(global.window, '__FLOWER_DEVTOOLS__')) {
223
- if (isInitialized === current) return;
224
- flowerCore.Emitter.emit('flower-devtool-from-client', {
225
- source: 'flower-client',
226
- action: 'SET_SELECTED',
227
- nodeId: current,
228
- name: flowName,
229
- time: new Date()
230
- });
231
- }
232
- }, [dispatch, flowName, current, isDisabled, store, wsDevtools, isInitialized]);
233
- const contextValues = React.useMemo(() => ({
234
- flowName,
235
- initialData: _initialData,
236
- currentNode: current
237
- }), [flowName, _initialData, current]);
238
- return isInitialized ? ( /*#__PURE__*/React.createElement(Provider, {
239
- value: contextValues
240
- }, prevFlowerNodeId !== current && typeof prevFlowerNodeId === 'string' && nodeById[prevFlowerNodeId], !isDisabled && nodeById[current])) : null;
143
+ };
144
+ }, [dispatch, flowName]);
145
+ React.useEffect(() => () => {
146
+ // unmount function
147
+ if (destroyOnUnmount && one.current === true) {
148
+ one.current = false;
149
+ dispatch(actions.destroy({ name: flowName }));
150
+ }
151
+ }, [dispatch, flowName, destroyOnUnmount]);
152
+ React.useEffect(() => {
153
+ /* istanbul ignore next */
154
+ if (isInitialized &&
155
+ wsDevtools &&
156
+ flowerCore.devtoolState &&
157
+ _get(flowerCore.devtoolState, '__FLOWER_DEVTOOLS__')) {
158
+ flowerCore.Emitter.emit('flower-devtool-from-client', {
159
+ source: 'flower-client',
160
+ action: 'FLOWER_CLIENT_INIT',
161
+ name: flowName,
162
+ time: new Date(),
163
+ nodeId: isInitialized,
164
+ getState: store.getState
165
+ });
166
+ }
167
+ }, [dispatch, flowName, wsDevtools, isInitialized, store]);
168
+ React.useEffect(() => {
169
+ /* istanbul ignore next */
170
+ if (isInitialized &&
171
+ wsDevtools &&
172
+ flowerCore.devtoolState &&
173
+ _get(flowerCore.devtoolState, '__FLOWER_DEVTOOLS__')) {
174
+ flowerCore.Emitter.emit('flower-devtool-from-client', {
175
+ source: 'flower-client',
176
+ action: 'SET_HISTORY',
177
+ name: flowName,
178
+ history
179
+ });
180
+ }
181
+ }, [dispatch, flowName, history, wsDevtools, isInitialized]);
182
+ React.useEffect(() => {
183
+ if (!current)
184
+ return;
185
+ /* istanbul ignore next */
186
+ if (!isInitialized)
187
+ return;
188
+ /* istanbul ignore next */
189
+ if (isInitialized &&
190
+ wsDevtools &&
191
+ flowerCore.devtoolState &&
192
+ _get(flowerCore.devtoolState, '__FLOWER_DEVTOOLS__')) {
193
+ flowerCore.Emitter.emit('flower-devtool-from-client', {
194
+ source: 'flower-client',
195
+ action: 'SET_CURRENT',
196
+ name: flowName,
197
+ current
198
+ });
199
+ }
200
+ }, [flowName, current, wsDevtools, isInitialized]);
201
+ React.useEffect(() => {
202
+ if (!current)
203
+ return;
204
+ /* istanbul ignore next */
205
+ if (!isInitialized)
206
+ return;
207
+ if (isDisabled) {
208
+ dispatch({ type: 'flower/next', payload: { flowName, disabled: true } });
209
+ // eslint-disable-next-line no-underscore-dangle, no-undef
210
+ /* istanbul ignore next */
211
+ if (wsDevtools &&
212
+ flowerCore.devtoolState &&
213
+ _get(flowerCore.devtoolState, '__FLOWER_DEVTOOLS__')) {
214
+ flowerCore.Emitter.emit('flower-devtool-from-client', {
215
+ source: 'flower-client',
216
+ action: 'FLOWER_NAVIGATE',
217
+ nodeId: current,
218
+ name: flowName,
219
+ time: new Date(),
220
+ params: { action: 'next', payload: { flowName, disabled: true } }
221
+ });
222
+ }
223
+ return;
224
+ }
225
+ // eslint-disable-next-line no-underscore-dangle, no-undef
226
+ /* istanbul ignore next */
227
+ if (wsDevtools &&
228
+ flowerCore.devtoolState &&
229
+ _get(flowerCore.devtoolState, '__FLOWER_DEVTOOLS__')) {
230
+ if (isInitialized === current)
231
+ return; // salto il primo evento
232
+ flowerCore.Emitter.emit('flower-devtool-from-client', {
233
+ source: 'flower-client',
234
+ action: 'SET_SELECTED',
235
+ nodeId: current,
236
+ name: flowName,
237
+ time: new Date()
238
+ });
239
+ }
240
+ }, [
241
+ dispatch,
242
+ flowName,
243
+ current,
244
+ isDisabled,
245
+ store,
246
+ wsDevtools,
247
+ isInitialized
248
+ ]);
249
+ const contextValues = React.useMemo(() => ({
250
+ flowName,
251
+ initialData,
252
+ currentNode: current
253
+ }), [flowName, initialData, current]);
254
+ return isInitialized ? (React.createElement(Provider, { value: contextValues },
255
+ prevFlowerNodeId !== current &&
256
+ typeof prevFlowerNodeId === 'string' &&
257
+ nodeById[prevFlowerNodeId],
258
+ !isDisabled && nodeById[current])) : null;
241
259
  };
242
- const component$c = /*#__PURE__*/React.memo(FlowerClient);
260
+ const component$c = React.memo(FlowerClient);
243
261
 
244
- const FlowerNode = ({
245
- children,
246
- onEnter,
247
- onExit
248
- }) => {
249
- React.useEffect(() => {
250
- onEnter == null || onEnter();
251
- return () => {
252
- onExit == null || onExit();
253
- };
254
- }, [onEnter, onExit]);
255
- return children;
262
+ const FlowerNode = ({ children, onEnter, onExit }) => {
263
+ React.useEffect(() => {
264
+ onEnter?.();
265
+ return () => {
266
+ onExit?.();
267
+ };
268
+ }, [onEnter, onExit]);
269
+ return children;
256
270
  };
257
- const component$b = /*#__PURE__*/React.memo(FlowerNode);
271
+ const component$b = React.memo(FlowerNode);
258
272
  component$b.displayName = 'FlowerNode';
259
273
 
260
- const FlowAction = ({
261
- children,
262
- onEnter,
263
- onExit
264
- }) => {
265
- React.useEffect(() => {
266
- onEnter == null || onEnter();
267
- return () => {
268
- onExit == null || onExit();
269
- };
270
- }, [onEnter, onExit]);
271
- return children;
274
+ const FlowAction = ({ children, onEnter, onExit }) => {
275
+ React.useEffect(() => {
276
+ onEnter?.();
277
+ return () => {
278
+ onExit?.();
279
+ };
280
+ }, [onEnter, onExit]);
281
+ return children;
272
282
  };
273
- const component$a = /*#__PURE__*/React.memo(FlowAction);
283
+ const component$a = React.memo(FlowAction);
274
284
  component$a.displayName = 'FlowerAction';
275
285
 
276
- const FlowerServer = ({
277
- children
278
- }) => {
279
- return children;
286
+ const FlowerServer = ({ children }) => {
287
+ return children;
280
288
  };
281
- const component$9 = /*#__PURE__*/React.memo(FlowerServer);
289
+ const component$9 = React.memo(FlowerServer);
282
290
  component$9.displayName = 'FlowerServer';
283
291
 
284
- const FlowerFlow = ({
285
- children,
286
- onEnter,
287
- onExit
288
- }) => {
289
- React.useEffect(() => {
290
- onEnter == null || onEnter();
291
- return () => {
292
- onExit == null || onExit();
293
- };
294
- }, [onEnter, onExit]);
295
- return children;
292
+ const FlowerFlow = ({ children, onEnter, onExit }) => {
293
+ React.useEffect(() => {
294
+ onEnter?.();
295
+ return () => {
296
+ onExit?.();
297
+ };
298
+ }, [onEnter, onExit]);
299
+ return children;
296
300
  };
297
- const component$8 = /*#__PURE__*/React.memo(FlowerFlow);
301
+ const component$8 = React.memo(FlowerFlow);
298
302
  component$8.displayName = 'FlowerFlow';
299
303
 
300
304
  function FlowerStart() {
301
- const dispatch = useDispatch();
302
- const one = React.useRef(false);
303
- const {
304
- flowName,
305
- autostart = true,
306
- currentNode
307
- } = React.useContext(context);
308
- const startNodeId = useSelector(makeSelectStartNodeId(flowName != null ? flowName : ''));
309
- React.useEffect(() => {
310
- if (startNodeId === currentNode && autostart && one.current === false) {
311
- one.current = true;
312
- dispatch({
313
- type: 'flower/next',
314
- payload: {
315
- flowName,
316
- isStart: true
305
+ const dispatch = useDispatch();
306
+ const one = React.useRef(false);
307
+ const { flowName, autostart = true, currentNode } = React.useContext(context);
308
+ const startNodeId = useSelector(makeSelectStartNodeId(flowName ?? ''));
309
+ React.useEffect(() => {
310
+ if (startNodeId === currentNode && autostart && one.current === false) {
311
+ one.current = true;
312
+ dispatch({ type: 'flower/next', payload: { flowName, isStart: true } });
313
+ // if (global.window
314
+ // // eslint-disable-next-line no-underscore-dangle, no-undef
315
+ // && global.window.__FLOWER_DEVTOOLS__ && global.window.__FLOWER_DEVTOOLS__AUTO) {
316
+ // Emitter.emit('flower-devtool-from-client', {
317
+ // source: 'flower-client',
318
+ // action: 'START_FLOWER',
319
+ // name: flowName,
320
+ // });
321
+ // }
317
322
  }
318
- });
319
- }
320
- }, [dispatch, autostart, startNodeId, currentNode, flowName]);
321
- return null;
323
+ }, [dispatch, autostart, startNodeId, currentNode, flowName]);
324
+ return null;
322
325
  }
323
- const component$7 = /*#__PURE__*/React.memo(FlowerStart);
326
+ const component$7 = React.memo(FlowerStart);
324
327
  component$7.displayName = 'FlowerStart';
325
328
 
326
- const FlowerRoute = ({
327
- autostart: _autostart = true,
328
- children,
329
- onEnter,
330
- onExit
331
- }) => {
332
- const dispatch = useDispatch();
333
- const one = React.useRef(false);
334
- const {
335
- flowName
336
- } = React.useContext(context);
337
- React.useEffect(() => {
338
- onEnter == null || onEnter();
339
- return () => {
340
- onExit == null || onExit();
341
- };
342
- }, [onEnter, onExit]);
343
- React.useEffect(() => {
344
- if (_autostart && one.current === false) {
345
- one.current = true;
346
- dispatch({
347
- type: 'flower/next',
348
- payload: {
349
- flowName
329
+ const FlowerRoute = ({ autostart = true, children, onEnter, onExit }) => {
330
+ const dispatch = useDispatch();
331
+ const one = React.useRef(false);
332
+ const { flowName } = React.useContext(context);
333
+ React.useEffect(() => {
334
+ onEnter?.();
335
+ return () => {
336
+ onExit?.();
337
+ };
338
+ }, [onEnter, onExit]);
339
+ React.useEffect(() => {
340
+ if (autostart && one.current === false) {
341
+ one.current = true;
342
+ dispatch({ type: 'flower/next', payload: { flowName } });
350
343
  }
351
- });
352
- }
353
- }, [dispatch, flowName, _autostart]);
354
- return children;
344
+ }, [dispatch, flowName, autostart]);
345
+ return children;
355
346
  };
356
- const component$6 = /*#__PURE__*/React.memo(FlowerRoute);
347
+ const component$6 = React.memo(FlowerRoute);
357
348
  component$6.displayName = 'FlowerRoute';
358
349
 
359
- function _extends() {
360
- return _extends = Object.assign ? Object.assign.bind() : function (n) {
361
- for (var e = 1; e < arguments.length; e++) {
362
- var t = arguments[e];
363
- for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
364
- }
365
- return n;
366
- }, _extends.apply(null, arguments);
367
- }
368
- function _objectWithoutPropertiesLoose(r, e) {
369
- if (null == r) return {};
370
- var t = {};
371
- for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
372
- if (e.indexOf(n) >= 0) continue;
373
- t[n] = r[n];
374
- }
375
- return t;
376
- }
377
-
378
- const FlowerRule = ({
379
- children,
380
- rules,
381
- value,
382
- alwaysDisplay,
383
- flowName,
384
- id,
385
- onUpdate
386
- }) => {
387
- const {
388
- flowName: flowNameContext,
389
- currentNode
390
- } = React.useContext(context);
391
- const name = flowName || flowNameContext;
392
- const keys = flowerCore.MatchRules.utils.getKeys(rules, {
393
- prefix: name
394
- });
395
- const hidden = useSelector(selectorRulesDisabled(id != null ? id : '', rules, keys != null ? keys : [], name != null ? name : '', value, currentNode != null ? currentNode : ''));
396
- React.useEffect(() => {
397
- if (onUpdate) {
398
- onUpdate(hidden);
350
+ const FlowerRule = ({ children, rules, value, alwaysDisplay, flowName, id, onUpdate }) => {
351
+ const { flowName: flowNameContext, currentNode } = React.useContext(context);
352
+ const name = flowName || flowNameContext;
353
+ const keys = flowerCore.MatchRules.utils.getKeys(rules, { prefix: name });
354
+ const hidden = useSelector(selectorRulesDisabled(id ?? '', rules, keys ?? [], name ?? '', value, currentNode ?? ''));
355
+ React.useEffect(() => {
356
+ if (onUpdate) {
357
+ onUpdate(hidden);
358
+ }
359
+ }, [hidden, onUpdate]);
360
+ if (typeof children === 'function') {
361
+ if (alwaysDisplay && hidden) {
362
+ return children({ hidden });
363
+ }
364
+ if (hidden) {
365
+ return undefined;
366
+ }
367
+ return children({});
399
368
  }
400
- }, [hidden, onUpdate]);
401
- if (typeof children === 'function') {
402
369
  if (alwaysDisplay && hidden) {
403
- return children({
404
- hidden
405
- });
406
- }
407
- if (hidden) {
408
- return undefined;
409
- }
410
- return children({});
411
- }
412
- if (alwaysDisplay && hidden) {
413
- return React.Children.map(children, (child, i) => {
414
- if ( /*#__PURE__*/React.isValidElement(child)) {
415
- const {
416
- props,
417
- type
418
- } = child;
419
- const Component = type;
420
- return Component && /*#__PURE__*/React.createElement(Component, _extends({
421
- key: i,
422
- hidden: true
423
- }, props));
424
- }
425
- return child;
426
- });
427
- }
428
- return hidden ? undefined : React.Children.map(children, (child, i) => {
429
- if ( /*#__PURE__*/React.isValidElement(child)) {
430
- const {
431
- props,
432
- type
433
- } = child;
434
- const Component = type;
435
- return Component && /*#__PURE__*/React.createElement(Component, _extends({
436
- key: i
437
- }, props));
370
+ return React.Children.map(children, (child, i) => {
371
+ if (React.isValidElement(child)) {
372
+ const { props, type } = child;
373
+ const Component = type;
374
+ // eslint-disable-next-line react/jsx-props-no-spreading
375
+ return Component && React.createElement(Component, { key: i, hidden: true, ...props });
376
+ }
377
+ return child;
378
+ });
438
379
  }
439
- return child;
440
- });
380
+ return hidden
381
+ ? undefined
382
+ : React.Children.map(children, (child, i) => {
383
+ if (React.isValidElement(child)) {
384
+ const { props, type } = child;
385
+ const Component = type;
386
+ // eslint-disable-next-line react/jsx-props-no-spreading
387
+ return Component && React.createElement(Component, { key: i, ...props });
388
+ }
389
+ return child;
390
+ });
441
391
  };
442
- const component$5 = /*#__PURE__*/React.memo(FlowerRule);
392
+ const component$5 = React.memo(FlowerRule);
443
393
  component$5.displayName = 'FlowerRule';
444
394
 
445
- const _excluded$3 = ["Component", "id", "flowName", "currentNode", "validate", "asyncDebounce", "asyncValidate", "asyncInitialError", "asyncWaitingError", "destroyValue", "onBlur", "hidden", "onUpdate", "defaultValue"];
395
+ /* eslint-disable */
446
396
  function isIntrinsicElement$1(x) {
447
- return typeof x === 'string';
397
+ return typeof x === 'string';
448
398
  }
449
- function Wrapper$1(_ref) {
450
- let {
451
- Component,
452
- id,
453
- flowName,
454
- currentNode,
455
- validate,
456
- asyncDebounce = 0,
457
- asyncValidate,
458
- asyncInitialError,
459
- asyncWaitingError,
460
- destroyValue,
461
- onBlur = val => null,
462
- hidden,
463
- onUpdate,
464
- defaultValue
465
- } = _ref,
466
- props = _objectWithoutPropertiesLoose(_ref, _excluded$3);
467
- const dispatch = useDispatch();
468
- const [touched, setTouched] = React.useState();
469
- const [customErrors, setCustomErrors] = React.useState(asyncValidate && [asyncInitialError]);
470
- const [isValidating, setIsValidating] = React.useState(undefined);
471
- const {
472
- flowNameFromPath = flowName,
473
- path
474
- } = React.useMemo(() => flowerCore.CoreUtils.getPath(id), [id]);
475
- const value = useSelector(getDataFromState(flowNameFromPath, path));
476
- const errors = useSelector(makeSelectFieldError(flowName, id, validate), flowerCore.CoreUtils.allEqual);
477
- const refValue = React.useRef();
478
- const one = React.useRef();
479
- const validateFn = React.useCallback(async value => {
480
- if (asyncWaitingError) {
481
- setCustomErrors([asyncWaitingError]);
482
- }
483
- setIsValidating(true);
484
- const state = flowerCore.FlowerStateUtils.getAllData(store);
485
- const res = await asyncValidate(value, state, errors);
486
- setIsValidating(false);
487
- setCustomErrors(res);
488
- }, [asyncWaitingError, errors]);
489
- const debouncedValidation = React.useCallback(debounce(validateFn, asyncDebounce), [validateFn]);
490
- React.useEffect(() => {
491
- if (asyncValidate) {
492
- if (refValue.current === value) return;
493
- refValue.current = value;
494
- const hasValue = !flowerCore.MatchRules.utils.isEmpty(value);
495
- if (!hasValue) {
496
- setCustomErrors([asyncInitialError]);
399
+ //TODO make types for wrapper function props
400
+ function Wrapper$1({ Component, id, flowName, currentNode, validate, asyncDebounce = 0, asyncValidate, asyncInitialError, asyncWaitingError, destroyValue, onBlur = (val) => null, hidden, onUpdate, defaultValue, ...props }) {
401
+ const dispatch = useDispatch();
402
+ const [touched, setTouched] = React.useState();
403
+ const [customErrors, setCustomErrors] = React.useState(asyncValidate && [asyncInitialError]);
404
+ const [isValidating, setIsValidating] = React.useState(undefined);
405
+ const { flowNameFromPath = flowName, path } = React.useMemo(() => flowerCore.CoreUtils.getPath(id), [id]);
406
+ const value = useSelector(getDataFromState(flowNameFromPath, path));
407
+ const errors = useSelector(makeSelectFieldError(flowName, id, validate), flowerCore.CoreUtils.allEqual);
408
+ const refValue = React.useRef();
409
+ const one = React.useRef();
410
+ const validateFn = React.useCallback(async (value) => {
411
+ if (asyncWaitingError) {
412
+ setCustomErrors([asyncWaitingError]);
413
+ }
414
+ setIsValidating(true);
415
+ const state = flowerCore.FlowerStateUtils.getAllData(store);
416
+ const res = await asyncValidate(value, state, errors);
497
417
  setIsValidating(false);
498
- return;
499
- }
500
- setTouched(true);
501
- debouncedValidation(value);
502
- }
503
- }, [asyncValidate, asyncInitialError, value, debouncedValidation]);
504
- const touchedForm = useSelector(makeSelectNodeFormTouched(flowName, currentNode));
505
- const allErrors = React.useMemo(() => [...errors, ...(customErrors || []).filter(Boolean)], [errors, customErrors]);
506
- React.useEffect(() => {
507
- if (onUpdate) {
508
- onUpdate(value);
509
- }
510
- }, [value, onUpdate]);
511
- const onChange = React.useCallback(val => {
512
- dispatch({
513
- type: `flower/addDataByPath`,
514
- payload: {
515
- flowName: flowNameFromPath,
516
- id: path,
517
- value: val
518
- }
519
- });
520
- }, [flowNameFromPath, path, onBlur, dispatch]);
521
- const onBlurInternal = React.useCallback(e => {
522
- setTouched(true);
523
- onBlur && onBlur(e);
524
- }, [onBlur]);
525
- React.useLayoutEffect(() => {
526
- dispatch({
527
- type: 'flower/formAddErrors',
528
- payload: {
529
- name: flowName,
530
- id,
531
- currentNode,
532
- errors: allErrors
533
- }
534
- });
535
- }, [id, flowName, allErrors, currentNode]);
536
- React.useEffect(() => {
537
- dispatch({
538
- type: 'flower/setFormIsValidating',
539
- payload: {
540
- name: flowName,
541
- currentNode,
542
- isValidating
543
- }
544
- });
545
- }, [flowName, currentNode, isValidating]);
546
- React.useLayoutEffect(() => {
547
- return () => {
548
- if (destroyValue) {
418
+ setCustomErrors(res);
419
+ }, [asyncWaitingError, errors]);
420
+ const debouncedValidation = React.useCallback(debounce(validateFn, asyncDebounce), [
421
+ validateFn
422
+ ]);
423
+ React.useEffect(() => {
424
+ if (asyncValidate) {
425
+ if (refValue.current === value)
426
+ return;
427
+ refValue.current = value;
428
+ const hasValue = !flowerCore.MatchRules.utils.isEmpty(value);
429
+ if (!hasValue) {
430
+ setCustomErrors([asyncInitialError]);
431
+ setIsValidating(false);
432
+ return;
433
+ }
434
+ setTouched(true);
435
+ debouncedValidation(value);
436
+ }
437
+ }, [asyncValidate, asyncInitialError, value, debouncedValidation]);
438
+ const touchedForm = useSelector(makeSelectNodeFormTouched(flowName, currentNode));
439
+ const allErrors = React.useMemo(() => [...errors, ...(customErrors || []).filter(Boolean)], [errors, customErrors]);
440
+ React.useEffect(() => {
441
+ if (onUpdate) {
442
+ onUpdate(value);
443
+ }
444
+ }, [value, onUpdate]);
445
+ const onChange = React.useCallback((val) => {
549
446
  dispatch({
550
- type: `flower/unsetData`,
551
- payload: {
552
- flowName: flowNameFromPath,
553
- id: path
554
- }
447
+ type: `flower/addDataByPath`,
448
+ payload: {
449
+ flowName: flowNameFromPath,
450
+ id: path,
451
+ value: val
452
+ }
555
453
  });
556
- }
557
- dispatch({
558
- type: 'flower/formRemoveErrors',
559
- payload: {
560
- name: flowName,
561
- id,
562
- currentNode
454
+ }, [flowNameFromPath, path, onBlur, dispatch]);
455
+ const onBlurInternal = React.useCallback((e) => {
456
+ setTouched(true);
457
+ onBlur && onBlur(e);
458
+ }, [onBlur]);
459
+ React.useLayoutEffect(() => {
460
+ dispatch({
461
+ type: 'flower/formAddErrors',
462
+ payload: {
463
+ name: flowName,
464
+ id,
465
+ currentNode,
466
+ errors: allErrors
467
+ }
468
+ });
469
+ }, [id, flowName, allErrors, currentNode]);
470
+ React.useEffect(() => {
471
+ dispatch({
472
+ type: 'flower/setFormIsValidating',
473
+ payload: {
474
+ name: flowName,
475
+ currentNode,
476
+ isValidating
477
+ }
478
+ });
479
+ }, [flowName, currentNode, isValidating]);
480
+ React.useLayoutEffect(() => {
481
+ // destroy
482
+ return () => {
483
+ if (destroyValue) {
484
+ dispatch({
485
+ type: `flower/unsetData`,
486
+ payload: { flowName: flowNameFromPath, id: path }
487
+ });
488
+ }
489
+ dispatch({
490
+ type: 'flower/formRemoveErrors',
491
+ payload: {
492
+ name: flowName,
493
+ id,
494
+ currentNode
495
+ }
496
+ });
497
+ };
498
+ }, [destroyValue]);
499
+ React.useEffect(() => {
500
+ if (defaultValue && !one.current) {
501
+ one.current = true;
502
+ onChange(defaultValue);
563
503
  }
564
- });
565
- };
566
- }, [destroyValue]);
567
- React.useEffect(() => {
568
- if (defaultValue && !one.current) {
569
- one.current = true;
570
- onChange(defaultValue);
504
+ }, [defaultValue, onChange]);
505
+ const isTouched = touched || touchedForm;
506
+ const newProps = React.useMemo(() => ({
507
+ ...props,
508
+ id,
509
+ value,
510
+ errors: isTouched && allErrors,
511
+ hasError: isTouched && !!allErrors.length,
512
+ onChange,
513
+ onBlur: onBlurInternal,
514
+ isTouched,
515
+ hidden,
516
+ isValidating
517
+ }), [
518
+ props,
519
+ id,
520
+ value,
521
+ allErrors,
522
+ isTouched,
523
+ onChange,
524
+ onBlurInternal,
525
+ hidden,
526
+ isValidating
527
+ ]);
528
+ if (typeof Component === 'function') {
529
+ return Component(newProps);
530
+ }
531
+ // TODO si arriva in questa condizione quando si passa un componente primitivo es. div
532
+ // in questo caso non posso props custom di flower
533
+ if (isIntrinsicElement$1(Component)) {
534
+ return React.createElement(Component, { id: id, ...props });
571
535
  }
572
- }, [defaultValue, onChange]);
573
- const isTouched = touched || touchedForm;
574
- const newProps = React.useMemo(() => _extends({}, props, {
575
- id,
576
- value,
577
- errors: isTouched && allErrors,
578
- hasError: isTouched && !!allErrors.length,
579
- onChange,
580
- onBlur: onBlurInternal,
581
- isTouched,
582
- hidden,
583
- isValidating
584
- }), [props, id, value, allErrors, isTouched, onChange, onBlurInternal, hidden, isValidating]);
585
- if (typeof Component === 'function') {
586
- return Component(newProps);
587
- }
588
- if (isIntrinsicElement$1(Component)) {
589
- return /*#__PURE__*/React.createElement(Component, _extends({
590
- id: id
591
- }, props));
592
- }
593
- return Component && /*#__PURE__*/React.createElement(Component, _extends({}, newProps));
536
+ return Component && React.createElement(Component, { ...newProps });
594
537
  }
595
- const FlowerField = ({
596
- id,
597
- validate,
598
- asyncValidate,
599
- asyncDebounce,
600
- asyncInitialError,
601
- asyncWaitingError,
602
- rules,
603
- alwaysDisplay,
604
- value,
605
- children,
606
- defaultValue,
607
- destroyValue,
608
- flowName,
609
- onUpdate
610
- }) => {
611
- const {
612
- flowName: flowNameContext,
613
- currentNode
614
- } = React.useContext(context);
615
- const name = flowName || flowNameContext;
616
- if (typeof children === 'function') {
617
- return /*#__PURE__*/React.createElement(component$5, {
618
- alwaysDisplay: alwaysDisplay,
619
- rules: rules,
620
- value: value,
621
- flowName: name,
622
- id: id
623
- }, ({
624
- hidden
625
- }) => ( /*#__PURE__*/React.createElement(Wrapper$1, {
626
- hidden: hidden,
627
- id: id,
628
- Component: children,
629
- flowName: name,
630
- currentNode: currentNode,
631
- validate: validate,
632
- asyncValidate: asyncValidate,
633
- asyncDebounce: asyncDebounce,
634
- asyncInitialError: asyncInitialError,
635
- asyncWaitingError: asyncWaitingError,
636
- destroyValue: destroyValue,
637
- onUpdate: onUpdate,
638
- defaultValue: defaultValue
639
- })));
640
- }
641
- return React.Children.map(children, (child, i) => {
642
- if (! /*#__PURE__*/React.isValidElement(child)) {
643
- return child;
538
+ const FlowerField = ({ id, validate, asyncValidate, asyncDebounce, asyncInitialError, asyncWaitingError, rules, alwaysDisplay, value, children, defaultValue, destroyValue, flowName, onUpdate }) => {
539
+ const { flowName: flowNameContext, currentNode } = React.useContext(context);
540
+ const name = flowName || flowNameContext;
541
+ if (typeof children === 'function') {
542
+ return (React.createElement(component$5, { alwaysDisplay: alwaysDisplay, rules: rules, value: value, flowName: name, id: id }, ({ hidden }) => (React.createElement(Wrapper$1, { hidden: hidden, id: id, Component: children, flowName: name, currentNode: currentNode, validate: validate, asyncValidate: asyncValidate, asyncDebounce: asyncDebounce, asyncInitialError: asyncInitialError, asyncWaitingError: asyncWaitingError, destroyValue: destroyValue, onUpdate: onUpdate, defaultValue: defaultValue }))));
644
543
  }
645
- const {
646
- type,
647
- props
648
- } = child;
649
- const Component = type;
650
- return /*#__PURE__*/React.createElement(component$5, {
651
- key: i,
652
- alwaysDisplay: alwaysDisplay,
653
- rules: rules,
654
- value: value,
655
- flowName: name
656
- }, ({
657
- hidden
658
- }) => ( /*#__PURE__*/React.createElement(Wrapper$1, _extends({}, props, {
659
- hidden: hidden,
660
- id: id,
661
- Component: Component,
662
- flowName: name,
663
- currentNode: currentNode,
664
- validate: validate,
665
- asyncValidate: asyncValidate,
666
- asyncDebounce: asyncDebounce,
667
- asyncInitialError: asyncInitialError,
668
- asyncWaitingError: asyncWaitingError,
669
- destroyValue: destroyValue,
670
- onUpdate: onUpdate,
671
- defaultValue: defaultValue
672
- }))));
673
- });
544
+ return React.Children.map(children, (child, i) => {
545
+ if (!React.isValidElement(child)) {
546
+ return child;
547
+ }
548
+ const { type, props } = child;
549
+ const Component = type;
550
+ return (React.createElement(component$5, { key: i, alwaysDisplay: alwaysDisplay, rules: rules, value: value, flowName: name }, ({ hidden }) => (React.createElement(Wrapper$1, { ...props, hidden: hidden, id: id, Component: Component, flowName: name, currentNode: currentNode, validate: validate, asyncValidate: asyncValidate, asyncDebounce: asyncDebounce, asyncInitialError: asyncInitialError, asyncWaitingError: asyncWaitingError, destroyValue: destroyValue, onUpdate: onUpdate, defaultValue: defaultValue }))));
551
+ });
674
552
  };
675
- const component$4 = /*#__PURE__*/React.memo(FlowerField);
553
+ const component$4 = React.memo(FlowerField);
676
554
  component$4.displayName = 'FlowerField';
677
555
 
678
- const _excluded$2 = ["Component", "id", "flowName", "spreadValue", "hidden", "onUpdate"],
679
- _excluded2 = ["id", "alwaysDisplay", "rules", "value", "Component", "spreadValue", "flowName", "onUpdate"];
680
- function Wrapper(_ref) {
681
- let {
682
- Component,
683
- id,
684
- flowName,
685
- spreadValue,
686
- hidden,
687
- onUpdate
688
- } = _ref,
689
- props = _objectWithoutPropertiesLoose(_ref, _excluded$2);
690
- const {
691
- flowNameFromPath = flowName,
692
- path
693
- } = React.useMemo(() => flowerCore.CoreUtils.getPath(id), [id]);
694
- const value = useSelector(getDataFromState(flowNameFromPath, path));
695
- const values = spreadValue && typeof value === 'object' && !Array.isArray(value) ? value : {
696
- value
697
- };
698
- React.useEffect(() => {
699
- if (onUpdate) {
700
- onUpdate(value);
701
- }
702
- }, [onUpdate, value]);
703
- return /*#__PURE__*/React.createElement(Component, _extends({
704
- id: id
705
- }, props, {
706
- flowName: flowName,
707
- hidden: hidden
708
- }, values));
556
+ /* eslint-disable */
557
+ //TODO make types for wrapper function
558
+ function Wrapper({ Component, id, flowName, spreadValue, hidden, onUpdate, ...props }) {
559
+ const { flowNameFromPath = flowName, path } = React.useMemo(() => flowerCore.CoreUtils.getPath(id), [id]);
560
+ const value = useSelector(getDataFromState(flowNameFromPath, path));
561
+ const values = spreadValue && typeof value === 'object' && !Array.isArray(value)
562
+ ? value
563
+ : { value };
564
+ React.useEffect(() => {
565
+ if (onUpdate) {
566
+ onUpdate(value);
567
+ }
568
+ }, [onUpdate, value]);
569
+ return (React.createElement(Component, { id: id, ...props, flowName: flowName, hidden: hidden, ...values }));
709
570
  }
710
- const RenderRules$1 = _ref2 => {
711
- let {
712
- id,
713
- alwaysDisplay,
714
- rules,
715
- value,
716
- Component,
717
- spreadValue,
718
- flowName,
719
- onUpdate
720
- } = _ref2,
721
- props = _objectWithoutPropertiesLoose(_ref2, _excluded2);
722
- return /*#__PURE__*/React.createElement(component$5, {
723
- alwaysDisplay: alwaysDisplay,
724
- rules: rules,
725
- value: value,
726
- flowName: flowName,
727
- id: id
728
- }, ({
729
- hidden
730
- }) => ( /*#__PURE__*/React.createElement(Wrapper, _extends({}, props, {
731
- hidden: hidden,
732
- id: id,
733
- Component: Component,
734
- spreadValue: spreadValue,
735
- flowName: flowName,
736
- onUpdate: onUpdate
737
- }))));
571
+ const RenderRules$1 = ({ id, alwaysDisplay, rules, value, Component, spreadValue, flowName, onUpdate, ...props }) => {
572
+ return (React.createElement(component$5, { alwaysDisplay: alwaysDisplay, rules: rules, value: value, flowName: flowName, id: id }, ({ hidden }) => (React.createElement(Wrapper, { ...props, hidden: hidden, id: id, Component: Component, spreadValue: spreadValue, flowName: flowName, onUpdate: onUpdate }))));
738
573
  };
739
- const FlowerValue = ({
740
- id: _id = '*',
741
- rules,
742
- alwaysDisplay,
743
- value,
744
- children,
745
- spreadValue,
746
- flowName,
747
- onUpdate
748
- }) => {
749
- const {
750
- flowName: flowNameContext
751
- } = React.useContext(context);
752
- const name = flowName || flowNameContext;
753
- if (typeof children === 'function') {
754
- return /*#__PURE__*/React.createElement(RenderRules$1, {
755
- id: _id,
756
- alwaysDisplay: alwaysDisplay,
757
- rules: rules,
758
- value: value,
759
- spreadValue: spreadValue,
760
- Component: children,
761
- flowName: name,
762
- onUpdate: onUpdate
574
+ const FlowerValue = ({ id = '*', rules, alwaysDisplay, value, children, spreadValue, flowName, onUpdate, }) => {
575
+ const { flowName: flowNameContext } = React.useContext(context);
576
+ const name = flowName || flowNameContext;
577
+ if (typeof children === 'function') {
578
+ return (React.createElement(RenderRules$1, { id: id, alwaysDisplay: alwaysDisplay, rules: rules, value: value, spreadValue: spreadValue, Component: children, flowName: name, onUpdate: onUpdate }));
579
+ }
580
+ return React.Children.map(children, (child, i) => {
581
+ if (!React.isValidElement(child))
582
+ return child;
583
+ const { type, props } = child;
584
+ const Component = type;
585
+ // eslint-disable-next-line react/jsx-props-no-spreading
586
+ return (React.createElement(RenderRules$1, { key: i, id: id, alwaysDisplay: alwaysDisplay, rules: rules, value: value, spreadValue: spreadValue, flowName: name, Component: Component, ...props, onUpdate: onUpdate }));
763
587
  });
764
- }
765
- return React.Children.map(children, (child, i) => {
766
- if (! /*#__PURE__*/React.isValidElement(child)) return child;
767
- const {
768
- type,
769
- props
770
- } = child;
771
- const Component = type;
772
- return /*#__PURE__*/React.createElement(RenderRules$1, _extends({
773
- key: i,
774
- id: _id,
775
- alwaysDisplay: alwaysDisplay,
776
- rules: rules,
777
- value: value,
778
- spreadValue: spreadValue,
779
- flowName: name,
780
- Component: Component
781
- }, props, {
782
- onUpdate: onUpdate
783
- }));
784
- });
785
588
  };
786
- const component$3 = /*#__PURE__*/React.memo(FlowerValue);
589
+ const component$3 = React.memo(FlowerValue);
787
590
  component$3.displayName = 'FlowerValue';
788
591
 
789
592
  const ACTION_TYPES = {
790
- back: ['prev', 'prevToNode'],
791
- jump: ['node', 'node'],
792
- next: ['next', 'next'],
793
- restart: ['restart', 'restart'],
794
- reset: ['reset', 'initializeFromNode']
593
+ back: ['prev', 'prevToNode'],
594
+ jump: ['node', 'node'],
595
+ next: ['next', 'next'],
596
+ restart: ['restart', 'restart'],
597
+ reset: ['reset', 'initializeFromNode']
795
598
  };
796
599
  const PAYLAOAD_KEYS_NEEDED = {
797
- back: ['node'],
798
- jump: ['node', 'history'],
799
- next: ['node', 'route', 'data'],
800
- restart: ['node'],
801
- reset: ['node', 'initialData']
600
+ back: ['node'],
601
+ jump: ['node', 'history'],
602
+ next: ['node', 'route', 'data'],
603
+ restart: ['node'],
604
+ reset: ['node', 'initialData']
802
605
  };
803
606
  const makeActionPayload = (actions, keys) => (flowName, params) => {
804
- const rest = typeof params === 'string' ? {
805
- node: params
806
- } : params;
807
- const payload = _extends({
808
- flowName: (params == null ? void 0 : params.flowName) || flowName
809
- }, Object.fromEntries(Object.entries(rest != null ? rest : {}).filter(([k]) => keys.includes(k))));
810
- const type = !params || !payload.node ? actions[0] : actions[1];
811
- return {
812
- type,
813
- payload
814
- };
607
+ const rest = typeof params === 'string' ? { node: params } : params;
608
+ const payload = {
609
+ flowName: params?.flowName || flowName,
610
+ ...Object.fromEntries(Object.entries(rest ?? {}).filter(([k]) => keys.includes(k)))
611
+ };
612
+ const type = !params || !payload.node ? actions[0] : actions[1];
613
+ return {
614
+ type,
615
+ payload
616
+ };
815
617
  };
816
618
  const makeActionPayloadOnPrev = makeActionPayload(ACTION_TYPES.back, PAYLAOAD_KEYS_NEEDED.back);
817
619
  const makeActionPayloadOnReset = makeActionPayload(ACTION_TYPES.reset, PAYLAOAD_KEYS_NEEDED.reset);
818
620
  const makeActionPayloadOnNode = makeActionPayload(ACTION_TYPES.jump, PAYLAOAD_KEYS_NEEDED.jump);
819
621
  const makeActionPayloadOnNext = makeActionPayload(ACTION_TYPES.next, PAYLAOAD_KEYS_NEEDED.next);
820
622
  const makeActionPayloadOnRestart = makeActionPayload(ACTION_TYPES.restart, PAYLAOAD_KEYS_NEEDED.restart);
821
- const useFlower = ({
822
- flowName: customFlowName,
823
- name
824
- } = {}) => {
825
- const dispatch = useDispatch();
826
- const {
827
- flowName: flowNameDefault,
828
- initialData
829
- } = React.useContext(context);
830
- const flowName = customFlowName || name || flowNameDefault;
831
- const nodeId = useSelector(makeSelectCurrentNodeId(flowName != null ? flowName : ''));
832
- const emitNavigateEvent = React.useCallback(params => {
833
- if (_get(global.window, '__FLOWER_DEVTOOLS__')) {
834
- flowerCore.Emitter.emit('flower-devtool-from-client', {
835
- source: 'flower-client',
836
- action: 'FLOWER_NAVIGATE',
623
+ /** This hook allows you to read flow informations, such as the flowName and ID of the current node.
624
+ *
625
+ * It also exposes all the functions to navigate within the flow:
626
+ *
627
+ * - next
628
+ *
629
+ * - back
630
+ *
631
+ * - jump
632
+ *
633
+ * - reset
634
+ *
635
+ * - restart
636
+ *
637
+ * @param {string} flowName - first optional parameter
638
+ *
639
+ * @param {string} name - optional parameter, if flowName exist, name is not used
640
+ */
641
+ const useFlower = ({ flowName: customFlowName, name } = {}) => {
642
+ const dispatch = useDispatch();
643
+ const { flowName: flowNameDefault, initialData } = React.useContext(context);
644
+ const flowName = customFlowName || name || flowNameDefault;
645
+ const nodeId = useSelector(makeSelectCurrentNodeId(flowName ?? ''));
646
+ const startId = useSelector(makeSelectStartNodeId(flowName ?? ''));
647
+ const emitNavigateEvent = React.useCallback(
648
+ //TODO check this function is needed
649
+ (params) => {
650
+ /* istanbul ignore next */
651
+ // eslint-disable-next-line no-underscore-dangle, no-undef
652
+ if (_get(flowerCore.devtoolState, '__FLOWER_DEVTOOLS__')) {
653
+ flowerCore.Emitter.emit('flower-devtool-from-client', {
654
+ source: 'flower-client',
655
+ action: 'FLOWER_NAVIGATE',
656
+ nodeId,
657
+ name: flowName,
658
+ time: new Date(),
659
+ params
660
+ });
661
+ }
662
+ }, [flowName, nodeId]);
663
+ const next = React.useCallback((param) => {
664
+ const params = typeof param === 'string' ? { route: param } : { data: param };
665
+ const { type, payload } = makeActionPayloadOnNext(flowName, params);
666
+ dispatch({
667
+ type: `flower/${type}`,
668
+ payload
669
+ });
670
+ emitNavigateEvent({ type, payload });
671
+ }, [dispatch, emitNavigateEvent, flowName]);
672
+ const back = React.useCallback((param) => {
673
+ const { type, payload } = makeActionPayloadOnPrev(flowName, param);
674
+ dispatch({ type: `flower/${type}`, payload });
675
+ emitNavigateEvent({ type, payload });
676
+ }, [dispatch, emitNavigateEvent, flowName]);
677
+ const restart = React.useCallback((param) => {
678
+ const { type, payload } = makeActionPayloadOnRestart(flowName, param);
679
+ dispatch({ type: `flower/${type}`, payload });
680
+ emitNavigateEvent({ type, payload });
681
+ }, [dispatch, emitNavigateEvent, flowName]);
682
+ const reset = React.useCallback((param) => {
683
+ const { type, payload } = makeActionPayloadOnReset(flowName, typeof param === 'string'
684
+ ? { node: param, initialData }
685
+ : {
686
+ ...param,
687
+ initialData
688
+ });
689
+ dispatch({ type: `flower/${type}`, payload });
690
+ emitNavigateEvent({ type, payload });
691
+ }, [dispatch, emitNavigateEvent, flowName, initialData]);
692
+ const jump = React.useCallback((param) => {
693
+ const { type, payload } = makeActionPayloadOnNode(flowName, param);
694
+ dispatch({ type: `flower/${type}`, payload });
695
+ emitNavigateEvent({ type, payload });
696
+ }, [dispatch, emitNavigateEvent, flowName]);
697
+ return {
698
+ flowName,
837
699
  nodeId,
838
- name: flowName,
839
- time: new Date(),
840
- params
841
- });
842
- }
843
- }, [flowName, nodeId]);
844
- const next = React.useCallback(param => {
845
- const params = typeof param === 'string' ? {
846
- route: param
847
- } : {
848
- data: param
700
+ startId,
701
+ next,
702
+ jump,
703
+ back,
704
+ reset,
705
+ restart
849
706
  };
850
- const {
851
- type,
852
- payload
853
- } = makeActionPayloadOnNext(flowName, params);
854
- dispatch({
855
- type: `flower/${type}`,
856
- payload
857
- });
858
- emitNavigateEvent({
859
- type,
860
- payload
861
- });
862
- }, [dispatch, emitNavigateEvent, flowName]);
863
- const back = React.useCallback(param => {
864
- const {
865
- type,
866
- payload
867
- } = makeActionPayloadOnPrev(flowName, param);
868
- dispatch({
869
- type: `flower/${type}`,
870
- payload
871
- });
872
- emitNavigateEvent({
873
- type,
874
- payload
875
- });
876
- }, [dispatch, emitNavigateEvent, flowName]);
877
- const restart = React.useCallback(param => {
878
- const {
879
- type,
880
- payload
881
- } = makeActionPayloadOnRestart(flowName, param);
882
- dispatch({
883
- type: `flower/${type}`,
884
- payload
885
- });
886
- emitNavigateEvent({
887
- type,
888
- payload
889
- });
890
- }, [dispatch, emitNavigateEvent, flowName]);
891
- const reset = React.useCallback(param => {
892
- const {
893
- type,
894
- payload
895
- } = makeActionPayloadOnReset(flowName, typeof param === 'string' ? {
896
- node: param,
897
- initialData
898
- } : _extends({}, param, {
899
- initialData
900
- }));
901
- dispatch({
902
- type: `flower/${type}`,
903
- payload
904
- });
905
- emitNavigateEvent({
906
- type,
907
- payload
908
- });
909
- }, [dispatch, emitNavigateEvent, flowName, initialData]);
910
- const jump = React.useCallback(param => {
911
- const {
912
- type,
913
- payload
914
- } = makeActionPayloadOnNode(flowName, param);
915
- dispatch({
916
- type: `flower/${type}`,
917
- payload
918
- });
919
- emitNavigateEvent({
920
- type,
921
- payload
922
- });
923
- }, [dispatch, emitNavigateEvent, flowName]);
924
- return {
925
- flowName,
926
- nodeId,
927
- next,
928
- jump,
929
- back,
930
- reset,
931
- restart
932
- };
933
707
  };
934
708
 
935
- const useFlowerNavigate = ({
936
- flowName,
937
- action,
938
- route,
939
- node
940
- }) => {
941
- const {
942
- flowName: flowNameContext
943
- } = React.useContext(context);
944
- const name = flowName || flowNameContext;
945
- const {
946
- next,
947
- jump,
948
- back,
949
- reset,
950
- restart
951
- } = useFlower({
952
- flowName: name
953
- });
954
- const onNavigate = React.useCallback(() => {
955
- switch (action) {
956
- case 'next':
957
- next(route);
958
- return;
959
- case 'jump':
960
- jump(node);
961
- return;
962
- case 'back':
963
- back(node);
964
- return;
965
- case 'reset':
966
- reset(node);
967
- return;
968
- case 'restart':
969
- restart(node);
970
- return;
971
- default:
972
- next();
973
- return;
974
- }
975
- }, [next, jump, back, reset, restart, node, route]);
976
- return {
977
- onNavigate,
978
- flowName
979
- };
709
+ /* eslint-disable */
710
+ // {
711
+ // flowName?: string | undefined;
712
+ // action?: 'next' | 'back' | 'reset' | 'jump';
713
+ // } & (
714
+ // | {
715
+ // node?: undefined;
716
+ // route?: Route;
717
+ // }
718
+ // | {
719
+ // node?: RoutePrev;
720
+ // route?: undefined;
721
+ // }
722
+ // | {
723
+ // node?: RouteReset;
724
+ // route?: undefined;
725
+ // }
726
+ // | {
727
+ // node?: RouteNode;
728
+ // route?: undefined;
729
+ // }
730
+ // );
731
+ const useFlowerNavigate = ({ flowName, action, route, node }) => {
732
+ const { flowName: flowNameContext } = React.useContext(context);
733
+ const name = flowName || flowNameContext;
734
+ const { next, jump, back, reset, restart } = useFlower({ flowName: name });
735
+ const onNavigate = React.useCallback(() => {
736
+ switch (action) {
737
+ case 'next':
738
+ next(route);
739
+ return;
740
+ case 'jump':
741
+ jump(node);
742
+ return;
743
+ case 'back':
744
+ back(node);
745
+ return;
746
+ case 'reset':
747
+ reset(node);
748
+ return;
749
+ case 'restart':
750
+ restart(node);
751
+ return;
752
+ default:
753
+ next();
754
+ return;
755
+ }
756
+ }, [next, jump, back, reset, restart, node, route]);
757
+ return {
758
+ onNavigate,
759
+ flowName
760
+ };
980
761
  };
981
762
 
982
- const _excluded$1 = ["hidden", "Component", "onNavigate"];
763
+ /* eslint-disable */
983
764
  function isIntrinsicElement(x) {
984
- return typeof x === 'string';
765
+ return typeof x === 'string';
985
766
  }
986
- function FlowerNavigateWrapper(_ref) {
987
- let {
988
- hidden,
989
- Component,
990
- onNavigate
991
- } = _ref,
992
- props = _objectWithoutPropertiesLoose(_ref, _excluded$1);
993
- const newProps = React.useMemo(() => _extends({}, props, {
994
- hidden,
995
- onClick: onNavigate
996
- }), [props, onNavigate]);
997
- if (typeof Component === 'function') {
998
- return Component(newProps);
999
- }
1000
- if (isIntrinsicElement(Component)) {
1001
- return /*#__PURE__*/React.createElement(Component, _extends({}, props, {
1002
- onClick: onNavigate
1003
- }));
1004
- }
1005
- return Component && /*#__PURE__*/React.createElement(Component, _extends({}, newProps));
767
+ //TODO type FlowerNavigateWrapper props
768
+ function FlowerNavigateWrapper({ hidden, Component, onNavigate, ...props }) {
769
+ const newProps = React.useMemo(() => ({
770
+ ...props,
771
+ hidden,
772
+ onClick: onNavigate,
773
+ }), [props, onNavigate]);
774
+ if (typeof Component === 'function') {
775
+ return Component(newProps);
776
+ }
777
+ // TODO si arriva in questa condizione quando si passa un componente primitivo es. div
778
+ // in questo caso non posso props custom di flower
779
+ if (isIntrinsicElement(Component)) {
780
+ return React.createElement(Component, { ...props, onClick: onNavigate });
781
+ }
782
+ // TODO in questa condizione si arriva se nel progetto si utilizza Vite, in questo caso i componenti non sono Function ma Object,
783
+ // oppure nel caso di un testo semplice come children di questo componente
784
+ /* istanbul ignore next */
785
+ return Component && React.createElement(Component, { ...newProps });
1006
786
  }
1007
- const component$2 = /*#__PURE__*/React.memo(FlowerNavigateWrapper);
787
+ const component$2 = React.memo(FlowerNavigateWrapper);
1008
788
 
1009
- const _excluded = ["alwaysDisplay", "rules", "Component", "flowName", "onNavigate"];
1010
- const RenderRules = _ref => {
1011
- let {
1012
- alwaysDisplay,
1013
- rules,
1014
- Component,
1015
- flowName,
1016
- onNavigate
1017
- } = _ref,
1018
- props = _objectWithoutPropertiesLoose(_ref, _excluded);
1019
- return /*#__PURE__*/React.createElement(component$5, {
1020
- alwaysDisplay: alwaysDisplay,
1021
- rules: rules,
1022
- flowName: flowName
1023
- }, ({
1024
- hidden
1025
- }) => /*#__PURE__*/React.createElement(component$2, _extends({}, props, {
1026
- Component: Component,
1027
- hidden: hidden,
1028
- onNavigate: onNavigate
1029
- })));
789
+ /* eslint-disable */
790
+ //TODO type RenderRules props
791
+ const RenderRules = ({ alwaysDisplay, rules, Component, flowName, onNavigate, ...props }) => {
792
+ return (React.createElement(component$5, { alwaysDisplay: alwaysDisplay, rules: rules, flowName: flowName }, ({ hidden }) => React.createElement(component$2, { ...props, Component: Component, hidden: hidden, onNavigate: onNavigate })));
1030
793
  };
1031
- const FlowerNavigate = ({
1032
- children,
1033
- flowName: forceFlowName,
1034
- action,
1035
- route,
1036
- node,
1037
- rules,
1038
- alwaysDisplay
1039
- }) => {
1040
- const {
1041
- onNavigate,
1042
- flowName
1043
- } = useFlowerNavigate({
1044
- flowName: forceFlowName,
1045
- action,
1046
- route,
1047
- node
1048
- });
1049
- if (typeof children === 'function') {
1050
- return /*#__PURE__*/React.createElement(RenderRules, {
1051
- alwaysDisplay: alwaysDisplay,
1052
- rules: rules,
1053
- Component: children,
1054
- flowName: flowName,
1055
- onNavigate: onNavigate
794
+ const FlowerNavigate = ({ children, flowName: forceFlowName, action, route, node, rules, alwaysDisplay, }) => {
795
+ const { onNavigate, flowName } = useFlowerNavigate({ flowName: forceFlowName, action, route, node });
796
+ if (typeof children === 'function') {
797
+ return React.createElement(RenderRules, { alwaysDisplay: alwaysDisplay, rules: rules, Component: children, flowName: flowName, onNavigate: onNavigate });
798
+ }
799
+ return React.Children.map(children, (child, i) => {
800
+ if (!React.isValidElement(child))
801
+ return child;
802
+ const { type, props } = child;
803
+ const Component = type;
804
+ // eslint-disable-next-line react/jsx-props-no-spreading
805
+ return React.createElement(RenderRules, { key: i, alwaysDisplay: alwaysDisplay, rules: rules, Component: Component, flowName: flowName, onNavigate: onNavigate, ...props });
1056
806
  });
1057
- }
1058
- return React.Children.map(children, (child, i) => {
1059
- if (! /*#__PURE__*/React.isValidElement(child)) return child;
1060
- const {
1061
- type,
1062
- props
1063
- } = child;
1064
- const Component = type;
1065
- return /*#__PURE__*/React.createElement(RenderRules, _extends({
1066
- key: i,
1067
- alwaysDisplay: alwaysDisplay,
1068
- rules: rules,
1069
- Component: Component,
1070
- flowName: flowName,
1071
- onNavigate: onNavigate
1072
- }, props));
1073
- });
1074
807
  };
1075
- const component$1 = /*#__PURE__*/React.memo(FlowerNavigate);
808
+ const component$1 = React.memo(FlowerNavigate);
1076
809
  component$1.displayName = 'FlowerNavigate';
1077
810
 
1078
- const FlowerComponent = ({
1079
- children
1080
- }) => children;
1081
- const component = /*#__PURE__*/React.memo(FlowerComponent);
811
+ const FlowerComponent = ({ children }) => children;
812
+ const component = React.memo(FlowerComponent);
1082
813
 
1083
- const useFlowerForm = ({
1084
- flowName: customFlowName,
1085
- name
1086
- } = {}) => {
1087
- const {
1088
- flowName: flowNameDefault
1089
- } = React.useContext(context);
1090
- const dispatch = useDispatch();
1091
- const store = useStore();
1092
- const flowName = customFlowName || name || flowNameDefault || '';
1093
- const currentNode = useSelector(makeSelectCurrentNodeId(flowName));
1094
- const {
1095
- errors,
1096
- isValid,
1097
- touched,
1098
- isValidating
1099
- } = useSelector(makeSelectNodeErrors(flowName, currentNode));
1100
- const getData = React.useCallback(path => {
1101
- const {
1102
- flowNameFromPath = flowName,
1103
- path: newpath
1104
- } = flowerCore.CoreUtils.getPath(path);
1105
- return _get(store.getState(), ['flower', flowNameFromPath, 'data', ...newpath]);
1106
- }, [store, flowName]);
1107
- const setData = React.useCallback((val, path) => {
1108
- if (path) {
1109
- const {
1110
- flowNameFromPath = flowName,
1111
- path: newpath
1112
- } = flowerCore.CoreUtils.getPath(path);
1113
- dispatch(actions.addDataByPath({
1114
- flowName: flowNameFromPath,
1115
- id: Array.isArray(newpath) ? newpath : [newpath],
1116
- value: val
1117
- }));
1118
- return;
1119
- }
1120
- dispatch(actions.addData({
1121
- flowName,
1122
- value: val
1123
- }));
1124
- }, [flowName, dispatch]);
1125
- const unsetData = React.useCallback(path => {
1126
- const {
1127
- flowNameFromPath = flowName,
1128
- path: newpath
1129
- } = flowerCore.CoreUtils.getPath(path);
1130
- dispatch(actions.unsetData({
1131
- flowName: flowNameFromPath,
1132
- id: newpath
1133
- }));
1134
- }, [flowName, dispatch]);
1135
- const replaceData = React.useCallback(val => {
1136
- dispatch(actions.replaceData({
1137
- flowName,
1138
- value: val
1139
- }));
1140
- }, [flowName, dispatch]);
1141
- return {
1142
- touched,
1143
- errors,
1144
- isValid,
1145
- isValidating,
1146
- getData,
1147
- setData,
1148
- unsetData,
1149
- replaceData
1150
- };
814
+ /** This hook allows you to manage and retrieve information about Forms.
815
+ *
816
+ * It exposes details regarding the form's state and a set of methods for reading and writing within it:
817
+ *
818
+ * - getData
819
+ *
820
+ * - setData
821
+ *
822
+ * - unSetData
823
+ *
824
+ * - replaceData
825
+ *
826
+ * @param {string} flowName - first optional parameter
827
+ *
828
+ * @param {string} name - optional parameter, if flowName exist, name is not used
829
+ *
830
+ */
831
+ const useFlowerForm = ({ flowName: customFlowName, name } = {}) => {
832
+ const { flowName: flowNameDefault } = React.useContext(context);
833
+ const dispatch = useDispatch();
834
+ const store = useStore();
835
+ const flowName = customFlowName || name || flowNameDefault || '';
836
+ const currentNode = useSelector(makeSelectCurrentNodeId(flowName));
837
+ const { errors, isValid, touched, isValidating } = useSelector(makeSelectNodeErrors(flowName, currentNode));
838
+ const getData = React.useCallback((path) => {
839
+ const { flowNameFromPath = flowName, path: newpath } = flowerCore.CoreUtils.getPath(path);
840
+ return _get(store.getState(), [
841
+ 'flower',
842
+ flowNameFromPath,
843
+ 'data',
844
+ ...newpath
845
+ ]);
846
+ }, [store, flowName]);
847
+ const setData = React.useCallback((val, path) => {
848
+ if (path) {
849
+ const { flowNameFromPath = flowName, path: newpath } = flowerCore.CoreUtils.getPath(path);
850
+ dispatch(actions.addDataByPath({
851
+ flowName: flowNameFromPath,
852
+ id: Array.isArray(newpath) ? newpath : [newpath],
853
+ value: val
854
+ }));
855
+ return;
856
+ }
857
+ dispatch(actions.addData({ flowName, value: val }));
858
+ }, [flowName, dispatch]);
859
+ const unsetData = React.useCallback((path) => {
860
+ const { flowNameFromPath = flowName, path: newpath } = flowerCore.CoreUtils.getPath(path);
861
+ dispatch(actions.unsetData({ flowName: flowNameFromPath, id: newpath }));
862
+ }, [flowName, dispatch]);
863
+ const replaceData = React.useCallback((val) => {
864
+ dispatch(actions.replaceData({ flowName, value: val }));
865
+ }, [flowName, dispatch]);
866
+ return {
867
+ touched,
868
+ errors,
869
+ isValid,
870
+ isValidating,
871
+ getData,
872
+ setData,
873
+ unsetData,
874
+ replaceData
875
+ };
1151
876
  };
1152
877
 
1153
878
  exports.Flower = component$c;