@flowerforce/flower-react 3.5.1-beta.0 → 3.5.1-beta.2

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
@@ -408,19 +408,19 @@ function isIntrinsicElement$1(x) {
408
408
  return typeof x === 'string';
409
409
  }
410
410
  //TODO make types for wrapper function props
411
- function Wrapper$1({ Component, id, flowName, currentNode, validate, asyncDebounce = 0, asyncValidate, asyncInitialError, asyncWaitingError, destroyValue, onBlur, onFocus, hidden, onUpdate, defaultValue, ...props }) {
411
+ function Wrapper$1({ Component, id, flowName, currentNode, validate, asyncDebounce = 0, asyncValidate, asyncInitialError, asyncWaitingError, destroyValue, destroyOnHide, onBlur, onFocus, hidden, onUpdate, defaultValue, ...props }) {
412
412
  const dispatch = useDispatch();
413
- const [customAsyncErrors, setCustomAsyncErrors] = React.useState(!hidden && asyncValidate && [asyncInitialError]);
413
+ const [customAsyncErrors, setCustomAsyncErrors] = React.useState(asyncValidate && asyncInitialError && [asyncInitialError]);
414
414
  const [isValidating, setIsValidating] = React.useState(undefined);
415
415
  const { flowNameFromPath = flowName, path } = React.useMemo(() => flowerCore.CoreUtils.getPath(id), [id]);
416
416
  const value = useSelector(getDataFromState(flowNameFromPath, path));
417
- const errors = useSelector(makeSelectFieldError(flowName, id, hidden ? [] : validate), flowerCore.CoreUtils.allEqual);
417
+ const errors = useSelector(makeSelectFieldError(flowName, id, validate), flowerCore.CoreUtils.allEqual);
418
418
  const dirty = useSelector(makeSelectNodeFieldDirty(flowName, currentNode, id));
419
419
  const touched = useSelector(makeSelectNodeFieldTouched(flowName, currentNode, id));
420
420
  const focused = useSelector(makeSelectNodeFieldFocused(flowName, currentNode, id));
421
421
  const refValue = React.useRef();
422
422
  const isSubmitted = useSelector(makeSelectNodeFormSubmitted(flowName, currentNode));
423
- const allErrors = React.useMemo(() => [...errors, ...(customAsyncErrors || []).filter(Boolean)], [errors, customAsyncErrors]);
423
+ const allErrors = React.useMemo(() => hidden ? [] : [...errors, ...(customAsyncErrors || []).filter(Boolean)], [errors, hidden, customAsyncErrors]);
424
424
  const setTouched = React.useCallback((touched) => {
425
425
  dispatch({
426
426
  type: 'flower/formFieldTouch',
@@ -488,7 +488,7 @@ function Wrapper$1({ Component, id, flowName, currentNode, validate, asyncDeboun
488
488
  refValue.current = value;
489
489
  const hasValue = !flowerCore.MatchRules.utils.isEmpty(value);
490
490
  if (!hasValue) {
491
- setCustomAsyncErrors([asyncInitialError]);
491
+ setCustomAsyncErrors(asyncInitialError && [asyncInitialError]);
492
492
  setIsValidating(false);
493
493
  return;
494
494
  }
@@ -522,6 +522,34 @@ function Wrapper$1({ Component, id, flowName, currentNode, validate, asyncDeboun
522
522
  }
523
523
  });
524
524
  }, [flowName, currentNode, isValidating]);
525
+ const resetField = React.useCallback(() => {
526
+ dispatch({
527
+ type: 'flower/formFieldTouch',
528
+ payload: {
529
+ name: flowName,
530
+ id,
531
+ currentNode,
532
+ touched: false
533
+ }
534
+ });
535
+ dispatch({
536
+ type: 'flower/formFieldDirty',
537
+ payload: {
538
+ name: flowName,
539
+ id,
540
+ currentNode,
541
+ dirty: false
542
+ }
543
+ });
544
+ dispatch({
545
+ type: 'flower/formRemoveErrors',
546
+ payload: {
547
+ name: flowName,
548
+ id,
549
+ currentNode
550
+ }
551
+ });
552
+ }, [currentNode, id, flowName]);
525
553
  React.useEffect(() => {
526
554
  // destroy
527
555
  return () => {
@@ -531,16 +559,20 @@ function Wrapper$1({ Component, id, flowName, currentNode, validate, asyncDeboun
531
559
  payload: { flowName: flowNameFromPath, id: path }
532
560
  });
533
561
  }
534
- dispatch({
535
- type: 'flower/formRemoveErrors',
536
- payload: {
537
- name: flowName,
538
- id,
539
- currentNode
540
- }
541
- });
562
+ resetField();
542
563
  };
543
- }, [destroyValue, id, flowNameFromPath, path, currentNode]);
564
+ }, [destroyValue, id, flowNameFromPath, path, resetField]);
565
+ React.useEffect(() => {
566
+ if (hidden) {
567
+ if (destroyOnHide) {
568
+ dispatch({
569
+ type: `flower/unsetData`,
570
+ payload: { flowName: flowNameFromPath, id: path }
571
+ });
572
+ resetField();
573
+ }
574
+ }
575
+ }, [destroyOnHide, hidden, flowNameFromPath, path, resetField]);
544
576
  React.useEffect(() => {
545
577
  if (defaultValue && !dirty && !isEqual(value, defaultValue)) {
546
578
  onChange(defaultValue);
@@ -586,11 +618,11 @@ function Wrapper$1({ Component, id, flowName, currentNode, validate, asyncDeboun
586
618
  }
587
619
  return Component && React.createElement(Component, { ...newProps });
588
620
  }
589
- const FlowerField = ({ id, validate, asyncValidate, asyncDebounce, asyncInitialError, asyncWaitingError, rules, alwaysDisplay, value, children, defaultValue, destroyValue, flowName, onUpdate }) => {
621
+ const FlowerField = ({ id, validate, asyncValidate, asyncDebounce, asyncInitialError, asyncWaitingError, rules, alwaysDisplay, value, children, defaultValue, destroyOnHide, destroyValue, flowName, onUpdate }) => {
590
622
  const { flowName: flowNameContext, currentNode } = React.useContext(context);
591
623
  const name = flowName || flowNameContext;
592
624
  if (typeof children === 'function') {
593
- 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 }))));
625
+ 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, destroyOnHide: destroyOnHide }))));
594
626
  }
