@canlooks/can-ui 0.0.55 → 0.0.57

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.
@@ -39,8 +39,7 @@ exports.CurdDialog = (0, react_2.memo)(({ ref, onFinish, curdProps, ...props })
39
39
  (0, react_2.useImperativeHandle)(ref, () => ({
40
40
  open(row, initialFormValue) {
41
41
  innerFormRef.current?.resetForm();
42
- suppressTransform.current = !!initialFormValue;
43
- initialFormValue && innerFormRef.current?.setFormValue(initialFormValue);
42
+ handlingFormValue.current = initialFormValue;
44
43
  setActiveRow(row);
45
44
  setInnerOpen(true);
46
45
  const { promise } = openResolvers.current = Promise.withResolvers();
@@ -64,13 +63,21 @@ exports.CurdDialog = (0, react_2.memo)(({ ref, onFinish, curdProps, ...props })
64
63
  * 表单项
65
64
  */
66
65
  const [activeRow, setActiveRow] = (0, react_2.useState)();
67
- const suppressTransform = (0, react_2.useRef)(false);
66
+ const handlingFormValue = (0, react_2.useRef)(void 0);
68
67
  const [transforming, setFormValue] = (0, utils_1.useLoading)(async (row) => {
69
68
  const formValue = rowToForm && row ? await rowToForm(row) : row;
70
69
  formValue && innerFormRef.current?.setFormValue(formValue);
71
70
  });
72
71
  (0, react_2.useEffect)(() => {
73
- innerOpen.current && !suppressTransform.current && setFormValue(activeRow);
72
+ if (!innerOpen.current) {
73
+ return;
74
+ }
75
+ if (handlingFormValue.current) {
76
+ innerFormRef.current?.setFormValue(handlingFormValue.current);
77
+ }
78
+ else {
79
+ setFormValue(activeRow);
80
+ }
74
81
  }, [innerOpen.current, activeRow]);
75
82
  const renderedFormItems = (0, react_2.useMemo)(() => {
76
83
  return (0, utils_1.columnsToFormItem)(columns, activeRow)?.map(col => {
@@ -81,6 +81,7 @@ inline, columnCount = 1, gap, columnGap, rowGap, ...props }) => {
81
81
  for (const [, item] of itemsContainer.current) {
82
82
  item.reset(newValue);
83
83
  }
84
+ console.log('after', newValue);
84
85
  setFormValue(newValue);
85
86
  },
86
87
  resetField: field => {
@@ -28,7 +28,11 @@ export interface FormItemOwnProps<I = any> extends FormSharedProps {
28
28
  error?: boolean;
29
29
  helperText?: ReactNode;
30
30
  dependencies?: FieldPath[];
31
- children?: ReactNode | ((fieldProps: FieldProps<I>, styleProps: StyleProps) => ReactNode);
31
+ /**
32
+ * 若`children`为`function`类型时,第三个参数`key`主要用于强制重渲染,
33
+ * 在没有{@link initialValue}时非常有用
34
+ */
35
+ children?: ReactNode | ((fieldProps: FieldProps<I>, styleProps: StyleProps, key?: string) => ReactNode);
32
36
  noStyle?: boolean;
33
37
  }
34
38
  export type FormItemProps<I = any, C extends ElementType = 'div'> = MergeProps<FormItemOwnProps<I>, DescriptionItemProps<C>>;
@@ -21,6 +21,10 @@ const FormItem = ({ ref, wrapperRef, field, initialValue, label = '', rules, req
21
21
  return (0, utils_1.queryDeep)(formValue, field);
22
22
  }
23
23
  }, [formValue, field]);
24
+ const [randomKey] = (0, utils_1.useDerivedState)(prev => {
25
+ // fieldValue变为undefined时,需要更新key以强制重渲染组件
26
+ return typeof fieldValue === 'undefined' ? (0, utils_1.getShortID)() : prev;
27
+ }, [fieldValue]);
24
28
  /**
25
29
  * --------------------------------------------------------------------
26
30
  * 校验
@@ -135,11 +139,12 @@ const FormItem = ({ ref, wrapperRef, field, initialValue, label = '', rules, req
135
139
  : {}, {
136
140
  ...props.size && { size: props.size },
137
141
  ...innerError.current ? { color: 'error' } : {}
138
- });
142
+ }, randomKey.current);
139
143
  }
140
144
  if ((0, react_1.isValidElement)(children)) {
141
145
  const { props: childProps } = children;
142
146
  return (0, react_1.cloneElement)(children, {
147
+ key: randomKey.current,
143
148
  ...typeof field !== 'undefined' && {
144
149
  ...typeof fieldValue === 'boolean'
145
150
  ? { checked: childProps.checked ?? fieldValue }
@@ -36,8 +36,7 @@ export const CurdDialog = memo(({ ref, onFinish, curdProps, ...props }) => {
36
36
  useImperativeHandle(ref, () => ({
37
37
  open(row, initialFormValue) {
38
38
  innerFormRef.current?.resetForm();
39
- suppressTransform.current = !!initialFormValue;
40
- initialFormValue && innerFormRef.current?.setFormValue(initialFormValue);
39
+ handlingFormValue.current = initialFormValue;
41
40
  setActiveRow(row);
42
41
  setInnerOpen(true);
43
42
  const { promise } = openResolvers.current = Promise.withResolvers();
@@ -61,13 +60,21 @@ export const CurdDialog = memo(({ ref, onFinish, curdProps, ...props }) => {
61
60
  * 表单项
62
61
  */
63
62
  const [activeRow, setActiveRow] = useState();
64
- const suppressTransform = useRef(false);
63
+ const handlingFormValue = useRef(void 0);
65
64
  const [transforming, setFormValue] = useLoading(async (row) => {
66
65
  const formValue = rowToForm && row ? await rowToForm(row) : row;
67
66
  formValue && innerFormRef.current?.setFormValue(formValue);
68
67
  });
69
68
  useEffect(() => {
70
- innerOpen.current && !suppressTransform.current && setFormValue(activeRow);
69
+ if (!innerOpen.current) {
70
+ return;
71
+ }
72
+ if (handlingFormValue.current) {
73
+ innerFormRef.current?.setFormValue(handlingFormValue.current);
74
+ }
75
+ else {
76
+ setFormValue(activeRow);
77
+ }
71
78
  }, [innerOpen.current, activeRow]);
72
79
  const renderedFormItems = useMemo(() => {
73
80
  return columnsToFormItem(columns, activeRow)?.map(col => {
@@ -77,6 +77,7 @@ inline, columnCount = 1, gap, columnGap, rowGap, ...props }) => {
77
77
  for (const [, item] of itemsContainer.current) {
78
78
  item.reset(newValue);
79
79
  }
80
+ console.log('after', newValue);
80
81
  setFormValue(newValue);
81
82
  },
82
83
  resetField: field => {
@@ -28,7 +28,11 @@ export interface FormItemOwnProps<I = any> extends FormSharedProps {
28
28
  error?: boolean;
29
29
  helperText?: ReactNode;
30
30
  dependencies?: FieldPath[];
31
- children?: ReactNode | ((fieldProps: FieldProps<I>, styleProps: StyleProps) => ReactNode);
31
+ /**
32
+ * 若`children`为`function`类型时,第三个参数`key`主要用于强制重渲染,
33
+ * 在没有{@link initialValue}时非常有用
34
+ */
35
+ children?: ReactNode | ((fieldProps: FieldProps<I>, styleProps: StyleProps, key?: string) => ReactNode);
32
36
  noStyle?: boolean;
33
37
  }
34
38
  export type FormItemProps<I = any, C extends ElementType = 'div'> = MergeProps<FormItemOwnProps<I>, DescriptionItemProps<C>>;
@@ -1,7 +1,7 @@
1
1
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "@emotion/react/jsx-runtime";
2
2
  import { cloneElement, isValidElement, useEffect, useImperativeHandle, useMemo, useRef } from 'react';
3
3
  import { useFormContext } from './form';
4
- import { clsx, getValueOnChange, queryDeep, stringifyField, useDerivedState, toArray } from '../../utils';
4
+ import { clsx, getValueOnChange, queryDeep, stringifyField, useDerivedState, toArray, getShortID } from '../../utils';
5
5
  import { DescriptionItem } from '../descriptions';
6
6
  import { classes } from './form.style';
7
7
  import { Collapse } from '../transitionBase';
@@ -18,6 +18,10 @@ export const FormItem = ({ ref, wrapperRef, field, initialValue, label = '', rul
18
18
  return queryDeep(formValue, field);
19
19
  }
20
20
  }, [formValue, field]);
21
+ const [randomKey] = useDerivedState(prev => {
22
+ // fieldValue变为undefined时,需要更新key以强制重渲染组件
23
+ return typeof fieldValue === 'undefined' ? getShortID() : prev;
24
+ }, [fieldValue]);
21
25
  /**
22
26
  * --------------------------------------------------------------------
23
27
  * 校验
@@ -132,11 +136,12 @@ export const FormItem = ({ ref, wrapperRef, field, initialValue, label = '', rul
132
136
  : {}, {
133
137
  ...props.size && { size: props.size },
134
138
  ...innerError.current ? { color: 'error' } : {}
135
- });
139
+ }, randomKey.current);
136
140
  }
137
141
  if (isValidElement(children)) {
138
142
  const { props: childProps } = children;
139
143
  return cloneElement(children, {
144
+ key: randomKey.current,
140
145
  ...typeof field !== 'undefined' && {
141
146
  ...typeof fieldValue === 'boolean'
142
147
  ? { checked: childProps.checked ?? fieldValue }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canlooks/can-ui",
3
- "version": "0.0.55",
3
+ "version": "0.0.57",
4
4
  "author": "C.CanLiang <canlooks@gmail.com>",
5
5
  "description": "My ui framework",
6
6
  "license": "MIT",