@qsxy/element-plus-react 1.0.1 → 1.0.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.
- package/dist/Calendar/Calendar.js +12 -3
- package/dist/Calendar/Footer.js +4 -2
- package/dist/Calendar/QuarterPanel.d.ts +4 -0
- package/dist/Calendar/QuarterPanel.js +109 -0
- package/dist/Calendar/WeekPanel.js +1 -1
- package/dist/Calendar/typings.d.ts +1 -1
- package/dist/Card/Card.js +9 -4
- package/dist/Card/typings.d.ts +1 -1
- package/dist/Cascader/Cascader.js +1 -1
- package/dist/DatePicker/DatePicker.js +38 -12
- package/dist/DatePicker/DateRangePicker.js +27 -9
- package/dist/DatePicker/main.js +1 -1
- package/dist/DatePicker/typings.d.ts +1 -1
- package/dist/DateTimePicker/DateTimePicker.js +2 -2
- package/dist/Input/Input.1.d.ts +16 -0
- package/dist/Input/Input.1.js +376 -0
- package/dist/Input/Input.js +137 -121
- package/dist/Input/InputRange.js +1 -1
- package/dist/Input/TextArea.js +87 -12
- package/dist/Input/typings.d.ts +15 -5
- package/dist/Input/useComposition.d.ts +23 -0
- package/dist/Input/useComposition.js +63 -0
- package/dist/Input/utils.d.ts +6 -0
- package/dist/Input/utils.js +91 -0
- package/dist/InputNumber/InputNumber.js +1 -1
- package/dist/InputNumber/typings.d.ts +1 -1
- package/dist/Select/Option.js +30 -14
- package/dist/Select/SelectContext.d.ts +3 -4
- package/dist/Select/SelectCore.d.ts +2 -2
- package/dist/Select/SelectCore.js +164 -447
- package/dist/Select/SelectDropdown.js +67 -145
- package/dist/Select/typings.d.ts +47 -21
- package/dist/Select/useSelect.d.ts +125 -0
- package/dist/Select/useSelect.js +516 -0
- package/dist/Table/util.js +6 -5
- package/dist/Tag/Tag.js +22 -17
- package/dist/TimePicker/TimePicker.js +2 -2
- package/dist/TreeSelect/TreeSelect.js +2 -2
- package/dist/Util/aria.d.ts +25 -0
- package/dist/Util/aria.js +118 -0
- package/dist/hooks/useCalcInputWidth.d.ts +8 -0
- package/dist/hooks/useCalcInputWidth.js +30 -0
- package/dist/hooks/useComposition.d.ts +16 -0
- package/dist/hooks/useComposition.js +39 -0
- package/dist/hooks/useCursor.d.ts +2 -0
- package/dist/hooks/useCursor.js +52 -0
- package/dist/hooks/useFocusController.d.ts +27 -0
- package/dist/hooks/useFocusController.js +72 -0
- package/dist/index.css +1 -1
- package/dist/locale/en.d.ts +15 -0
- package/dist/locale/en.js +15 -0
- package/dist/locale/zhCn.d.ts +15 -0
- package/dist/locale/zhCn.js +15 -0
- package/package.json +1 -1
- package/theme-chalk/calendar/index.scss +1 -0
- package/theme-chalk/calendar/quarter-table.scss +78 -0
- package/theme-chalk/common/var.scss +1 -1
- package/theme-chalk/input/input.scss +424 -578
- package/theme-chalk/input/input.scss--bak +578 -0
- package/theme-chalk/select/index.scss +272 -247
- package/theme-chalk/select/index.scss--bak +247 -0
- package/theme-chalk/select/option-group.scss +2 -2
- package/theme-chalk/select/option.scss +2 -2
- package/theme-chalk/select/select-dropdown.scss +2 -2
- package/theme-chalk/tag.scss +181 -203
- package/theme-chalk/tag.scss--bak +203 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
var FOCUSABLE_ELEMENT_SELECTORS = 'a[href],button:not([disabled]),button:not([hidden]),:not([tabindex="-1"]),input:not([disabled]),input:not([type="hidden"]),select:not([disabled]),textarea:not([disabled])';
|
|
2
|
+
var isHTMLElement = function isHTMLElement(e) {
|
|
3
|
+
if (typeof Element === 'undefined') {
|
|
4
|
+
return false;
|
|
5
|
+
}
|
|
6
|
+
return e instanceof Element;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Determine if the testing element is visible on screen no matter if its on the viewport or not
|
|
11
|
+
*/
|
|
12
|
+
export var isVisible = function isVisible(element) {
|
|
13
|
+
if (process.env.NODE_ENV === 'test') {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
var computed = getComputedStyle(element);
|
|
17
|
+
// element.offsetParent won't work on fix positioned
|
|
18
|
+
// WARNING: potential issue here, going to need some expert advices on this issue
|
|
19
|
+
return computed.position === 'fixed' ? false : element.offsetParent !== null;
|
|
20
|
+
};
|
|
21
|
+
export var obtainAllFocusableElements = function obtainAllFocusableElements(element) {
|
|
22
|
+
return Array.from(element.querySelectorAll(FOCUSABLE_ELEMENT_SELECTORS)).filter(function (item) {
|
|
23
|
+
return isFocusable(item) && isVisible(item);
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @desc Determine if target element is focusable
|
|
29
|
+
* @param element {HTMLElement}
|
|
30
|
+
* @returns {Boolean} true if it is focusable
|
|
31
|
+
*/
|
|
32
|
+
export var isFocusable = function isFocusable(element) {
|
|
33
|
+
if (element.tabIndex > 0 || element.tabIndex === 0 && element.getAttribute('tabIndex') !== null) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
if (element.tabIndex < 0 || element.hasAttribute('disabled') || element.getAttribute('aria-disabled') === 'true') {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
switch (element.nodeName) {
|
|
40
|
+
case 'A':
|
|
41
|
+
{
|
|
42
|
+
// casting current element to Specific HTMLElement in order to be more type precise
|
|
43
|
+
return !!element.href && element.rel !== 'ignore';
|
|
44
|
+
}
|
|
45
|
+
case 'INPUT':
|
|
46
|
+
{
|
|
47
|
+
return !(element.type === 'hidden' || element.type === 'file');
|
|
48
|
+
}
|
|
49
|
+
case 'BUTTON':
|
|
50
|
+
case 'SELECT':
|
|
51
|
+
case 'TEXTAREA':
|
|
52
|
+
{
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
default:
|
|
56
|
+
{
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Trigger an event
|
|
64
|
+
* mouseenter, mouseleave, mouseover, keyup, change, click, etc.
|
|
65
|
+
* @param {HTMLElement} elm
|
|
66
|
+
* @param {String} name
|
|
67
|
+
* @param {*} opts
|
|
68
|
+
*/
|
|
69
|
+
export var triggerEvent = function triggerEvent(elm, name) {
|
|
70
|
+
var eventName;
|
|
71
|
+
if (name.includes('mouse') || name.includes('click')) {
|
|
72
|
+
eventName = 'MouseEvents';
|
|
73
|
+
} else if (name.includes('key')) {
|
|
74
|
+
eventName = 'KeyboardEvent';
|
|
75
|
+
} else {
|
|
76
|
+
eventName = 'HTMLEvents';
|
|
77
|
+
}
|
|
78
|
+
var evt = document.createEvent(eventName);
|
|
79
|
+
for (var _len = arguments.length, opts = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
|
80
|
+
opts[_key - 2] = arguments[_key];
|
|
81
|
+
}
|
|
82
|
+
evt.initEvent.apply(evt, [name].concat(opts));
|
|
83
|
+
elm.dispatchEvent(evt);
|
|
84
|
+
return elm;
|
|
85
|
+
};
|
|
86
|
+
export var isLeaf = function isLeaf(el) {
|
|
87
|
+
return !el.getAttribute('aria-owns');
|
|
88
|
+
};
|
|
89
|
+
export var getSibling = function getSibling(el, distance, elClass) {
|
|
90
|
+
var parentNode = el.parentNode;
|
|
91
|
+
if (!parentNode) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
var siblings = parentNode.querySelectorAll(elClass);
|
|
95
|
+
var index = Array.prototype.indexOf.call(siblings, el);
|
|
96
|
+
return siblings[index + distance] || null;
|
|
97
|
+
};
|
|
98
|
+
export var focusElement = function focusElement(el, options) {
|
|
99
|
+
if (!el || !el.focus) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
var cleanup = false;
|
|
103
|
+
if (isHTMLElement(el) && !isFocusable(el) && !el.getAttribute('tabindex')) {
|
|
104
|
+
el.setAttribute('tabindex', '-1');
|
|
105
|
+
cleanup = true;
|
|
106
|
+
}
|
|
107
|
+
el.focus(options);
|
|
108
|
+
if (isHTMLElement(el) && cleanup) {
|
|
109
|
+
el.removeAttribute('tabindex');
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
export var focusNode = function focusNode(el) {
|
|
113
|
+
if (!el) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
focusElement(el);
|
|
117
|
+
!isLeaf(el) && el.click();
|
|
118
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
2
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
3
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
4
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
5
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
6
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
7
|
+
import { useMemo, useRef, useState } from 'react';
|
|
8
|
+
import { useResizeObserver } from "./useResizeObserver";
|
|
9
|
+
export function useCalcInputWidth() {
|
|
10
|
+
var calculatorRef = useRef();
|
|
11
|
+
var _useState = useState(0),
|
|
12
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
13
|
+
calculatorWidth = _useState2[0],
|
|
14
|
+
setCalculatorWidth = _useState2[1];
|
|
15
|
+
var inputStyle = useMemo(function () {
|
|
16
|
+
return {
|
|
17
|
+
minWidth: "".concat(Math.max(calculatorWidth, 11), "px")
|
|
18
|
+
};
|
|
19
|
+
}, [calculatorWidth]);
|
|
20
|
+
var resetCalculatorWidth = function resetCalculatorWidth() {
|
|
21
|
+
var _calculatorRef$curren, _calculatorRef$curren2;
|
|
22
|
+
setCalculatorWidth((_calculatorRef$curren = (_calculatorRef$curren2 = calculatorRef.current) === null || _calculatorRef$curren2 === void 0 ? void 0 : _calculatorRef$curren2.getBoundingClientRect().width) !== null && _calculatorRef$curren !== void 0 ? _calculatorRef$curren : 0);
|
|
23
|
+
};
|
|
24
|
+
useResizeObserver(calculatorRef, resetCalculatorWidth);
|
|
25
|
+
return {
|
|
26
|
+
calculatorRef: calculatorRef,
|
|
27
|
+
calculatorWidth: calculatorWidth,
|
|
28
|
+
inputStyle: inputStyle
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { CompositionEvent } from 'react';
|
|
2
|
+
interface UseCompositionOptions {
|
|
3
|
+
afterComposition: (event: CompositionEvent<HTMLInputElement>) => void;
|
|
4
|
+
compositionstart?: (evt: CompositionEvent<HTMLInputElement>) => void;
|
|
5
|
+
compositionupdate?: (evt: CompositionEvent<HTMLInputElement>) => void;
|
|
6
|
+
compositionend?: (evt: CompositionEvent<HTMLInputElement>) => void;
|
|
7
|
+
}
|
|
8
|
+
export declare function useComposition({ afterComposition, compositionstart, compositionupdate, compositionend }: UseCompositionOptions): {
|
|
9
|
+
isComposing: import("react").MutableRefObject<boolean>;
|
|
10
|
+
handleComposition: (event: CompositionEvent<HTMLInputElement>) => void;
|
|
11
|
+
handleCompositionStart: (event: CompositionEvent<HTMLInputElement>) => void;
|
|
12
|
+
handleCompositionUpdate: (event: CompositionEvent<HTMLInputElement>) => void;
|
|
13
|
+
handleCompositionEnd: (event: CompositionEvent<HTMLInputElement>) => void;
|
|
14
|
+
};
|
|
15
|
+
export declare const isKorean: (text: string) => boolean;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { useRef } from 'react';
|
|
2
|
+
export function useComposition(_ref) {
|
|
3
|
+
var afterComposition = _ref.afterComposition,
|
|
4
|
+
compositionstart = _ref.compositionstart,
|
|
5
|
+
compositionupdate = _ref.compositionupdate,
|
|
6
|
+
compositionend = _ref.compositionend;
|
|
7
|
+
var isComposing = useRef(false);
|
|
8
|
+
var handleCompositionStart = function handleCompositionStart(event) {
|
|
9
|
+
compositionstart === null || compositionstart === void 0 || compositionstart(event);
|
|
10
|
+
isComposing.current = true;
|
|
11
|
+
};
|
|
12
|
+
var handleCompositionUpdate = function handleCompositionUpdate(event) {
|
|
13
|
+
var _event$target;
|
|
14
|
+
compositionupdate === null || compositionupdate === void 0 || compositionupdate(event);
|
|
15
|
+
var text = (_event$target = event.target) === null || _event$target === void 0 ? void 0 : _event$target.value;
|
|
16
|
+
var lastCharacter = text[text.length - 1] || '';
|
|
17
|
+
isComposing.current = !isKorean(lastCharacter);
|
|
18
|
+
};
|
|
19
|
+
var handleCompositionEnd = function handleCompositionEnd(event) {
|
|
20
|
+
compositionend === null || compositionend === void 0 || compositionend(event);
|
|
21
|
+
if (isComposing.current) {
|
|
22
|
+
isComposing.current = false;
|
|
23
|
+
afterComposition(event);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var handleComposition = function handleComposition(event) {
|
|
27
|
+
event.type === 'compositionend' ? handleCompositionEnd(event) : handleCompositionUpdate(event);
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
isComposing: isComposing,
|
|
31
|
+
handleComposition: handleComposition,
|
|
32
|
+
handleCompositionStart: handleCompositionStart,
|
|
33
|
+
handleCompositionUpdate: handleCompositionUpdate,
|
|
34
|
+
handleCompositionEnd: handleCompositionEnd
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export var isKorean = function isKorean(text) {
|
|
38
|
+
return /([\uAC00-\uD7AF\u3130-\u318F])+/gi.test(text);
|
|
39
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Keep input cursor in the correct position when we use formatter.
|
|
2
|
+
export function useCursor(input) {
|
|
3
|
+
var selectionInfo;
|
|
4
|
+
function recordCursor() {
|
|
5
|
+
if (input.current == undefined) {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
var _input$current = input.current,
|
|
9
|
+
selectionStart = _input$current.selectionStart,
|
|
10
|
+
selectionEnd = _input$current.selectionEnd,
|
|
11
|
+
value = _input$current.value;
|
|
12
|
+
if (selectionStart == null || selectionEnd == null) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
var beforeTxt = value.slice(0, Math.max(0, selectionStart));
|
|
16
|
+
var afterTxt = value.slice(Math.max(0, selectionEnd));
|
|
17
|
+
selectionInfo = {
|
|
18
|
+
selectionStart: selectionStart,
|
|
19
|
+
selectionEnd: selectionEnd,
|
|
20
|
+
value: value,
|
|
21
|
+
beforeTxt: beforeTxt,
|
|
22
|
+
afterTxt: afterTxt
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function setCursor() {
|
|
26
|
+
if (input.current == undefined || selectionInfo == undefined) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
var value = input.current.value;
|
|
30
|
+
var _selectionInfo = selectionInfo,
|
|
31
|
+
beforeTxt = _selectionInfo.beforeTxt,
|
|
32
|
+
afterTxt = _selectionInfo.afterTxt,
|
|
33
|
+
selectionStart = _selectionInfo.selectionStart;
|
|
34
|
+
if (beforeTxt == undefined || afterTxt == undefined || selectionStart == undefined) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
var startPos = value.length;
|
|
38
|
+
if (value.endsWith(afterTxt)) {
|
|
39
|
+
startPos = value.length - afterTxt.length;
|
|
40
|
+
} else if (value.startsWith(beforeTxt)) {
|
|
41
|
+
startPos = beforeTxt.length;
|
|
42
|
+
} else {
|
|
43
|
+
var beforeLastChar = beforeTxt[selectionStart - 1];
|
|
44
|
+
var newIndex = value.indexOf(beforeLastChar, selectionStart - 1);
|
|
45
|
+
if (newIndex !== -1) {
|
|
46
|
+
startPos = newIndex + 1;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
input.current.setSelectionRange(startPos, startPos);
|
|
50
|
+
}
|
|
51
|
+
return [recordCursor, setCursor];
|
|
52
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { RefObject } from 'react';
|
|
2
|
+
interface UseFocusControllerOptions {
|
|
3
|
+
disabled?: boolean;
|
|
4
|
+
/**
|
|
5
|
+
* return true to cancel focus
|
|
6
|
+
* @param event FocusEvent
|
|
7
|
+
*/
|
|
8
|
+
beforeFocus?: (event: FocusEvent) => boolean | undefined;
|
|
9
|
+
afterFocus?: () => void;
|
|
10
|
+
/**
|
|
11
|
+
* return true to cancel blur
|
|
12
|
+
* @param event FocusEvent
|
|
13
|
+
*/
|
|
14
|
+
beforeBlur?: (event: FocusEvent) => boolean | undefined;
|
|
15
|
+
afterBlur?: () => void;
|
|
16
|
+
}
|
|
17
|
+
export declare function useFocusController<T extends {
|
|
18
|
+
focus: () => void;
|
|
19
|
+
}>(target: RefObject<HTMLElement | undefined>, { disabled, beforeFocus, afterFocus, beforeBlur, afterBlur }?: UseFocusControllerOptions): {
|
|
20
|
+
isFocused: boolean;
|
|
21
|
+
/** Avoid using wrapperRef and handleFocus/handleBlur together */
|
|
22
|
+
wrapperRef: import("react").MutableRefObject<HTMLInputElement>;
|
|
23
|
+
handleFocus: (event: FocusEvent) => void;
|
|
24
|
+
handleBlur: (event: FocusEvent) => void;
|
|
25
|
+
handleClick: (event: Event) => void;
|
|
26
|
+
};
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
2
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
3
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
4
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
5
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
6
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
7
|
+
import isFunction from 'lodash/isFunction';
|
|
8
|
+
import { useRef, useState } from 'react';
|
|
9
|
+
import { isFocusable } from "../Util/aria";
|
|
10
|
+
export function useFocusController(target) {
|
|
11
|
+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
|
12
|
+
disabled = _ref.disabled,
|
|
13
|
+
beforeFocus = _ref.beforeFocus,
|
|
14
|
+
afterFocus = _ref.afterFocus,
|
|
15
|
+
beforeBlur = _ref.beforeBlur,
|
|
16
|
+
afterBlur = _ref.afterBlur;
|
|
17
|
+
var _useState = useState(false),
|
|
18
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
19
|
+
isFocused = _useState2[0],
|
|
20
|
+
setIsFocused = _useState2[1];
|
|
21
|
+
var wrapperRef = useRef(null);
|
|
22
|
+
var handleFocus = function handleFocus(event) {
|
|
23
|
+
var cancelFocus = isFunction(beforeFocus) ? beforeFocus(event) : false;
|
|
24
|
+
if (disabled || isFocused || cancelFocus) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
setIsFocused(true);
|
|
28
|
+
// emit('focus', event);
|
|
29
|
+
afterFocus === null || afterFocus === void 0 || afterFocus();
|
|
30
|
+
};
|
|
31
|
+
var handleBlur = function handleBlur(event) {
|
|
32
|
+
var _wrapperRef$current;
|
|
33
|
+
var cancelBlur = isFunction(beforeBlur) ? beforeBlur(event) : false;
|
|
34
|
+
if (disabled || event.relatedTarget && (_wrapperRef$current = wrapperRef.current) !== null && _wrapperRef$current !== void 0 && _wrapperRef$current.contains(event.relatedTarget) || cancelBlur) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
setIsFocused(false);
|
|
38
|
+
|
|
39
|
+
// emit('blur', event);
|
|
40
|
+
afterBlur === null || afterBlur === void 0 || afterBlur();
|
|
41
|
+
};
|
|
42
|
+
var handleClick = function handleClick(event) {
|
|
43
|
+
var _wrapperRef$current2, _target$current;
|
|
44
|
+
if (disabled || isFocusable(event.target) || (_wrapperRef$current2 = wrapperRef.current) !== null && _wrapperRef$current2 !== void 0 && _wrapperRef$current2.contains(document.activeElement) && wrapperRef.current !== document.activeElement) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
(_target$current = target.current) === null || _target$current === void 0 || _target$current.focus();
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// useMount(() => {
|
|
51
|
+
// if (!wrapperRef.current) {
|
|
52
|
+
// return;
|
|
53
|
+
// }
|
|
54
|
+
// wrapperRef.current.addEventListener('focus', handleFocus, true);
|
|
55
|
+
// wrapperRef.current.addEventListener('blur', handleBlur, true);
|
|
56
|
+
// wrapperRef.current.addEventListener('click', handleClick, true);
|
|
57
|
+
// return () => {
|
|
58
|
+
// wrapperRef.current?.removeEventListener('focus', handleFocus, true);
|
|
59
|
+
// wrapperRef.current?.removeEventListener('blur', handleBlur, true);
|
|
60
|
+
// wrapperRef.current?.removeEventListener('click', handleClick, true);
|
|
61
|
+
// };
|
|
62
|
+
// });
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
isFocused: isFocused,
|
|
66
|
+
/** Avoid using wrapperRef and handleFocus/handleBlur together */
|
|
67
|
+
wrapperRef: wrapperRef,
|
|
68
|
+
handleFocus: handleFocus,
|
|
69
|
+
handleBlur: handleBlur,
|
|
70
|
+
handleClick: handleClick
|
|
71
|
+
};
|
|
72
|
+
}
|