595
627
  return React.Children.map(children, (child, i) => {
596
628
  if (!React.isValidElement(child)) {
package/dist/index.esm.js CHANGED
@@ -406,19 +406,19 @@ function isIntrinsicElement$1(x) {
406
406
  return typeof x === 'string';
407
407
  }
408
408
  //TODO make types for wrapper function props
409
- function Wrapper$1({ Component, id, flowName, currentNode, validate, asyncDebounce = 0, asyncValidate, asyncInitialError, asyncWaitingError, destroyValue, onBlur, onFocus, hidden, onUpdate, defaultValue, ...props }) {
409
+ function Wrapper$1({ Component, id, flowName, currentNode, validate, asyncDebounce = 0, asyncValidate, asyncInitialError, asyncWaitingError, destroyValue, destroyOnHide, onBlur, onFocus, hidden, onUpdate, defaultValue, ...props }) {
410
410
  const dispatch = useDispatch();
411
- const [customAsyncErrors, setCustomAsyncErrors] = useState(!hidden && asyncValidate && [asyncInitialError]);
411
+ const [customAsyncErrors, setCustomAsyncErrors] = useState(asyncValidate && asyncInitialError && [asyncInitialError]);
412
412
  const [isValidating, setIsValidating] = useState(undefined);
413
413
  const { flowNameFromPath = flowName, path } = useMemo(() => CoreUtils.getPath(id), [id]);
414
414
  const value = useSelector(getDataFromState(flowNameFromPath, path));
415
- const errors = useSelector(makeSelectFieldError(flowName, id, hidden ? [] : validate), CoreUtils.allEqual);
415
+ const errors = useSelector(makeSelectFieldError(flowName, id, validate), CoreUtils.allEqual);
416
416
  const dirty = useSelector(makeSelectNodeFieldDirty(flowName, currentNode, id));
417
417
  const touched = useSelector(makeSelectNodeFieldTouched(flowName, currentNode, id));
418
418
  const focused = useSelector(makeSelectNodeFieldFocused(flowName, currentNode, id));
419
419
  const refValue = useRef();
420
420
  const isSubmitted = useSelector(makeSelectNodeFormSubmitted(flowName, currentNode));
421
- const allErrors = useMemo(() => [...errors, ...(customAsyncErrors || []).filter(Boolean)], [errors, customAsyncErrors]);
421
+ const allErrors = useMemo(() => hidden ? [] : [...errors, ...(customAsyncErrors || []).filter(Boolean)], [errors, hidden, customAsyncErrors]);
422
422
  const setTouched = useCallback((touched) => {
423
423
  dispatch({
424
424
  type: 'flower/formFieldTouch',
@@ -486,7 +486,7 @@ function Wrapper$1({ Component, id, flowName, currentNode, validate, asyncDeboun
486
486
  refValue.current = value;
487
487
  const hasValue = !MatchRules.utils.isEmpty(value);
488
488
  if (!hasValue) {
489
- setCustomAsyncErrors([asyncInitialError]);
489
+ setCustomAsyncErrors(asyncInitialError && [asyncInitialError]);
490
490
  setIsValidating(false);
491
491
  return;
492
492
  }
@@ -520,6 +520,34 @@ function Wrapper$1({ Component, id, flowName, currentNode, validate, asyncDeboun
520
520
  }
521
521
  });
522
522
  }, [flowName, currentNode, isValidating]);
523
+ const resetField = useCallback(() => {
524
+ dispatch({
525
+ type: 'flower/formFieldTouch',
526
+ payload: {
527
+ name: flowName,
528
+ id,
529
+ currentNode,
530
+ touched: false
531
+ }
532
+ });
533
+ dispatch({
534
+ type: 'flower/formFieldDirty',
535
+ payload: {
536
+ name: flowName,
537
+ id,
538
+ currentNode,
539
+ dirty: false
540
+ }
541
+ });
542
+ dispatch({
543
+ type: 'flower/formRemoveErrors',
544
+ payload: {
545
+ name: flowName,
546
+ id,
547
+ currentNode
548
+ }
549
+ });
550
+ }, [currentNode, id, flowName]);
523
551
  useEffect(() => {
524
552
  // destroy
525
553
  return () => {
@@ -529,16 +557,20 @@ function Wrapper$1({ Component, id, flowName, currentNode, validate, asyncDeboun
529
557
  payload: { flowName: flowNameFromPath, id: path }
530
558
  });
531
559
  }
532
- dispatch({
533
- type: 'flower/formRemoveErrors',
534
- payload: {
535
- name: flowName,
536
- id,
537
- currentNode
538
- }
539
- });
560
+ resetField();
540
561
  };
541
- }, [destroyValue, id, flowNameFromPath, path, currentNode]);
562
+ }, [destroyValue, id, flowNameFromPath, path, resetField]);
563
+ useEffect(() => {
564
+ if (hidden) {
565
+ if (destroyOnHide) {
566
+ dispatch({
567
+ type: `flower/unsetData`,
568
+ payload: { flowName: flowNameFromPath, id: path }
569
+ });
570
+ resetField();
571
+ }
572
+ }
573
+ }, [destroyOnHide, hidden, flowNameFromPath, path, resetField]);
542
574
  useEffect(() => {
543
575
  if (defaultValue && !dirty && !isEqual(value, defaultValue)) {
544
576
  onChange(defaultValue);
@@ -584,11 +616,11 @@ function Wrapper$1({ Component, id, flowName, currentNode, validate, asyncDeboun
584
616
  }
585
617
  return Component && React.createElement(Component, { ...newProps });
586
618
  }
587
- const FlowerField = ({ id, validate, asyncValidate, asyncDebounce, asyncInitialError, asyncWaitingError, rules, alwaysDisplay, value, children, defaultValue, destroyValue, flowName, onUpdate }) => {
619
+ const FlowerField = ({ id, validate, asyncValidate, asyncDebounce, asyncInitialError, asyncWaitingError, rules, alwaysDisplay, value, children, defaultValue, destroyOnHide, destroyValue, flowName, onUpdate }) => {
588
620
  const { flowName: flowNameContext, currentNode } = useContext(context);
589
621
  const name = flowName || flowNameContext;
590
622
  if (typeof children === 'function') {
591
- 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 }))));
623
+ 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, destroyOnHide: destroyOnHide }))));
592
624
  }
