@fairys/rn-valtio-form-basic 1.0.1 → 1.0.3

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/esm/form.item.js CHANGED
@@ -3,7 +3,7 @@ import { Text, View } from "react-native";
3
3
  import { Fragment } from "react";
4
4
  import { FairysValtioFormParentAttrsContext, useFairysValtioFormInstanceContextHideState } from "@fairys/valtio-form-basic/esm/common";
5
5
  import { formItemStyles } from "./styles/form.item.js";
6
- import { useFairysValtioFormItemAttrs, useFairysValtioFormItemNoStyleAttrs } from "./hooks/form.item.js";
6
+ import { useFairysValtioFormItemAttrs, useFairysValtioFormItemHideAttrs, useFairysValtioFormItemNoStyleAttrs } from "./hooks/form.item.js";
7
7
  function FairysRNValtioFormItemBase(props) {
8
8
  const { label, extra, helpText } = props;
9
9
  const { itemStyle, itemContainerStyle, itemLabelStyle, itemLabelTextStyle, itemLabelShowColonStyle, itemExtraStyle, itemInputStyle, itemBodyStyle, isInvalid, isRequired, showColon, formAttrsNameInstance, itemBorderType, errorStyle, helpStyle, error, children } = useFairysValtioFormItemAttrs(props);
@@ -74,10 +74,16 @@ function FairysRNValtioFormItemBase(props) {
74
74
  ]
75
75
  });
76
76
  }
77
+ function FairysRNValtioFormHideItemEmpty(props) {
78
+ useFairysValtioFormItemHideAttrs(props);
79
+ return /*#__PURE__*/ jsx(Fragment, {});
80
+ }
77
81
  function FairysRNValtioFormHideItem(props) {
78
82
  const [state] = useFairysValtioFormInstanceContextHideState();
79
83
  const isHide = state[props.name];
80
- if (isHide) return /*#__PURE__*/ jsx(Fragment, {});
84
+ if (isHide) return /*#__PURE__*/ jsx(FairysRNValtioFormHideItemEmpty, {
85
+ ...props
86
+ });
81
87
  return /*#__PURE__*/ jsx(FairysRNValtioFormItemBase, {
82
88
  ...props
83
89
  });
@@ -117,3 +117,7 @@ export interface FairysValtioFormItemNoStyleAttrsReturn<T extends MObject<T> = R
117
117
  /**子元素*/
118
118
  children?: React.ReactNode;
119
119
  }
