react-antd-xform 1.0.5 → 1.0.6

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025 knowledge is limitless
3
+ Copyright (c) JHuaZhang
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -6,7 +6,8 @@ const FormEnvContext = createContext({
6
6
  validateOnBlur: true,
7
7
  validateOnChange: true,
8
8
  writeDefaultValueToModel: false,
9
- autoUnmount: false
9
+ autoUnmount: false,
10
+ variant: "outlined"
10
11
  });
11
12
  FormEnvContext.displayName = "FormEnvContext";
12
13
  const useFormEnv = () => useContext(FormEnvContext);
@@ -1,4 +1,2 @@
1
- import { FormLayoutProps, FormItemGroupProps } from '../type';
1
+ import { FormLayoutProps } from '../type';
2
2
  export declare function FormLayout({ children, className, style, labelPosition, labelWidth, formItemGap, controlWidth, defaultLabelTopPosition, inlineError, containerProps, }: FormLayoutProps): import("react/jsx-runtime").JSX.Element;
3
- /** @deprecated 请使用 Form.ItemView 代替 ItemGroup */
4
- export declare const FormItemGroup: ({ label, asterisk, tip, children, labelWidth, controlWidth, className, style, inline, }: FormItemGroupProps) => import("react/jsx-runtime").JSX.Element;
@@ -165,16 +165,6 @@ function FormLayout({
165
165
  }
166
166
  );
167
167
  }
168
- styled.div`
169
- ${FormLayoutContainer}.horizontal & {
170
- display: flex;
171
- }
172
- &.inline {
173
- .form-item-group-content {
174
- display: flex;
175
- }
176
- }
177
- `;
178
168
  export {
179
169
  FormLayout
180
170
  };
@@ -253,6 +253,8 @@ const Hidden = createFormItem({
253
253
  }
254
254
  });
