antd-mobile 5.42.2-alpha.0 → 5.42.2-alpha.2

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.
Files changed (36) hide show
  1. package/2x/bundle/antd-mobile.cjs.development.js +14 -6
  2. package/2x/bundle/antd-mobile.cjs.js +4 -4
  3. package/2x/bundle/antd-mobile.es.development.js +14 -6
  4. package/2x/bundle/antd-mobile.es.js +373 -373
  5. package/2x/bundle/antd-mobile.umd.development.js +14 -6
  6. package/2x/bundle/antd-mobile.umd.js +4 -4
  7. package/2x/cjs/components/virtual-input/use-click-outside.d.ts +1 -1
  8. package/2x/cjs/components/virtual-input/use-click-outside.js +10 -4
  9. package/2x/cjs/components/virtual-input/virtual-input.js +11 -3
  10. package/2x/cjs/utils/with-stop-propagation.d.ts +2 -2
  11. package/2x/cjs/utils/with-stop-propagation.js +5 -0
  12. package/2x/es/components/virtual-input/use-click-outside.d.ts +1 -1
  13. package/2x/es/components/virtual-input/use-click-outside.js +10 -4
  14. package/2x/es/components/virtual-input/virtual-input.js +11 -3
  15. package/2x/es/utils/with-stop-propagation.d.ts +2 -2
  16. package/2x/es/utils/with-stop-propagation.js +5 -0
  17. package/2x/package.json +1 -1
  18. package/bundle/antd-mobile.cjs.development.js +14 -6
  19. package/bundle/antd-mobile.cjs.js +4 -4
  20. package/bundle/antd-mobile.compatible.umd.js +1 -1
  21. package/bundle/antd-mobile.es.development.js +14 -6
  22. package/bundle/antd-mobile.es.js +373 -373
  23. package/bundle/antd-mobile.umd.development.js +14 -6
  24. package/bundle/antd-mobile.umd.js +4 -4
  25. package/cjs/components/virtual-input/use-click-outside.d.ts +1 -1
  26. package/cjs/components/virtual-input/use-click-outside.js +10 -4
  27. package/cjs/components/virtual-input/virtual-input.js +11 -3
  28. package/cjs/utils/with-stop-propagation.d.ts +2 -2
  29. package/cjs/utils/with-stop-propagation.js +5 -0
  30. package/es/components/virtual-input/use-click-outside.d.ts +1 -1
  31. package/es/components/virtual-input/use-click-outside.js +10 -4
  32. package/es/components/virtual-input/virtual-input.js +11 -3
  33. package/es/utils/with-stop-propagation.d.ts +2 -2
  34. package/es/utils/with-stop-propagation.js +5 -0
  35. package/package.json +1 -1
  36. package/umd/antd-mobile.js +1 -1
@@ -1,2 +1,2 @@
1
- declare function useClickOutside(handler: (event: MouseEvent) => void, ref: React.RefObject<HTMLElement>): void;
1
+ declare function useClickOutside(handler: (event: MouseEvent) => void, ref: React.RefObject<HTMLElement>, hasKeyboardProps?: boolean): void;
2
2
  export default useClickOutside;
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  var _react = require("react");
8
8
  // 监听点击组件外部的事件