120
+ /**
121
+ * 处理表单表单项属性,隐藏表单项时移除数据值
122
+ */
123
+ export declare function useFairysValtioFormItemHideAttrs<T extends MObject<T> = Record<string, any>>(props: FairysValtioFormItemAttrsProps<T>): void;
@@ -16,7 +16,7 @@ function useFairysValtioFormItemAttrs(props) {
16
16
  const parent_isInvalidBorderRed = layoutAttrs.isInvalidBorderRed;
17
17
  const parent_isInvalidTextRed = layoutAttrs.isInvalidTextRed;
18
18
  const parent_showColon = layoutAttrs.showColon;
19
- const { name, valuePropName = 'value', getValuePath = 'text', getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', style, labelStyle, bodyStyle, children, labelMode = parent_labelMode, errorLayout = parent_errorLayout, colSpan = 1, isRequired: _isRequired, itemBorderType = parent_borderedType, attrs = {}, showColon = parent_showColon, itemBorderColor = parent_itemBorderColor, isInvalidBorderRed = parent_isInvalidBorderRed, isInvalidTextRed = parent_isInvalidTextRed, isJoinParentField = true, rules, isRemoveValueOnUnmount = true } = props;
19
+ const { name, valuePropName = 'value', getValuePath = 'text', getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', style, labelStyle, bodyStyle, children, labelMode = parent_labelMode, errorLayout = parent_errorLayout, colSpan = 1, isRequired: _isRequired, itemBorderType = parent_borderedType, attrs = {}, showColon = parent_showColon, itemBorderColor = parent_itemBorderColor, isInvalidBorderRed = parent_isInvalidBorderRed, isInvalidTextRed = parent_isInvalidTextRed, isJoinParentField = true, rules, isRemoveValueOnUnmount = false } = props;
20
20
  const { name: _name, paths, parentName, formAttrsNameInstance } = useFairysValtioFormAttrsName({
21
21
  name,
22
22
  isJoinParentField
@@ -216,7 +216,7 @@ function useFairysValtioFormItemAttrs(props) {
216
216
  };
217
217
  }
218
218
  function useFairysValtioFormItemNoStyleAttrs(props) {
219
- const { name, valuePropName = 'value', getValuePath = 'text', getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', children, attrs = {}, isJoinParentField = true, rules, isRequired: _isRequired, isRemoveValueOnUnmount = true } = props;
219
+ const { name, valuePropName = 'value', getValuePath = 'text', getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', children, attrs = {}, isJoinParentField = true, rules, isRequired: _isRequired, isRemoveValueOnUnmount = false } = props;
220
220
  const [state, errorState, formInstance] = useFairysValtioFormInstanceContextState();
221
221
  const { name: _name, paths, parentName, formAttrsNameInstance } = useFairysValtioFormAttrsName({
222
222
  name,
@@ -295,4 +295,15 @@ function useFairysValtioFormItemNoStyleAttrs(props) {
295
295
  }) : children
296
296
  };
297
297
  }
298
- export { useFairysValtioFormItemAttrs, useFairysValtioFormItemNoStyleAttrs };
298
+ function useFairysValtioFormItemHideAttrs(props) {
299
+ const { name, isJoinParentField = true, isHideRemoveValue = true } = props;
300
+ const [, , formInstance] = useFairysValtioFormInstanceContextState();
301
+ const { name: _name } = useFairysValtioFormAttrsName({
302
+ name,
303
+ isJoinParentField
304
+ });
305
+ useEffect(()=>()=>{
306
+ if (isHideRemoveValue) formInstance.removeValueByPaths(_name);
307
+ }, []);
308
+ }
309
+ export { useFairysValtioFormItemAttrs, useFairysValtioFormItemHideAttrs, useFairysValtioFormItemNoStyleAttrs };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "author": "SunLxy <1011771396@qq.com>",
4
4
  "description": "rn表单框架组件",
5
5
  "homepage": "https://github.com/autumn-fairy-tales/valtio-form-basic",
6
- "version": "1.0.1",
6
+ "version": "1.0.3",
7
7
  "main": "esm/index.js",
8
8
  "types": "esm/index.d.ts",
9
9
  "module": "esm/index.js",
@@ -25,7 +25,7 @@
25
25
  "esm"
26
26
  ],
27
27
  "dependencies": {
28
- "@fairys/valtio-form-basic": "^1.0.1"
28
+ "@fairys/valtio-form-basic": "^1.0.3"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@types/react": "~18.2.21",
package/src/form.item.tsx CHANGED
@@ -10,7 +10,11 @@ import {
10
10
  } from '@fairys/valtio-form-basic/esm/common';
11
11
  import { formItemStyles } from './styles/form.item';
12
12
  import type { FairysValtioFormItemAttrsProps } from './hooks/form.item';
13
- import { useFairysValtioFormItemAttrs, useFairysValtioFormItemNoStyleAttrs } from 'hooks/form.item';
13
+ import {
14
+ useFairysValtioFormItemAttrs,
15
+ useFairysValtioFormItemNoStyleAttrs,
16
+ useFairysValtioFormItemHideAttrs,
17
+ } from 'hooks/form.item';
14
18
 
15
19
  export interface FairysRNValtioFormItemProps<T extends MObject<T> = Record<string, any>>
16
20
  extends Omit<ViewProps, 'style'>,
@@ -98,6 +102,15 @@ export function FairysRNValtioFormItemBase<T extends MObject<T> = Record<string,
98
102
  </View>
99
103
  );
100
104
  }
105
+
106
+ /**隐藏表单项的空组件*/
107
+ function FairysRNValtioFormHideItemEmpty<T extends MObject<T> = Record<string, any>>(
108
+ props: Omit<FairysRNValtioFormItemProps<T>, 'isHide' | 'noStyle'>,
109
+ ) {
110
+ useFairysValtioFormItemHideAttrs(props);
111
+ return <Fragment />;
112
+ }
113
+
101
114
  /**控制隐藏的表单项*/
102
115
  export function FairysRNValtioFormHideItem<T extends MObject<T> = Record<string, any>>(
103
116
  props: Omit<FairysRNValtioFormItemProps<T>, 'isHide' | 'noStyle'>,
@@ -105,7 +118,7 @@ export function FairysRNValtioFormHideItem<T extends MObject<T> = Record<string,
105
118
  const [state] = useFairysValtioFormInstanceContextHideState();
106
119
  const isHide = state[props.name];
107
120
  if (isHide) {
108
- return <Fragment />;
121
+ return <FairysRNValtioFormHideItemEmpty<T> {...props} />;
109
122
  }
110
123
  return <FairysRNValtioFormItemBase<T> {...props} />;
111
124
  }
@@ -75,7 +75,7 @@ export function useFairysValtioFormItemAttrs<T extends MObject<T> = Record<strin
75
75
  isInvalidTextRed = parent_isInvalidTextRed,
76
76
  isJoinParentField = true,
77
77
  rules,
78
- isRemoveValueOnUnmount = true,
78
+ isRemoveValueOnUnmount = false,
79
79
  } = props;
80
80
  const {
81
81
  name: _name,
@@ -382,7 +382,7 @@ export function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = Recor
382
382
  isJoinParentField = true,
383
383
  rules,
384
384
  isRequired: _isRequired,
385
- isRemoveValueOnUnmount = true,
385
+ isRemoveValueOnUnmount = false,
386
386
  } = props;
387
387
  const [state, errorState, formInstance] = useFairysValtioFormInstanceContextState<T>();
388
388
  const {
@@ -511,3 +511,21 @@ export interface FairysValtioFormItemNoStyleAttrsReturn<T extends MObject<T> = R
511
511
  /**子元素*/
512
512
  children?: React.ReactNode;
513
513
  }
514
+
515
+ /**
516
+ * 处理表单表单项属性,隐藏表单项时移除数据值
517
+ */
518
+ export function useFairysValtioFormItemHideAttrs<T extends MObject<T> = Record<string, any>>(
519
+ props: FairysValtioFormItemAttrsProps<T>,
520
+ ) {
521
+ const { name, isJoinParentField = true, isHideRemoveValue = true } = props;
522
+ const [, , formInstance] = useFairysValtioFormInstanceContextState<T>();
523
+ const { name: _name } = useFairysValtioFormAttrsName({ name, isJoinParentField });
524
+ useEffect(() => {
525
+ return () => {
526
+ if (isHideRemoveValue) {
527
+ formInstance.removeValueByPaths(_name);
528
+ }
529
+ };
530
+ }, []);
531
+ }