255
255
  function FormItem({ use, component, ...props }) {
256
+ const formEnv = useFormEnv();
257
+ const variant = formEnv.variant;
256
258
  if (use === false) {
257
259
  return null;
258
260
  }
@@ -266,13 +268,16 @@ function FormItem({ use, component, ...props }) {
266
268
  if (Comp == null) {
267
269
  return /* @__PURE__ */ jsxRuntimeExports.jsx(NotFound, { ...props, componentProps: { $Component: component } });
268
270
  }
269
- return React.createElement(Comp, props);
271
+ return React.createElement(Comp, {
272
+ ...props,
273
+ componentProps: { ...props.componentProps, variant }
274
+ });
270
275
  } else {
271
276
  return /* @__PURE__ */ jsxRuntimeExports.jsx(
272
277
  AnonymousFormItem,
273
278
  {
274
279
  ...props,
275
- componentProps: { ...props.componentProps, $Component: component }
280
+ componentProps: { ...props.componentProps, variant, $Component: component }
276
281
  }
277
282
  );
278
283
  }
@@ -4,7 +4,7 @@ import { FormModel } from './model';
4
4
  import { FormItemView } from './form-item';
5
5
  import { FormLayout } from './extend/form-layout';
6
6
  import { FormSubmit } from './extend/form-submit';
7
- export declare function Form({ model: modelProp, defaultValue, children, className, style, layout, containerProps, htmlIdPrefix: htmlIdPrefixProp, ...restEnvProps }: FormProps): import("react/jsx-runtime").JSX.Element;
7
+ export declare function Form({ model: modelProp, defaultValue, children, className, style, layout, containerProps, htmlIdPrefix: htmlIdPrefixProp, variant, ...restEnvProps }: FormProps): import("react/jsx-runtime").JSX.Element;
8
8
  export declare namespace Form {
9
9
  var Array: (<T extends unknown>({ name, children, layout, arrayModel: arrayModelProp, use, }: import("./extend/form-array").FormArrayProps<T>) => import("react/jsx-runtime").JSX.Element) & {
10
10
  displayName: string;
@@ -22,12 +22,13 @@ function Form({
22
22
  layout,
23
23
  containerProps,
24
24
  htmlIdPrefix: htmlIdPrefixProp,
25
+ variant,
25
26
  ...restEnvProps
26
27
  }) {
27
28
  const [_model] = useState(() => new FormModel(defaultValue));
28
29
  const model = composeValue(modelProp, _model);
29
30
  const htmlIdPrefix = useHtmlIdPrefix(htmlIdPrefixProp);
30
- return /* @__PURE__ */ jsxRuntimeExports.jsx(FormEnvProvider, { htmlIdPrefix, ...restEnvProps, children: /* @__PURE__ */ jsxRuntimeExports.jsx(ModelProvider, { value: model, children: /* @__PURE__ */ jsxRuntimeExports.jsx(FormLayout, { style, className, containerProps, ...layout, children }) }) });
31
+ return /* @__PURE__ */ jsxRuntimeExports.jsx(FormEnvProvider, { htmlIdPrefix, variant, ...restEnvProps, children: /* @__PURE__ */ jsxRuntimeExports.jsx(ModelProvider, { value: model, children: /* @__PURE__ */ jsxRuntimeExports.jsx(FormLayout, { style, className, containerProps, ...layout, children }) }) });
31
32
  }
32
33
  Form.Array = FormArray;
33
34
  Form.Check = FormCheck;
@@ -2,6 +2,8 @@ import { ReactNode } from 'react';
2
2
  import { Field, FormModel } from './model';
3
3
  import { FieldType, ModelType } from './enum';
4
4
  import { AsyncValue } from './helpers/AsyncValue';
5
+ export declare const Variants: readonly ["outlined", "borderless", "filled", "underlined"];
6
+ export type Variant = (typeof Variants)[number];
5
7
  type valueOf<T> = T[keyof T];
6
8
  export type XName<D> = 0 extends D & 1 ? any : D extends (infer U)[] ? number | `${number}` | `${number}.${XName<U>}` : D extends object ? valueOf<{
7
9
  [K in keyof D & string]: K | `${K}.${XName<D[K]>}`;
@@ -117,6 +119,10 @@ export interface FormEnvContextType {
117
119
  * @see https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/label
118
120
  * */
119
121
  htmlIdPrefix?: string;
122
+ /**
123
+ * 表单样式参数 from antd
124
+ * */
125
+ variant?: Variant;
120
126
  }
121
127
  export interface FormLayoutParams {
122
128
  /** 标签位置,可选 'left' 或 'top' */
@@ -150,6 +156,10 @@ export interface FormProps extends FormEnvContextType {
150
156
  * */
151
157
  layout?: FormLayoutParams;
152
158
  children?: React.ReactNode;
159
+ /**
160
+ * 表单样式参数 from antd
161
+ * */
162
+ variant?: Variant;
153
163
  }
154
164
  export interface FormLayoutParams {
155
165
  /** 标签位置,可选 'left' 或 'top' */
@@ -170,18 +180,6 @@ export interface FormLayoutProps extends Partial<FormLayoutParams> {
170
180
  containerProps?: any;
171
181
  defaultLabelTopPosition?: number | string;
172
182
  }
173
- export interface FormItemGroupProps {
174
- label?: React.ReactNode;
175
- tip?: React.ReactNode;
176
- asterisk?: boolean;
177
- children?: React.ReactNode;
178
- labelWidth?: number | string;
179
- labelTopPosition?: number | string;
180
- controlWidth?: number | string;
181
- className?: string;
182
- style?: React.CSSProperties;
183
- inline?: boolean;
184
- }
185
183
  export interface FormItemComponentProps {
186
184
  value?: any;
187
185
  onChange?(...args: any[]): void;
@@ -237,6 +235,7 @@ export interface FormItemProps extends Omit<FieldConfig<any>, 'defaultValueProp'
237
235
  controlStyle?: React.CSSProperties;
238
236
  rightNode?: React.ReactNode;
239
237
  isPreview?: boolean;
238
+ variant?: Variant;
240
239
  }
241
240
  export interface FormItemViewProps {
242
241
  /** `<label />` 的 id 属性 */
@@ -1,4 +1,6 @@
1
1
  import { default as React } from 'react';
2
+ import { ColorPicker } from 'antd';
3
+ import { ColorPickerProps } from 'antd/es/color-picker';
2
4
  export declare function isEmptyValue(value: any): boolean;
3
5
  /**
4
6
  * antd中某些组件的onChange并不是直接返回value,而是e.target.value
@@ -28,3 +30,14 @@ export declare const withDayjsTransformAntdTimeRange: (Component: React.Componen
28
30
  * 向组件中注入props
29
31
  */
30
32
  export declare const withInjectedProps: (injectedProps: Record<string, any>) => (Component: React.ComponentType<any>) => React.ForwardRefExoticComponent<Omit<any, "ref"> & React.RefAttributes<any>>;
33
+ /**
34
+ * 用于处理Antd ColorPicker的onChange返回值格式
35
+ * 根据 returnType 返回指定格式的值(如 hex、rgb、hsb)
36
+ */
37
+ type ColorReturnType = 'hex' | 'rgb' | 'hsv';
38
+ interface ColorPickerWrapperProps extends Omit<ColorPickerProps, 'onChange'> {
39
+ onChange?: (value: string) => void;
40
+ returnType?: ColorReturnType;
41
+ }
42
+ export declare const withColorPickerHandler: (Component: typeof ColorPicker) => ({ onChange, returnType, ...props }: ColorPickerWrapperProps) => import("react/jsx-runtime").JSX.Element;
43
+ export {};
@@ -1,9 +1,9 @@
1
1
  import { j as jsxRuntimeExports } from "../node_modules/.pnpm/react@18.3.1/node_modules/react/jsx-runtime.js";
2
2
  import React, { useMemo, useCallback } from "react";
3
3
  import dayjs from "../node_modules/.pnpm/dayjs@1.11.18/node_modules/dayjs/dayjs.min.js";
4
- import weekOfYear from "../node_modules/.pnpm/dayjs@1.11.18/node_modules/dayjs/plugin/weekOfYear.js";
5
- import weekYear from "../node_modules/.pnpm/dayjs@1.11.18/node_modules/dayjs/plugin/weekYear.js";
6
4
  import isoweek from "../node_modules/.pnpm/dayjs@1.11.18/node_modules/dayjs/plugin/isoWeek.js";
5
+ import weekYear from "../node_modules/.pnpm/dayjs@1.11.18/node_modules/dayjs/plugin/weekYear.js";
6
+ import weekOfYear from "../node_modules/.pnpm/dayjs@1.11.18/node_modules/dayjs/plugin/weekOfYear.js";
7
7
  dayjs.extend(weekOfYear);
8
8
  dayjs.extend(weekYear);
9
9
  dayjs.extend(isoweek);
@@ -248,8 +248,37 @@ const withInjectedProps = (injectedProps) => {
248
248
  return WrappedComponent;
249
249
  };
250
250
  };
251
+ const withColorPickerHandler = (Component) => {
252
+ return ({
253
+ onChange,
254
+ returnType = "hex",
255
+ ...props
256
+ }) => {
257
+ const handleChange = (color) => {
258
+ if (!onChange) return;
259
+ let value;
260
+ switch (returnType) {
261
+ case "hex":
262
+ value = color.toHexString();
263
+ break;
264
+ case "rgb":
265
+ value = color.toRgbString();
266
+ break;
267
+ case "hsv":
268
+ const hsv = color.toHsb();
269
+ value = `hsv(${hsv.h}, ${hsv.s * 100}%, ${hsv.b * 100}%)`;
270
+ break;
271
+ default:
272
+ value = color.toHexString();
273
+ }
274
+ onChange(value);
275
+ };
276
+ return /* @__PURE__ */ jsxRuntimeExports.jsx(Component, { ...props, onChange: handleChange });
277
+ };
278
+ };
251
279
  export {
252
280
  isEmptyValue,
281
+ withColorPickerHandler,
253
282
  withDayjsTransformAntdDate,
254
283
  withDayjsTransformAntdDateRangePicker,
255
284
  withDayjsTransformAntdTime,
@@ -1,2 +1,9 @@
1
- import { FormItemCreationOptions } from '../form/type';
2
- export declare const ALL_COMPONENTS: FormItemCreationOptions[];
1
+ import { isEmptyValue } from './common-utils';
2
+ declare const ALL_COMPONENTS: {
3
+ name: "input" | "textArea" | "radio" | "inputNumber" | "slider" | "switch" | "datePicker" | "dateRangePicker" | "timePicker" | "timeRangePicker" | "colorPicker" | "multiSelect" | "select" | "singleSelect" | "checkbox" | "rate";
4
+ component: any;
5
+ defaultValue: any;
6
+ isEmpty: typeof isEmptyValue;
7
+ hasIntrinsicWidth: false;
8
+ }[];
9
+ export { ALL_COMPONENTS };
@@ -1,105 +1,92 @@
1
- import { Select, Checkbox, InputNumber, Slider, Switch, Input, Radio, DatePicker, TimePicker } from "antd";
2
- import { isEmptyValue, withValueChangeHandler, withInjectedProps, withDayjsTransformAntdDate, withDayjsTransformAntdDateRangePicker, withDayjsTransformAntdTime, withDayjsTransformAntdTimeRange } from "./common-utils.js";
3
- const ALL_COMPONENTS = [
4
- {
5
- name: "input",
6
- component: withValueChangeHandler(Input),
7
- defaultValue: "",
8
- isEmpty: isEmptyValue,
9
- hasIntrinsicWidth: false
10
- },
11
- {
12
- name: "textArea",
13
- component: withValueChangeHandler(Input.TextArea),
14
- defaultValue: "",
15
- isEmpty: isEmptyValue,
16
- hasIntrinsicWidth: false
17
- },
18
- {
19
- name: "select",
20
- component: Select,
21
- defaultValue: "",
22
- isEmpty: isEmptyValue,
23
- hasIntrinsicWidth: false
24
- },
25
- {
26
- name: "singleSelect",
27
- component: Select,
28
- defaultValue: "",
29
- isEmpty: isEmptyValue,
30
- hasIntrinsicWidth: false
31
- },
32
- {
33
- name: "multiSelect",
34
- component: withInjectedProps({ mode: "multiple" })(Select),
35
- defaultValue: [],
36
- isEmpty: isEmptyValue,
37
- hasIntrinsicWidth: false
38
- },
39
- {
40
- name: "radio",
41
- component: withValueChangeHandler(Radio.Group),
42
- defaultValue: "",
43
- isEmpty: isEmptyValue,
44
- hasIntrinsicWidth: false
45
- },
46
- {
47
- name: "checkbox",
48
- component: Checkbox.Group,
49
- defaultValue: "",
50
- isEmpty: isEmptyValue,
51
- hasIntrinsicWidth: false
52
- },
53
- {
54
- name: "inputNumber",
55
- component: InputNumber,
56
- defaultValue: "",
57
- isEmpty: isEmptyValue,
58
- hasIntrinsicWidth: false
59
- },
60
- {
61
- name: "slider",
62
- component: Slider,
63
- defaultValue: "",
64
- isEmpty: isEmptyValue,
65
- hasIntrinsicWidth: false
66
- },
67
- {
68
- name: "switch",
69
- component: Switch,
70
- defaultValue: false,
71
- isEmpty: isEmptyValue,
72
- hasIntrinsicWidth: false
73
- },
74
- {
75
- name: "datePicker",
76
- component: withDayjsTransformAntdDate(DatePicker),
77
- defaultValue: false,
78
- isEmpty: isEmptyValue,
79
- hasIntrinsicWidth: false
80
- },
81
- {
82
- name: "dateRangePicker",
83
- component: withDayjsTransformAntdDateRangePicker(DatePicker.RangePicker),
84
- defaultValue: false,
85
- isEmpty: isEmptyValue,
86
- hasIntrinsicWidth: false
87
- },
88
- {
89
- name: "timePicker",
90
- component: withDayjsTransformAntdTime(TimePicker),
91
- defaultValue: false,
92
- isEmpty: isEmptyValue,
93
- hasIntrinsicWidth: false
94
- },
95
- {
96
- name: "timeRangePickerPicker",
97
- component: withDayjsTransformAntdTimeRange(TimePicker.RangePicker),
98
- defaultValue: false,
1
+ import { ColorPicker, Rate, TimePicker, DatePicker, Switch, Slider, InputNumber, Checkbox, Radio, Select, Input } from "antd";
2
+ import { withInjectedProps, withColorPickerHandler, isEmptyValue, withValueChangeHandler, withDayjsTransformAntdDate, withDayjsTransformAntdDateRangePicker, withDayjsTransformAntdTime, withDayjsTransformAntdTimeRange } from "./common-utils.js";
3
+ const COMPONENT_MAP = {
4
+ input: Input,
5
+ textArea: Input.TextArea,
6
+ select: Select,
7
+ singleSelect: Select,
8
+ multiSelect: Select,
9
+ radio: Radio.Group,
10
+ checkbox: Checkbox.Group,
11
+ inputNumber: InputNumber,
12
+ slider: Slider,
13
+ switch: Switch,
14
+ datePicker: DatePicker,
15
+ dateRangePicker: DatePicker.RangePicker,
16
+ timePicker: TimePicker,
17
+ timeRangePicker: TimePicker.RangePicker,
18
+ rate: Rate,
19
+ colorPicker: ColorPicker
20
+ };
21
+ const HOC_RULES = {
22
+ // 需要withValueChangeHandler的组件
23
+ withValueChangeHandler: [
24
+ "input",
25
+ "textArea",
26
+ "radio",
27
+ "inputNumber",
28
+ "slider",
29
+ "switch"
30
+ ],
31
+ // 需要日期转换(Day.js)的组件
32
+ withDayjsTransformAntdDate: ["datePicker"],
33
+ withDayjsTransformAntdDateRangePicker: ["dateRangePicker"],
34
+ withDayjsTransformAntdTime: ["timePicker"],
35
+ withDayjsTransformAntdTimeRange: ["timeRangePicker"],
36
+ // 颜色选择处理
37
+ withColorPickerHandler: ["colorPicker"],
38
+ // 多选下拉框
39
+ multiSelect: ["multiSelect"]
40
+ };
41
+ const DEFAULT_VALUES = {
42
+ input: "",
43
+ textArea: "",
44
+ select: "",
45
+ singleSelect: "",
46
+ multiSelect: [],
47
+ radio: "",
48
+ checkbox: [],
49
+ inputNumber: null,
50
+ slider: null,
51
+ switch: false,
52
+ datePicker: null,
53
+ dateRangePicker: null,
54
+ timePicker: null,
55
+ timeRangePicker: null,
56
+ rate: 0,
57
+ colorPicker: ""
58
+ };
59
+ const ALL_COMPONENTS = Object.keys(COMPONENT_MAP).map((name) => {
60
+ let component = COMPONENT_MAP[name];
61
+ if (HOC_RULES.withValueChangeHandler.includes(name)) {
62
+ component = withValueChangeHandler(component);
63
+ }
64
+ if (HOC_RULES.withDayjsTransformAntdDate.includes(name)) {
65
+ component = withDayjsTransformAntdDate(component);
66
+ }
67
+ if (HOC_RULES.withDayjsTransformAntdDateRangePicker.includes(name)) {
68
+ component = withDayjsTransformAntdDateRangePicker(component);
69
+ }
70
+ if (HOC_RULES.withDayjsTransformAntdTime.includes(name)) {
71
+ component = withDayjsTransformAntdTime(component);
72
+ }
73
+ if (HOC_RULES.withDayjsTransformAntdTimeRange.includes(name)) {
74
+ component = withDayjsTransformAntdTimeRange(component);
75
+ }
76
+ if (HOC_RULES.multiSelect.includes(name)) {
77
+ component = withInjectedProps({ mode: "multiple" })(component);
78
+ }
79
+ if (HOC_RULES.withColorPickerHandler.includes(name)) {
80
+ component = withColorPickerHandler(component);
81
+ }
82
+ return {
83
+ name,
84
+ component,
85
+ defaultValue: DEFAULT_VALUES[name],
99
86
  isEmpty: isEmptyValue,
100
87
  hasIntrinsicWidth: false
101
- }
102
- ];
88
+ };
89
+ });
103
90
  export {
104
91
  ALL_COMPONENTS
105
92
  };
@@ -1,4 +1,4 @@
1
- import { getDefaultExportFromCjs, commonjsGlobal } from "../../../../../_virtual/_commonjsHelpers.js";
1
+ import { commonjsGlobal, getDefaultExportFromCjs } from "../../../../../_virtual/_commonjsHelpers.js";
2
2
  import { __module as dayjs_min } from "../../../../../_virtual/dayjs.min.js";
3
3
  (function(module, exports) {
4
4
  !function(t, e) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-antd-xform",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "基于mobx的form解决方案",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -13,24 +13,21 @@
13
13
  "scripts": {
14
14
  "build": "vite build",
15
15
  "dev": "sb dev -p 6006",
16
- "build-storybook": "sb build"
16
+ "build-storybook": "sb build",
17
+ "docs:dev": "dumi dev",
18
+ "docs:build": "dumi build",
19
+ "docs:serve": "dumi serve"
17
20
  },
18
- "keywords": [
19
- "form",
20
- "antd",
21
- "react",
22
- "mobx",
23
- "react-antd-xform"
24
- ],
21
+ "keywords": [],
25
22
  "author": "",
26
- "license": "MIT",
23
+ "license": "ISC",
27
24
  "packageManager": "pnpm@10.12.1",
28
25
  "dependencies": {
29
26
  "@types/invariant": "^2.2.37",
30
27
  "@types/stringify-object": "^4.0.5",
31
28
  "antd": "^5.26.2",
32
29
  "classnames": "^2.5.1",
33
- "dayjs": "^1.11.18",
30
+ "dayjs": "^1.11.15",
34
31
  "invariant": "^2.2.4",
35
32
  "mobx": "^6.13.7",
36
33
  "mobx-react-lite": "^4.1.0",
@@ -48,10 +45,10 @@
48
45
  "@types/react-dom": "^18.2.22",
49
46
  "@types/react-syntax-highlighter": "^15.5.13",
50
47
  "@vitejs/plugin-react": "^4.6.0",
48
+ "dumi": "^2.4.21",
51
49
  "react": "^18.2.0",
52
50
  "react-dom": "^18.2.0",
53
51
  "react-json-view": "^1.21.3",
54
- "react-syntax-highlighter": "^15.6.1",
55
52
  "vite": "^5.2.0",
56
53
  "vite-plugin-dts": "^4.5.4"
57
54
  },
package/README.md DELETED
@@ -1,2 +0,0 @@
1
- # react-antd-xform
2
- 基于react + mobx实现的antd表单方案