9
- function useClickOutside(handler, ref) {
9
+ function useClickOutside(handler, ref, hasKeyboardProps = false) {
10
10
  (0, _react.useEffect)(() => {
11
11
  function handleClick(event) {
12
12
  if (!ref.current || ref.current.contains(event.target)) {
@@ -14,10 +14,16 @@ function useClickOutside(handler, ref) {
14
14
  }
15
15
  handler(event);
16
16
  }
17
- // 在捕获阶段监听,以确保在事件被阻止传播之前触发
18
- document.addEventListener('click', handleClick, true);
17
+ // 向前兼容逻辑:
18
+ // 1. 对于有键盘属性的 VirtualInput,在捕获阶段监听:
19
+ // 这是为了确保在事件被阻止传播之前触发。比如输入框中的单个数字 click 事件会 stopPropagation, 但这里依然能捕获到
20
+ // 2. 对于无键盘属性的 VirtualInput 组件,在冒泡阶段监听:
21
+ // 这种情况通常是 VirtualInput + NumberKeyboard 为兄弟关系,在以前版本中点击 NumberKeyboard **不会**触发 VirtualInput 的 blur 事件
22
+ // 原先原理:通过 NumberKeyboard 内部 onMouseDown 时 preventDefault 阻止的 VirtualInput 内原生的 blur 事件
23
+ // 新的原理:NumberKeyboard 的 Popup 默认会 stopPropagation click, 这里在冒泡阶段监听不到,不会调用 VirtualInput 的 onBlur 回调(非原生事件)。
24
+ document.addEventListener('click', handleClick, hasKeyboardProps ? true : false);
19
25
  return () => {
20
- document.removeEventListener('click', handleClick, true);
26
+ document.removeEventListener('click', handleClick, hasKeyboardProps ? true : false);
21
27
  };
22
28
  }, [handler, ref]);
23
29
  }
@@ -100,7 +100,7 @@ const VirtualInput = (0, _react.forwardRef)((props, ref) => {
100
100
  }
101
101
  (0, _useClickOutside.default)(() => {
102
102
  setBlur();
103
- }, rootRef);
103
+ }, rootRef, !!mergedProps.keyboard);
104
104
  const keyboard = mergedProps.keyboard;
105
105
  const keyboardElement = keyboard && _react.default.cloneElement(keyboard, {
106
106
  onInput: v => {
@@ -128,15 +128,23 @@ const VirtualInput = (0, _react.forwardRef)((props, ref) => {
128
128
  },
129
129
  visible: hasFocus,
130
130
  onClose: () => {
131
- var _a, _b;
131
+ var _a, _b, _c;
132
+ const activeElement = document.activeElement;
133
+ if (activeElement === contentRef.current) {
134
+ // 点击 NumberKeyboard 的确认和关闭时不会触发输入框的 blur 事件,手动调用让其失焦,否则之后无法 focus
135
+ (_a = contentRef.current) === null || _a === void 0 ? void 0 : _a.blur();
136
+ }
132
137
  setBlur();
133
- (_b = (_a = keyboard.props).onClose) === null || _b === void 0 ? void 0 : _b.call(_a);
138
+ (_c = (_b = keyboard.props).onClose) === null || _c === void 0 ? void 0 : _c.call(_b);
134
139
  },
135
140
  getContainer: null
136
141
  });
137
142
  // 点击输入框时,将光标置于最后
138
143
  const setCaretPositionToEnd = e => {
139
144
  var _a, _b, _c;
145
+ if (mergedProps.disabled) {
146
+ return;
147
+ }
140
148
  if (caretPosition !== value.length) {
141
149
  setCaretPosition(value.length);
142
150
  (_b = (_a = mergedProps.cursor) === null || _a === void 0 ? void 0 : _a.onMove) === null || _b === void 0 ? void 0 : _b.call(_a, value.length);
@@ -1,4 +1,4 @@
1
- import React from 'react';
2
1
  import type { ReactElement } from 'react';
2
+ import React from 'react';
3
3
  export declare type PropagationEvent = 'click' | 'touchstart';
4
- export declare function withStopPropagation(events: PropagationEvent[], element: ReactElement): React.ReactElement<any, string | React.JSXElementConstructor<any>>;
4
+ export declare function withStopPropagation(events: PropagationEvent[], element: ReactElement): ReactElement<any, string | React.JSXElementConstructor<any>>;
@@ -17,6 +17,11 @@ function withStopPropagation(events, element) {
17
17
  props[prop] = function (e) {
18
18
  var _a, _b;
19
19
  e.stopPropagation();
20
+ // react <=16 事件绑定在 document,因此上面的 stopPropagation 无法阻止事件冒泡到用原生方式监听的 document 的事件回调中
21
+ // 增加 stopImmediatePropagation 可以阻止事件在**后绑定**的 document 上的回调执行
22
+ // 专用于解决 VirtualInput useClickOutside 的问题
23
+ // react >=17 则无此问题,事件会绑定在 React 根节点上
24
+ e.nativeEvent.stopImmediatePropagation();
20
25
  (_b = (_a = element.props)[prop]) === null || _b === void 0 ? void 0 : _b.call(_a, e);
21
26
  };
22
27
  }
@@ -1,2 +1,2 @@
1
- declare function useClickOutside(handler: (event: MouseEvent) => void, ref: React.RefObject<HTMLElement>): void;
1
+ declare function useClickOutside(handler: (event: MouseEvent) => void, ref: React.RefObject<HTMLElement>, hasKeyboardProps?: boolean): void;
2
2
  export default useClickOutside;
@@ -1,6 +1,6 @@
1
1
  import { useEffect } from 'react';
2
2
  // 监听点击组件外部的事件
3
- function useClickOutside(handler, ref) {
3
+ function useClickOutside(handler, ref, hasKeyboardProps = false) {
4
4
  useEffect(() => {
5
5
  function handleClick(event) {
6
6
  if (!ref.current || ref.current.contains(event.target)) {
@@ -8,10 +8,16 @@ function useClickOutside(handler, ref) {
8
8
  }
9
9
  handler(event);
10
10
  }
11
- // 在捕获阶段监听,以确保在事件被阻止传播之前触发
12
- document.addEventListener('click', handleClick, true);
11
+ // 向前兼容逻辑:
12
+ // 1. 对于有键盘属性的 VirtualInput,在捕获阶段监听:
13
+ // 这是为了确保在事件被阻止传播之前触发。比如输入框中的单个数字 click 事件会 stopPropagation, 但这里依然能捕获到
14
+ // 2. 对于无键盘属性的 VirtualInput 组件,在冒泡阶段监听:
15
+ // 这种情况通常是 VirtualInput + NumberKeyboard 为兄弟关系,在以前版本中点击 NumberKeyboard **不会**触发 VirtualInput 的 blur 事件
16
+ // 原先原理:通过 NumberKeyboard 内部 onMouseDown 时 preventDefault 阻止的 VirtualInput 内原生的 blur 事件
17
+ // 新的原理:NumberKeyboard 的 Popup 默认会 stopPropagation click, 这里在冒泡阶段监听不到,不会调用 VirtualInput 的 onBlur 回调(非原生事件)。
18
+ document.addEventListener('click', handleClick, hasKeyboardProps ? true : false);
13
19
  return () => {
14
- document.removeEventListener('click', handleClick, true);
20
+ document.removeEventListener('click', handleClick, hasKeyboardProps ? true : false);
15
21
  };
16
22
  }, [handler, ref]);
17
23
  }
@@ -91,7 +91,7 @@ export const VirtualInput = forwardRef((props, ref) => {
91
91
  }
92
92
  useClickOutside(() => {
93
93
  setBlur();
94
- }, rootRef);
94
+ }, rootRef, !!mergedProps.keyboard);
95
95
  const keyboard = mergedProps.keyboard;
96
96
  const keyboardElement = keyboard && React.cloneElement(keyboard, {
97
97
  onInput: v => {
@@ -119,15 +119,23 @@ export const VirtualInput = forwardRef((props, ref) => {
119
119
  },
120
120
  visible: hasFocus,
121
121
  onClose: () => {
122
- var _a, _b;
122
+ var _a, _b, _c;
123
+ const activeElement = document.activeElement;
124
+ if (activeElement === contentRef.current) {
125
+ // 点击 NumberKeyboard 的确认和关闭时不会触发输入框的 blur 事件,手动调用让其失焦,否则之后无法 focus
126
+ (_a = contentRef.current) === null || _a === void 0 ? void 0 : _a.blur();
127
+ }
123
128
  setBlur();
124
- (_b = (_a = keyboard.props).onClose) === null || _b === void 0 ? void 0 : _b.call(_a);
129
+ (_c = (_b = keyboard.props).onClose) === null || _c === void 0 ? void 0 : _c.call(_b);
125
130
  },
126
131
  getContainer: null
127
132
  });
128
133
  // 点击输入框时,将光标置于最后
129
134
  const setCaretPositionToEnd = e => {
130
135
  var _a, _b, _c;
136
+ if (mergedProps.disabled) {
137
+ return;
138
+ }
131
139
  if (caretPosition !== value.length) {
132
140
  setCaretPosition(value.length);
133
141
  (_b = (_a = mergedProps.cursor) === null || _a === void 0 ? void 0 : _a.onMove) === null || _b === void 0 ? void 0 : _b.call(_a, value.length);
@@ -1,4 +1,4 @@
1
- import React from 'react';
2
1
  import type { ReactElement } from 'react';
2
+ import React from 'react';
3
3
  export declare type PropagationEvent = 'click' | 'touchstart';
4
- export declare function withStopPropagation(events: PropagationEvent[], element: ReactElement): React.ReactElement<any, string | React.JSXElementConstructor<any>>;
4
+ export declare function withStopPropagation(events: PropagationEvent[], element: ReactElement): ReactElement<any, string | React.JSXElementConstructor<any>>;
@@ -10,6 +10,11 @@ export function withStopPropagation(events, element) {
10
10
  props[prop] = function (e) {
11
11
  var _a, _b;
12
12
  e.stopPropagation();
13
+ // react <=16 事件绑定在 document,因此上面的 stopPropagation 无法阻止事件冒泡到用原生方式监听的 document 的事件回调中
14
+ // 增加 stopImmediatePropagation 可以阻止事件在**后绑定**的 document 上的回调执行
15
+ // 专用于解决 VirtualInput useClickOutside 的问题
16
+ // react >=17 则无此问题,事件会绑定在 React 根节点上
17
+ e.nativeEvent.stopImmediatePropagation();
13
18
  (_b = (_a = element.props)[prop]) === null || _b === void 0 ? void 0 : _b.call(_a, e);
14
19
  };
15
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "antd-mobile",
3
- "version": "5.42.2-alpha.0",
3
+ "version": "5.42.2-alpha.2",
4
4
  "homepage": "https://github.com/ant-design/ant-design-mobile#readme",
5
5
  "bugs": {
6
6
  "url": "https://github.com/ant-design/ant-design-mobile/issues"