@fairys/rn-valtio-form-basic 0.0.12

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/README.md ADDED
File without changes
package/esm/form.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ import { FairysRNValtioFormItem, FairysRNValtioFormHideItem, FairysRNValtioFormItemBase } from './form.item';
2
+ import { useFairysValtioFormInstance, useFairysValtioFormInstanceContext, useFairysValtioFormInstanceContextState, useFairysValtioFormInstanceContextHideState } from '@fairys/valtio-form-basic/esm/common';
3
+ import type { MObject } from '@fairys/valtio-form-basic/esm/common';
4
+ import type { FairysValtioFormAttrsProps } from './hooks/form';
5
+ export interface FairysRNValtioFormProps<T extends MObject<T> = object> extends FairysValtioFormAttrsProps<T> {
6
+ }
7
+ export declare function FairysRNValtioForm<T extends MObject<T> = object>(props: FairysRNValtioFormProps<T>): import("react/jsx-runtime").JSX.Element;
8
+ export declare namespace FairysRNValtioForm {
9
+ var useForm: typeof useFairysValtioFormInstance;
10
+ var useFormState: typeof useFairysValtioFormInstanceContextState;
11
+ var useFormHideState: typeof useFairysValtioFormInstanceContextHideState;
12
+ var useFormInstance: typeof useFairysValtioFormInstanceContext;
13
+ var FormItemBase: typeof FairysRNValtioFormItemBase;
14
+ var FormItem: typeof FairysRNValtioFormItem;
15
+ var FormHideItem: typeof FairysRNValtioFormHideItem;
16
+ }
@@ -0,0 +1,18 @@
1
+ /**表单项*/
2
+ import type { ViewProps } from 'react-native';
3
+ import type { MObject } from '@fairys/valtio-form-basic';
4
+ import type { FairysValtioFormItemAttrsProps } from './hooks/form.item';
5
+ export interface FairysRNValtioFormItemProps<T extends MObject<T> = object> extends Omit<ViewProps, 'style'>, FairysValtioFormItemAttrsProps {
6
+ /**是否使用控制隐藏的表单项*/
7
+ isHide?: boolean;
8
+ /**是否使用无样式表单项*/
9
+ noStyle?: boolean;
10
+ }
11
+ /**普通表单项*/
12
+ export declare function FairysRNValtioFormItemBase<T extends MObject<T> = object>(props: Omit<FairysRNValtioFormItemProps<T>, 'isHide' | 'noStyle'>): import("react/jsx-runtime").JSX.Element;
13
+ /**控制隐藏的表单项*/
14
+ export declare function FairysRNValtioFormHideItem<T extends MObject<T> = object>(props: Omit<FairysRNValtioFormItemProps<T>, 'isHide' | 'noStyle'>): import("react/jsx-runtime").JSX.Element;
15
+ /**无样式表单项*/
16
+ export declare function FairysRNValtioFormItemNoStyle<T extends MObject<T> = object>(props: Omit<FairysRNValtioFormItemProps<T>, 'isHide' | 'noStyle'>): import("react/jsx-runtime").JSX.Element;
17
+ /**表单项基础组件(根据isHide和noStyle判断是否使用控制隐藏的表单项和无样式表单项)*/
18
+ export declare function FairysRNValtioFormItem<T extends MObject<T> = object>(props: FairysRNValtioFormItemProps<T>): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,104 @@
1
+ import { jsx, jsxs } from "react/jsx-runtime";
2
+ import { Text, View } from "react-native";
3
+ import { Fragment } from "react";
4
+ import { FairysValtioFormParentAttrsContext, useFairysValtioFormInstanceContextHideState } from "@fairys/valtio-form-basic/esm/common";
5
+ import { formItemStyles } from "./styles/form.item.js";
6
+ import { useFairysValtioFormItemAttrs, useFairysValtioFormItemNoStyleAttrs } from "./hooks/form.item.js";
7
+ function FairysRNValtioFormItemBase(props) {
8
+ const { label, extra, helpText } = props;
9
+ const { itemStyle, itemContainerStyle, itemLabelStyle, itemLabelTextStyle, itemLabelShowColonStyle, itemExtraStyle, itemInputStyle, itemBodyStyle, isInvalid, isRequired, showColon, formAttrsNameInstance, itemBorderType, errorStyle, helpStyle, error, children } = useFairysValtioFormItemAttrs(props);
10
+ return /*#__PURE__*/ jsxs(View, {
11
+ style: itemStyle,
12
+ children: [
13
+ /*#__PURE__*/ jsxs(View, {
14
+ style: itemContainerStyle,
15
+ children: [
16
+ label ? /*#__PURE__*/ jsxs(View, {
17
+ style: itemLabelStyle,
18
+ children: [
19
+ isRequired ? /*#__PURE__*/ jsx(Text, {
20
+ style: formItemStyles["fairys-valtio-form-item-label.required"],
21
+ children: "*"
22
+ }) : /*#__PURE__*/ jsx(Fragment, {}),
23
+ /*#__PURE__*/ jsx(Text, {
24
+ style: itemLabelTextStyle,
25
+ children: label
26
+ }),
27
+ showColon ? /*#__PURE__*/ jsx(Text, {
28
+ style: itemLabelShowColonStyle,
29
+ children: ":"
30
+ }) : /*#__PURE__*/ jsx(Fragment, {})
31
+ ]
32
+ }) : /*#__PURE__*/ jsx(Fragment, {}),
33
+ /*#__PURE__*/ jsxs(View, {
34
+ style: itemBodyStyle,
35
+ children: [
36
+ /*#__PURE__*/ jsx(FairysValtioFormParentAttrsContext.Provider, {
37
+ value: formAttrsNameInstance,
38
+ children: /*#__PURE__*/ jsx(View, {
39
+ style: itemInputStyle,
40
+ children: children
41
+ })
42
+ }),
43
+ extra ? /*#__PURE__*/ jsx(View, {
44
+ style: itemExtraStyle,
45
+ children: /*#__PURE__*/ jsx(Text, {
46
+ style: formItemStyles["fairys-valtio-form-item-body-extra-text"],
47
+ children: extra
48
+ })
49
+ }) : /*#__PURE__*/ jsx(Fragment, {}),
50
+ 'body' === itemBorderType && isInvalid ? /*#__PURE__*/ jsx(View, {
51
+ style: errorStyle,
52
+ children: /*#__PURE__*/ jsx(Text, {
53
+ style: formItemStyles["fairys-valtio-form-item-body-error-text"],
54
+ children: error
55
+ })
56
+ }) : /*#__PURE__*/ jsx(Fragment, {})
57
+ ]
58
+ })
59
+ ]
60
+ }),
61
+ helpText ? /*#__PURE__*/ jsx(View, {
62
+ style: helpStyle,
63
+ children: /*#__PURE__*/ jsx(Text, {
64
+ children: helpText
65
+ })
66
+ }) : /*#__PURE__*/ jsx(Fragment, {}),
67
+ isInvalid && 'body' !== itemBorderType ? /*#__PURE__*/ jsx(View, {
68
+ style: errorStyle,
69
+ children: /*#__PURE__*/ jsx(Text, {
70
+ style: formItemStyles["fairys-valtio-form-item-body-error-text"],
71
+ children: error
72
+ })
73
+ }) : /*#__PURE__*/ jsx(Fragment, {})
74
+ ]
75
+ });
76
+ }
77
+ function FairysRNValtioFormHideItem(props) {
78
+ const [state] = useFairysValtioFormInstanceContextHideState();
79
+ const isHide = state[props.name];
80
+ if (isHide) return /*#__PURE__*/ jsx(Fragment, {});
81
+ return /*#__PURE__*/ jsx(FairysRNValtioFormItemBase, {
82
+ ...props
83
+ });
84
+ }
85
+ function FairysRNValtioFormItemNoStyle(props) {
86
+ const { children, formAttrsNameInstance } = useFairysValtioFormItemNoStyleAttrs(props);
87
+ return /*#__PURE__*/ jsx(FairysValtioFormParentAttrsContext.Provider, {
88
+ value: formAttrsNameInstance,
89
+ children: children
90
+ });
91
+ }
92
+ function FairysRNValtioFormItem(props) {
93
+ const { isHide, noStyle, ...rest } = props;
94
+ if (isHide) return /*#__PURE__*/ jsx(FairysRNValtioFormHideItem, {
95
+ ...rest
96
+ });
97
+ if (noStyle) return /*#__PURE__*/ jsx(FairysRNValtioFormItemNoStyle, {
98
+ ...rest
99
+ });
100
+ return /*#__PURE__*/ jsx(FairysRNValtioFormItemBase, {
101
+ ...rest
102
+ });
103
+ }
104
+ export { FairysRNValtioFormHideItem, FairysRNValtioFormItem, FairysRNValtioFormItemBase, FairysRNValtioFormItemNoStyle };
package/esm/form.js ADDED
@@ -0,0 +1,23 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { FairysRNValtioFormLayout } from "./layout.js";
3
+ import { FairysRNValtioFormHideItem, FairysRNValtioFormItem, FairysRNValtioFormItemBase } from "./form.item.js";
4
+ import { FairysValtioFormInstanceContext, useFairysValtioFormInstance, useFairysValtioFormInstanceContext, useFairysValtioFormInstanceContextHideState, useFairysValtioFormInstanceContextState } from "@fairys/valtio-form-basic/esm/common";
5
+ import { useFairysValtioForm } from "./hooks/form.js";
6
+ function FairysRNValtioForm(props) {
7
+ const { formInstance, children, ...rest } = useFairysValtioForm(props);
8
+ return /*#__PURE__*/ jsx(FairysValtioFormInstanceContext.Provider, {
9
+ value: formInstance,
10
+ children: /*#__PURE__*/ jsx(FairysRNValtioFormLayout, {
11
+ ...rest,
12
+ children: children
13
+ })
14
+ });
15
+ }
16
+ FairysRNValtioForm.useForm = useFairysValtioFormInstance;
17
+ FairysRNValtioForm.useFormState = useFairysValtioFormInstanceContextState;
18
+ FairysRNValtioForm.useFormHideState = useFairysValtioFormInstanceContextHideState;
19
+ FairysRNValtioForm.useFormInstance = useFairysValtioFormInstanceContext;
20
+ FairysRNValtioForm.FormItemBase = FairysRNValtioFormItemBase;
21
+ FairysRNValtioForm.FormItem = FairysRNValtioFormItem;
22
+ FairysRNValtioForm.FormHideItem = FairysRNValtioFormHideItem;
23
+ export { FairysRNValtioForm };
@@ -0,0 +1,58 @@
1
+ import type { MObject } from '@fairys/valtio-form-basic/esm/common';
2
+ import { FairysValtioFormInstance } from '@fairys/valtio-form-basic/esm/common';
3
+ import { type ReactNode } from 'react';
4
+ import { FairysValtioFormLayoutAttrsProps } from './layout';
5
+ import { RuleItem } from 'async-validator';
6
+ export interface FairysValtioFormAttrsProps<T extends MObject<T> = object> extends FairysValtioFormLayoutAttrsProps {
7
+ /**表单实例*/
8
+ form?: FairysValtioFormInstance<T>;
9
+ /**子元素*/
10
+ children: ReactNode;
11
+ /**表单项规则(如果表单项没有指定规则,则使用全局规则,如果表单项指定规则,则使用表单项规则)*/
12
+ rules?: Record<PropertyKey, RuleItem[]>;
13
+ /**表单初始值*/
14
+ formData?: FairysValtioFormInstance<T>['state'];
15
+ /**表单隐藏状态*/
16
+ hideState?: FairysValtioFormInstance<T>['hideState'];
17
+ /**
18
+ * 初始化表单数据类型,默认值为 deepCopy
19
+ * - deepCopy:使用深度拷贝初始化表单数据
20
+ * - immutable:直接使用对象(注意:当传递的不是`valtio`的`proxy`对象时,会使用`valtio`中的`proxy`声明)
21
+ */
22
+ initFormDataType?: 'deepCopy' | 'immutable';
23
+ }
24
+ /**
25
+ * 表单属性处理
26
+ *
27
+ * @example
28
+ *
29
+ * ```tsx
30
+
31
+ * ```
32
+ */
33
+ export declare function useFairysValtioForm<T extends MObject<T> = object>(props: FairysValtioFormAttrsProps<T>): {
34
+ formInstance: FairysValtioFormInstance<T>;
35
+ /**子元素*/
36
+ children: ReactNode;
37
+ gap?: string | number;
38
+ title?: React.ReactNode;
39
+ extra?: React.ReactNode;
40
+ style?: import("react-native").ViewProps["style"];
41
+ headerStyle?: import("react-native").ViewProps["style"];
42
+ headerTextStyle?: import("react-native").ViewProps["style"];
43
+ headerExtraStyle?: import("react-native").ViewProps["style"];
44
+ bodyStyle?: import("react-native").ViewProps["style"];
45
+ bordered?: boolean;
46
+ formItemStyle?: import("react-native").ViewProps["style"];
47
+ formItemLabelStyle?: import("react-native").ViewProps["style"];
48
+ formItemBodyStyle?: import("react-native").ViewProps["style"];
49
+ platform?: "pc" | "rn" | "taro";
50
+ colCount?: number;
51
+ errorLayout?: "bottom-left" | "bottom-right" | "top-right" | "top-left" | "left-border-top" | "right-border-top";
52
+ labelMode?: "left" | "top" | "between";
53
+ itemBorderType?: "bottom" | "body" | "none";
54
+ itemBorderColor?: React.CSSProperties["borderColor"];
55
+ isInvalidBorderRed?: boolean;
56
+ isInvalidTextRed?: boolean;
57
+ showColon?: boolean;
58
+ };
@@ -0,0 +1,118 @@
1
+ /**表单项*/
2
+ import React from 'react';
3
+ import { ViewProps } from 'react-native';
4
+ import type { MObject, FairysValtioFormItemAttrsProps as _FairysValtioFormItemAttrsProps } from '@fairys/valtio-form-basic';
5
+ import { FairysValtioFormInstance, FairysValtioFormParentAttrs } from '@fairys/valtio-form-basic/esm/common';
6
+ import { FairysValtioFormLayoutContextOptions } from './layout';
7
+ export interface FairysValtioFormItemAttrsProps<T extends MObject<T> = object> extends Omit<_FairysValtioFormItemAttrsProps<T>, 'style' | 'labelStyle' | 'bodyStyle' | 'rowSpan' | 'className' | 'labelClassName' | 'bodyClassName'> {
8
+ style?: ViewProps['style'];
9
+ labelStyle?: ViewProps['style'];
10
+ bodyStyle?: ViewProps['style'];
11
+ }
12
+ /**
13
+ * 处理表单表单项属性
14
+ *
15
+ * @example
16
+ *
17
+ * ```tsx
18
+
19
+ * ```
20
+ *
21
+ */
22
+ export declare function useFairysValtioFormItemAttrs<T extends MObject<T> = object>(props: FairysValtioFormItemAttrsProps<T>): FairysValtioFormItemAttrsReturn<T>;
23
+ export interface FairysValtioFormItemAttrsReturn<T extends MObject<T> = object> {
24
+ /**表单项值*/
25
+ value?: any;
26
+ /**是否校验错误*/
27
+ isInvalid: boolean;
28
+ /**边框类型*/
29
+ itemBorderType: FairysValtioFormLayoutContextOptions['itemBorderType'];
30
+ /**值改变事件*/
31
+ onValueChange: (event: any) => void;
32
+ /**当前表单项占据列数*/
33
+ colSpan: number;
34
+ /**列数*/
35
+ colCount: number;
36
+ /**标签显示模式*/
37
+ labelMode: FairysValtioFormLayoutContextOptions['labelMode'];
38
+ /**错误提示位置*/
39
+ errorLayout: FairysValtioFormLayoutContextOptions['errorLayout'];
40
+ /**是否必填*/
41
+ isRequired: boolean;
42
+ /**表单状态*/
43
+ state: T;
44
+ /**错误状态*/
45
+ errorState: Record<PropertyKey, string[]>;
46
+ /**表单实例*/
47
+ formInstance: FairysValtioFormInstance<T>;
48
+ /**错误信息*/
49
+ error?: string[];
50
+ /**拼接父级字段名后得到的表单项名称*/
51
+ _name?: string;
52
+ /**表单项名称*/
53
+ name?: string;
54
+ /**表单项ID*/
55
+ id?: string;
56
+ /**表单项路径*/
57
+ paths?: (string | number)[];
58
+ /**父级字段名*/
59
+ parentName?: string;
60
+ /**表单属性名实例*/
61
+ formAttrsNameInstance: FairysValtioFormParentAttrs;
62
+ /**是否显示冒号*/
63
+ showColon: boolean;
64
+ itemStyle: ViewProps['style'];
65
+ itemContainerStyle: ViewProps['style'];
66
+ itemLabelStyle: ViewProps['style'];
67
+ itemLabelTextStyle: ViewProps['style'];
68
+ itemLabelShowColonStyle: ViewProps['style'];
69
+ itemBodyStyle: ViewProps['style'];
70
+ itemInputStyle: ViewProps['style'];
71
+ itemExtraStyle: ViewProps['style'];
72
+ errorStyle: ViewProps['style'];
73
+ helpStyle: ViewProps['style'];
74
+ /**子元素*/
75
+ children?: React.ReactNode;
76
+ }
77
+ /**
78
+ * 没有样式的表单项属性,仅返回基础输入组件参数
79
+ *
80
+ * @example
81
+ *
82
+ *```tsx
83
+
84
+ * ```
85
+ */
86
+ export declare function useFairysValtioFormItemNoStyleAttrs<T extends MObject<T> = object>(props: FairysValtioFormItemAttrsProps<T>): FairysValtioFormItemNoStyleAttrsReturn<T>;
87
+ export interface FairysValtioFormItemNoStyleAttrsReturn<T extends MObject<T> = object> {
88
+ /**表单项值*/
89
+ value?: any;
90
+ /**是否校验错误*/
91
+ isInvalid: boolean;
92
+ /**是否必填*/
93
+ isRequired: boolean;
94
+ /**错误信息*/
95
+ error?: string[];
96
+ /**值改变事件*/
97
+ onValueChange: (event: any) => void;
98
+ /**表单状态*/
99
+ state: T;
100
+ /**错误状态*/
101
+ errorState: Record<PropertyKey, string[]>;
102
+ /**表单实例*/
103
+ formInstance: FairysValtioFormInstance<T>;
104
+ /**拼接父级字段名后得到的表单项名称*/
105
+ _name?: string;
106
+ /**表单项名称*/
107
+ name?: string;
108
+ /**表单项ID*/
109
+ id?: string;
110
+ /**表单项路径*/
111
+ paths?: (string | number)[];
112
+ /**父级字段名*/
113
+ parentName?: string;
114
+ /**表单属性名实例*/
115
+ formAttrsNameInstance: FairysValtioFormParentAttrs;
116
+ /**子元素*/
117
+ children?: React.ReactNode;
118
+ }
@@ -0,0 +1,292 @@
1
+ import react, { useEffect, useMemo } from "react";
2
+ import { StyleSheet } from "react-native";
3
+ import { formatePath, get, useFairysValtioFormAttrsName, useFairysValtioFormInstanceContextState, useId } from "@fairys/valtio-form-basic/esm/common";
4
+ import { formItemStyles } from "../styles/form.item.js";
5
+ import { useFairysValtioFormLayoutContext } from "./layout.js";
6
+ function useFairysValtioFormItemAttrs(props) {
7
+ const [layoutAttrs] = useFairysValtioFormLayoutContext();
8
+ const colCount = layoutAttrs.colCount || 1;
9
+ const parent_borderedType = layoutAttrs.itemBorderType || 'bottom';
10
+ const parent_errorLayout = layoutAttrs.errorLayout || 'bottom-right';
11
+ const parent_formItemLabelStyle = layoutAttrs.formItemLabelStyle;
12
+ const parent_formItemStyle = layoutAttrs.formItemStyle;
13
+ const parent_formItemBodyStyle = layoutAttrs.formItemBodyStyle;
14
+ const parent_labelMode = layoutAttrs.labelMode || 'between';
15
+ const parent_itemBorderColor = layoutAttrs.itemBorderColor;
16
+ const parent_isInvalidBorderRed = layoutAttrs.isInvalidBorderRed;
17
+ const parent_isInvalidTextRed = layoutAttrs.isInvalidTextRed;
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 } = props;
20
+ const { name: _name, paths, parentName, formAttrsNameInstance } = useFairysValtioFormAttrsName({
21
+ name,
22
+ isJoinParentField
23
+ });
24
+ const widthStyles = useMemo(()=>{
25
+ if (colCount >= colSpan) return {
26
+ width: `${100 / colCount * colSpan}%`
27
+ };
28
+ return {
29
+ width: "100%"
30
+ };
31
+ }, [
32
+ colSpan,
33
+ colCount
34
+ ]);
35
+ const [state, errorState, formInstance] = useFairysValtioFormInstanceContextState();
36
+ const value = useMemo(()=>get(state, paths), [
37
+ state,
38
+ paths
39
+ ]);
40
+ const error = errorState[_name];
41
+ const _formItemRules = formInstance.rules?.[_name];
42
+ const id = useId(_name);
43
+ formInstance.nameToPaths[_name] = paths;
44
+ useEffect(()=>{
45
+ if (Array.isArray(rules) && rules.length) formInstance.mountRules[_name] = rules;
46
+ return ()=>{
47
+ formInstance.removeRules(_name);
48
+ };
49
+ }, [
50
+ _name,
51
+ rules
52
+ ]);
53
+ const onValueChange = (event)=>{
54
+ let newValue = event;
55
+ const target = event?.nativeEvent;
56
+ if ('function' == typeof getValueFromEvent) newValue = getValueFromEvent(event, formInstance);
57
+ else if (event && target && 'object' == typeof target && getValuePath in target) newValue = get(target, formatePath(getValuePath));
58
+ if ('function' == typeof formatValue) newValue = formatValue(newValue, formInstance, event);
59
+ if (newValue === value) return;
60
+ formInstance.updatedValueByPaths(_name, newValue);
61
+ if ('function' == typeof onAfterUpdate) onAfterUpdate(newValue, formInstance, event);
62
+ };
63
+ const baseControl = {
64
+ ...attrs,
65
+ style: StyleSheet.flatten([
66
+ attrs?.style || {},
67
+ 'between' === labelMode ? formItemStyles["input.between"] : {}
68
+ ]),
69
+ name,
70
+ id,
71
+ [valuePropName]: value,
72
+ [trigger]: onValueChange
73
+ };
74
+ const isRequired = useMemo(()=>{
75
+ if (_isRequired) return _isRequired;
76
+ if (Array.isArray(rules) && rules.length) return rules.some((rule)=>rule.required);
77
+ if (_formItemRules && Array.isArray(_formItemRules) && _formItemRules.length) return _formItemRules.some((rule)=>rule.required);
78
+ return false;
79
+ }, [
80
+ rules,
81
+ formInstance,
82
+ _formItemRules
83
+ ]);
84
+ const isInvalid = useMemo(()=>{
85
+ if (Array.isArray(error) && error.length) return true;
86
+ return false;
87
+ }, [
88
+ error
89
+ ]);
90
+ const item_style = useMemo(()=>StyleSheet.flatten([
91
+ formItemStyles["fairys-valtio-form-item"],
92
+ 'bottom' === itemBorderType ? formItemStyles["fairys-valtio-form-item.border-bottom"] : {},
93
+ itemBorderColor && 'bottom' === itemBorderType ? {
94
+ borderColor: itemBorderColor
95
+ } : {},
96
+ isInvalid && isInvalidBorderRed && 'bottom' === itemBorderType ? formItemStyles["fairys-valtio-form-item.border-bottom.red"] : {},
97
+ widthStyles,
98
+ parent_formItemStyle,
99
+ style
100
+ ]), [
101
+ style,
102
+ widthStyles,
103
+ parent_formItemStyle,
104
+ labelMode,
105
+ itemBorderType,
106
+ isInvalid,
107
+ isInvalidBorderRed
108
+ ]);
109
+ const itemContainer_style = useMemo(()=>StyleSheet.flatten([
110
+ formItemStyles["fairys-valtio-form-item-container"],
111
+ 'between' === labelMode ? formItemStyles["fairys-valtio-form-item-container.between"] : {},
112
+ 'top' === labelMode ? formItemStyles["fairys-valtio-form-item-container.top"] : {},
113
+ 'left' === labelMode ? formItemStyles["fairys-valtio-form-item-container.left"] : {}
114
+ ]), [
115
+ labelMode
116
+ ]);
117
+ const itemLabel_style = useMemo(()=>StyleSheet.flatten([
118
+ formItemStyles["fairys-valtio-form-item-label"],
119
+ 'left' === labelMode ? formItemStyles["fairys-valtio-form-item-label.left"] : {},
120
+ parent_formItemLabelStyle,
121
+ labelStyle
122
+ ]), [
123
+ labelMode,
124
+ parent_formItemLabelStyle,
125
+ labelStyle
126
+ ]);
127
+ const itemLabelShowColon_style = useMemo(()=>StyleSheet.flatten([
128
+ formItemStyles["fairys-valtio-form-item-label.show-colon"],
129
+ isInvalid && isInvalidTextRed ? formItemStyles["fairys-valtio-form-item-label.show-colon.red"] : {}
130
+ ]), [
131
+ isInvalid,
132
+ isInvalidTextRed
133
+ ]);
134
+ const itemLabelText_style = useMemo(()=>StyleSheet.flatten([
135
+ formItemStyles["fairys-valtio-form-item-label-text"],
136
+ isInvalid && isInvalidTextRed ? formItemStyles["fairys-valtio-form-item-label-text.red"] : {}
137
+ ]), [
138
+ labelMode,
139
+ isInvalid,
140
+ isInvalidTextRed
141
+ ]);
142
+ const itemBody_style = useMemo(()=>StyleSheet.flatten([
143
+ formItemStyles["fairys-valtio-form-item-body"],
144
+ 'left' === labelMode ? formItemStyles["fairys-valtio-form-item-body.left"] : {},
145
+ 'top' === labelMode ? formItemStyles["fairys-valtio-form-item-body.top"] : {},
146
+ 'between' === labelMode ? formItemStyles["fairys-valtio-form-item-body.between"] : {},
147
+ 'body' === itemBorderType ? formItemStyles["fairys-valtio-form-item-body.border-bottom"] : {},
148
+ itemBorderColor && 'body' === itemBorderType ? {
149
+ borderColor: itemBorderColor
150
+ } : {},
151
+ isInvalid && isInvalidBorderRed && 'body' === itemBorderType ? formItemStyles["fairys-valtio-form-item-body.border-bottom.red"] : {},
152
+ parent_formItemBodyStyle,
153
+ bodyStyle
154
+ ]), [
155
+ bodyStyle,
156
+ parent_formItemBodyStyle,
157
+ labelMode,
158
+ isInvalid
159
+ ]);
160
+ const itemBodyInput_style = useMemo(()=>StyleSheet.flatten([
161
+ formItemStyles["fairys-valtio-form-item-body-input"],
162
+ 'between' === labelMode ? formItemStyles["fairys-valtio-form-item-body-input.between"] : {},
163
+ 'between' !== labelMode ? formItemStyles["fairys-valtio-form-item-body-input.not.between"] : {}
164
+ ]), [
165
+ labelMode
166
+ ]);
167
+ const itemExtra_style = useMemo(()=>StyleSheet.flatten([
168
+ formItemStyles["fairys-valtio-form-item-body-extra"]
169
+ ]), []);
170
+ const itemHelp_style = useMemo(()=>StyleSheet.flatten([
171
+ formItemStyles["fairys-valtio-form-item-help"]
172
+ ]), []);
173
+ const itemError_style = useMemo(()=>StyleSheet.flatten([
174
+ formItemStyles["fairys-valtio-form-item-body-error"],
175
+ errorLayout ? formItemStyles['fairys-valtio-form-item-body-error.' + errorLayout] || {} : {}
176
+ ]), [
177
+ errorLayout
178
+ ]);
179
+ return {
180
+ value,
181
+ isInvalid,
182
+ itemBorderType,
183
+ onValueChange,
184
+ colSpan,
185
+ colCount,
186
+ labelMode,
187
+ errorLayout,
188
+ isRequired,
189
+ state,
190
+ errorState,
191
+ formInstance,
192
+ error,
193
+ _name,
194
+ name,
195
+ id,
196
+ paths,
197
+ parentName,
198
+ formAttrsNameInstance,
199
+ showColon,
200
+ itemStyle: item_style,
201
+ itemContainerStyle: itemContainer_style,
202
+ itemLabelStyle: itemLabel_style,
203
+ itemLabelTextStyle: itemLabelText_style,
204
+ itemLabelShowColonStyle: itemLabelShowColon_style,
205
+ itemBodyStyle: itemBody_style,
206
+ itemInputStyle: itemBodyInput_style,
207
+ itemExtraStyle: itemExtra_style,
208
+ errorStyle: itemError_style,
209
+ helpStyle: itemHelp_style,
210
+ children: react.isValidElement(children) ? react.cloneElement(children, {
211
+ ...baseControl
212
+ }) : children
213
+ };
214
+ }
215
+ function useFairysValtioFormItemNoStyleAttrs(props) {
216
+ const { name, valuePropName = 'value', getValuePath = 'text', getValueFromEvent, formatValue, onAfterUpdate, trigger = 'onChange', children, attrs = {}, isJoinParentField = true, rules, isRequired: _isRequired } = props;
217
+ const [state, errorState, formInstance] = useFairysValtioFormInstanceContextState();
218
+ const { name: _name, paths, parentName, formAttrsNameInstance } = useFairysValtioFormAttrsName({
219
+ name,
220
+ isJoinParentField
221
+ });
222
+ const id = useId(_name);
223
+ const value = useMemo(()=>get(state, paths), [
224
+ state,
225
+ paths
226
+ ]);
227
+ const error = errorState[_name];
228
+ formInstance.nameToPaths[_name] = paths;
229
+ const _formItemRules = formInstance.rules?.[_name];
230
+ useEffect(()=>{
231
+ if (Array.isArray(rules) && rules.length) formInstance.mountRules[_name] = rules;
232
+ return ()=>{
233
+ formInstance.removeRules(_name);
234
+ };
235
+ }, [
236
+ _name,
237
+ rules
238
+ ]);
239
+ const onValueChange = (event)=>{
240
+ let newValue = event;
241
+ const target = event?.nativeEvent;
242
+ if ('function' == typeof getValueFromEvent) newValue = getValueFromEvent(event, formInstance);
243
+ else if (event && target && 'object' == typeof target && getValuePath in target) newValue = get(target, formatePath(getValuePath));
244
+ if ('function' == typeof formatValue) newValue = formatValue(newValue, formInstance, event);
245
+ if (newValue === value) return;
246
+ formInstance.updatedValueByPaths(_name, newValue);
247
+ if ('function' == typeof onAfterUpdate) onAfterUpdate(newValue, formInstance, event);
248
+ };
249
+ const baseControl = {
250
+ ...attrs,
251
+ name,
252
+ id,
253
+ [valuePropName]: value,
254
+ [trigger]: onValueChange
255
+ };
256
+ const isRequired = useMemo(()=>{
257
+ if (_isRequired) return _isRequired;
258
+ if (Array.isArray(rules) && rules.length) return rules.some((rule)=>rule.required);
259
+ if (_formItemRules && Array.isArray(_formItemRules) && _formItemRules.length) return _formItemRules.some((rule)=>rule.required);
260
+ return false;
261
+ }, [
262
+ rules,
263
+ formInstance,
264
+ _formItemRules
265
+ ]);
266
+ const isInvalid = useMemo(()=>{
267
+ if (Array.isArray(error) && error.length) return true;
268
+ return false;
269
+ }, [
270
+ error
271
+ ]);
272
+ return {
273
+ value,
274
+ isRequired,
275
+ isInvalid,
276
+ error,
277
+ onValueChange,
278
+ state,
279
+ errorState,
280
+ formInstance,
281
+ _name,
282
+ name,
283
+ id,
284
+ paths,
285
+ parentName,
286
+ formAttrsNameInstance,
287
+ children: react.isValidElement(children) ? react.cloneElement(children, {
288
+ ...baseControl
289
+ }) : children
290
+ };
291
+ }
292
+ export { useFairysValtioFormItemAttrs, useFairysValtioFormItemNoStyleAttrs };
@@ -0,0 +1,17 @@
1
+ import { useFairysValtioFormInstance } from "@fairys/valtio-form-basic/esm/common";
2
+ import { useMemo } from "react";
3
+ function useFairysValtioForm(props) {
4
+ const { form, rules, formData, hideState, initFormDataType = 'deepCopy', ...rest } = props;
5
+ const formInstance = useFairysValtioFormInstance(form);
6
+ formInstance.rules = rules;
7
+ useMemo(()=>formInstance.ctor({
8
+ formData,
9
+ hideState,
10
+ initFormDataType
11
+ }), []);
12
+ return {
13
+ ...rest,
14
+ formInstance
15
+ };
16
+ }
17
+ export { useFairysValtioForm };