@zat-design/sisyphus-react 4.7.1 → 4.7.2-beta.1

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.
@@ -1,7 +1,7 @@
1
1
  import { Fragment, jsx } from "react/jsx-runtime";
2
2
  import isFunction from "lodash/isFunction";
3
3
  import isObject from "lodash/isObject";
4
- import React from "react";
4
+ import React, { useLayoutEffect, useRef, useState } from "react";
5
5
  import { Modal } from "antd";
6
6
  import ProForm from "../../index";
7
7
  import locale from "../../../locale";
@@ -9,45 +9,154 @@ const defaultConfirmProps = {
9
9
  title: locale.ProForm.confirmTitle,
10
10
  content: locale.ProForm.confirmContent
11
11
  };
12
+ const isTextEvent = (event) => {
13
+ const target = event == null ? void 0 : event.target;
14
+ return (target == null ? void 0 : target.tagName) === "TEXTAREA" || (target == null ? void 0 : target.tagName) === "INPUT" && ["text", "search", "password", "email", "tel", "url"].includes(target.type);
15
+ };
12
16
  const ConfirmWrapper = (props) => {
13
17
  const { onChange, children, confirm, otherProps, ...rest } = props;
14
18
  const { form, namePath, name, index, listName } = ProForm.useFieldProps() || {};
19
+ const [draft, setDraft] = useState();
20
+ const composing = useRef(false);
21
+ const compositionValue = useRef(void 0);
22
+ const revision = useRef(0);
23
+ useLayoutEffect(() => {
24
+ revision.current += 1;
25
+ composing.current = false;
26
+ compositionValue.current = void 0;
27
+ setDraft(void 0);
28
+ return () => {
29
+ revision.current += 1;
30
+ };
31
+ }, [rest.value]);
15
32
  const handleChange = async (value, ...other) => {
16
- const _value = (value == null ? void 0 : value.target) ? value.target.value : value;
17
- const confirmProps = isFunction(confirm) ? await confirm(value, form.getFieldsValue(), {
18
- option: other[0],
19
- form,
20
- namePath,
21
- name,
22
- index,
23
- preValue: form.getFieldValue(listName || name)
24
- }) : confirm;
25
- if (confirmProps === false) {
33
+ var _a;
34
+ const target = typeof value === "object" && value !== null && "target" in value ? value.target : void 0;
35
+ const _value = typeof target === "object" && target !== null ? "value" in target ? target.value : void 0 : value;
36
+ const text = isTextEvent(value);
37
+ const currentRevision = text ? ++revision.current : revision.current;
38
+ const isCurrent = () => !text || currentRevision === revision.current;
39
+ const rollback = () => {
40
+ if (text && isCurrent())
41
+ setDraft(void 0);
42
+ };
43
+ const cancel = () => {
44
+ rollback();
45
+ if (text && isCurrent())
46
+ revision.current += 1;
47
+ };
48
+ const commit = () => {
49
+ if (!isCurrent())
50
+ return;
51
+ rollback();
26
52
  onChange == null ? void 0 : onChange(_value, ...other);
53
+ };
54
+ if (text) {
55
+ setDraft(value.target.value);
56
+ if (composing.current)
57
+ return;
58
+ }
59
+ try {
60
+ const confirmProps = isFunction(confirm) ? await confirm(value, form.getFieldsValue(), {
61
+ option: other[0],
62
+ form,
63
+ namePath,
64
+ name,
65
+ index,
66
+ preValue: form.getFieldValue(listName || name)
67
+ }) : confirm;
68
+ if (!isCurrent())
69
+ return;
70
+ if (confirmProps === false) {
71
+ commit();
72
+ } else if (confirmProps === true) {
73
+ Modal.confirm({
74
+ ...defaultConfirmProps,
75
+ onOk: commit,
76
+ ...text ? { onCancel: cancel } : {}
77
+ });
78
+ } else if (isObject(confirmProps)) {
79
+ Modal.confirm({
80
+ ..."title" in confirmProps || "content" in confirmProps ? {} : defaultConfirmProps,
81
+ ...confirmProps,
82
+ ...text ? {
83
+ // Modal uses callback arity to decide who closes the dialog.
84
+ onCancel: ((_a = confirmProps.onCancel) == null ? void 0 : _a.length) ? (close) => {
85
+ var _a2;
86
+ cancel();
87
+ return (_a2 = confirmProps.onCancel) == null ? void 0 : _a2.call(confirmProps, close);
88
+ } : () => {
89
+ var _a2;
90
+ cancel();
91
+ return (_a2 = confirmProps.onCancel) == null ? void 0 : _a2.call(confirmProps);
92
+ }
93
+ } : {},
94
+ onOk: async (...arg) => {
95
+ var _a2;
96
+ if (!isCurrent())
97
+ return;
98
+ try {
99
+ await ((_a2 = confirmProps.onOk) == null ? void 0 : _a2.call(confirmProps, ...arg));
100
+ commit();
101
+ } catch (error) {
102
+ rollback();
103
+ throw error;
104
+ }
105
+ }
106
+ });
107
+ } else {
108
+ rollback();
109
+ }
110
+ } catch (error) {
111
+ rollback();
112
+ throw error;
113
+ }
114
+ };
115
+ const childProps = React.isValidElement(children) ? children.props : {};
116
+ const onCompositionStart = (event) => {
117
+ var _a;
118
+ if (isTextEvent(event)) {
119
+ revision.current += 1;
120
+ composing.current = true;
121
+ compositionValue.current = void 0;
122
+ setDraft(event.currentTarget.value);
27
123
  }
28
- if (confirmProps === true) {
29
- Modal.confirm({
30
- ...defaultConfirmProps,
31
- onOk: () => onChange == null ? void 0 : onChange(_value, ...other)
32
- });
124
+ (_a = rest.onCompositionStart ?? childProps.onCompositionStart) == null ? void 0 : _a(event);
125
+ };
126
+ const onCompositionEnd = (event) => {
127
+ var _a;
128
+ let result;
129
+ if (isTextEvent(event) && composing.current) {
130
+ composing.current = false;
131
+ compositionValue.current = event.currentTarget.value;
132
+ result = handleChange(event);
133
+ }
134
+ (_a = rest.onCompositionEnd ?? childProps.onCompositionEnd) == null ? void 0 : _a(event);
135
+ return result;
136
+ };
137
+ const onInputChange = (value, ...other) => {
138
+ if (isTextEvent(value) && compositionValue.current !== void 0) {
139
+ const previousComposition = compositionValue.current;
140
+ compositionValue.current = void 0;
141
+ if (value.target.value === previousComposition)
142
+ return;
33
143
  }
34
- if (isObject(confirmProps)) {
35
- Modal.confirm({
36
- ..."title" in confirmProps || "content" in confirmProps ? {} : defaultConfirmProps,
37
- ...confirmProps,
38
- onOk: async (...arg) => {
39
- var _a;
40
- await ((_a = confirmProps.onOk) == null ? void 0 : _a.call(confirmProps, ...arg));
41
- onChange == null ? void 0 : onChange(_value, ...other);
42
- }
43
- });
144
+ return handleChange(value, ...other);
145
+ };
146
+ const onKeyPress = (event) => {
147
+ var _a;
148
+ if (isTextEvent(event) && event.key === "Enter" && (composing.current || event.nativeEvent.isComposing || event.keyCode === 229)) {
149
+ event.stopPropagation();
44
150
  }
151
+ (_a = rest.onKeyPress ?? childProps.onKeyPress) == null ? void 0 : _a(event);
45
152
  };
46
153
  return /* @__PURE__ */ jsx(Fragment, { children: React.isValidElement(children) && React.cloneElement(children, {
47
154
  ...rest,
48
- // @ts-ignore
49
- value: rest.value === void 0 ? null : rest.value,
50
- onChange: handleChange
155
+ value: draft !== void 0 ? draft : rest.value === void 0 ? null : rest.value,
156
+ onChange: onInputChange,
157
+ onCompositionStart,
158
+ onCompositionEnd,
159
+ onKeyPress
51
160
  }) });
52
161
  };
53
162
  var ConfirmWrapper_default = ConfirmWrapper;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zat-design/sisyphus-react",
3
- "version": "4.7.1",
3
+ "version": "4.7.2-beta.1",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public",