593
625
  return React.Children.map(children, (child, i) => {
594
626
  if (!React.isValidElement(child)) {
@@ -1,4 +1,4 @@
1
1
  import React from 'react';
2
2
  import { FlowerFieldProps } from './types/FlowerField';
3
- declare const component: React.MemoExoticComponent<({ id, validate, asyncValidate, asyncDebounce, asyncInitialError, asyncWaitingError, rules, alwaysDisplay, value, children, defaultValue, destroyValue, flowName, onUpdate }: FlowerFieldProps) => React.JSX.Element | (string | number | Iterable<React.ReactNode> | React.JSX.Element)[] | null | undefined>;
3
+ declare const component: React.MemoExoticComponent<({ id, validate, asyncValidate, asyncDebounce, asyncInitialError, asyncWaitingError, rules, alwaysDisplay, value, children, defaultValue, destroyOnHide, destroyValue, flowName, onUpdate }: FlowerFieldProps) => React.JSX.Element | (string | number | Iterable<React.ReactNode> | React.JSX.Element)[] | null | undefined>;
4
4
  export default component;
@@ -99,7 +99,10 @@ export type FlowerFieldProps<T extends Record<string, any> = Record<string, any>
99
99
  flowName?: string;
100
100
  /** Initial value field */
101
101
  defaultValue?: unknown;
102
+ /** Remove value from data on destroy element */
102
103
  destroyValue?: boolean;
104
+ /** Remove value from data on hide element */
105
+ destroyOnHide?: boolean;
103
106
  value?: any;
104
107
  /** When set to true, the children is shown even if the rules are not satisfied
105
108
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowerforce/flower-react",
3
- "version": "3.5.1-beta.0",
3
+ "version": "3.5.1-beta.2",
4
4
  "description": "FlowerJS components, hooks and utils for React.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -34,7 +34,7 @@
34
34
  "typescript": "^5.4.5"
35
35
  },
36
36
  "dependencies": {
37
- "@flowerforce/flower-core": "3.3.1-beta.0",
37
+ "@flowerforce/flower-core": "3.3.1-beta.2",
38
38
  "@reduxjs/toolkit": "^2.2.4",
39
39
  "lodash": "^4.17.21",
40
40
  "react-redux": "^9.1.2",