@pega/cosmos-react-core 3.0.0-dev.8.0 → 3.0.0-dev.9.0
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/lib/components/AppShell/AppShell.d.ts.map +1 -1
- package/lib/components/AppShell/AppShell.js +3 -3
- package/lib/components/AppShell/AppShell.js.map +1 -1
- package/lib/components/Badges/Keyboard.d.ts +9 -0
- package/lib/components/Badges/Keyboard.d.ts.map +1 -0
- package/lib/components/Badges/Keyboard.js +51 -0
- package/lib/components/Badges/Keyboard.js.map +1 -0
- package/lib/components/Badges/index.d.ts +2 -0
- package/lib/components/Badges/index.d.ts.map +1 -1
- package/lib/components/Badges/index.js +1 -0
- package/lib/components/Badges/index.js.map +1 -1
- package/lib/components/CreditCard/CreditCard.types.d.ts +43 -0
- package/lib/components/CreditCard/CreditCard.types.d.ts.map +1 -1
- package/lib/components/CreditCard/CreditCard.types.js.map +1 -1
- package/lib/components/CreditCard/CreditCardInput.d.ts +6 -0
- package/lib/components/CreditCard/CreditCardInput.d.ts.map +1 -0
- package/lib/components/CreditCard/CreditCardInput.js +39 -0
- package/lib/components/CreditCard/CreditCardInput.js.map +1 -0
- package/lib/components/CreditCard/index.d.ts +3 -1
- package/lib/components/CreditCard/index.d.ts.map +1 -1
- package/lib/components/CreditCard/index.js +2 -0
- package/lib/components/CreditCard/index.js.map +1 -1
- package/lib/components/CreditCard/utils.d.ts +11 -0
- package/lib/components/CreditCard/utils.d.ts.map +1 -1
- package/lib/components/CreditCard/utils.js +20 -2
- package/lib/components/CreditCard/utils.js.map +1 -1
- package/lib/components/Currency/CurrencyInput.d.ts.map +1 -1
- package/lib/components/Currency/CurrencyInput.js +13 -12
- package/lib/components/Currency/CurrencyInput.js.map +1 -1
- package/lib/components/DateTime/Input/DateTime.styles.js +1 -1
- package/lib/components/DateTime/Input/DateTime.styles.js.map +1 -1
- package/lib/components/HTML/HTML.d.ts.map +1 -1
- package/lib/components/HTML/HTML.js +5 -1
- package/lib/components/HTML/HTML.js.map +1 -1
- package/lib/components/Modal/DockedModals.d.ts.map +1 -1
- package/lib/components/Modal/DockedModals.js +1 -2
- package/lib/components/Modal/DockedModals.js.map +1 -1
- package/lib/components/Modal/MinimizedModal.d.ts.map +1 -1
- package/lib/components/Modal/MinimizedModal.js +1 -0
- package/lib/components/Modal/MinimizedModal.js.map +1 -1
- package/lib/components/Modal/Modal.styles.d.ts.map +1 -1
- package/lib/components/Modal/Modal.styles.js +1 -0
- package/lib/components/Modal/Modal.styles.js.map +1 -1
- package/lib/components/Number/NumberInput.d.ts.map +1 -1
- package/lib/components/Number/NumberInput.js +40 -35
- package/lib/components/Number/NumberInput.js.map +1 -1
- package/lib/components/Number/NumberInput.types.d.ts +3 -1
- package/lib/components/Number/NumberInput.types.d.ts.map +1 -1
- package/lib/components/Number/NumberInput.types.js.map +1 -1
- package/lib/components/Number/utils.d.ts +1 -1
- package/lib/components/Number/utils.d.ts.map +1 -1
- package/lib/components/Number/utils.js +18 -32
- package/lib/components/Number/utils.js.map +1 -1
- package/lib/components/Phone/PhoneInput.d.ts +1 -1
- package/lib/components/Phone/PhoneInput.d.ts.map +1 -1
- package/lib/components/Phone/PhoneInput.js +19 -36
- package/lib/components/Phone/PhoneInput.js.map +1 -1
- package/lib/components/Phone/utils.d.ts +1 -2
- package/lib/components/Phone/utils.d.ts.map +1 -1
- package/lib/components/Phone/utils.js +3 -26
- package/lib/components/Phone/utils.js.map +1 -1
- package/lib/components/Tabs/Tabs.d.ts.map +1 -1
- package/lib/components/Tabs/Tabs.js +1 -1
- package/lib/components/Tabs/Tabs.js.map +1 -1
- package/lib/hooks/index.d.ts +2 -0
- package/lib/hooks/index.d.ts.map +1 -1
- package/lib/hooks/index.js +2 -0
- package/lib/hooks/index.js.map +1 -1
- package/lib/hooks/useActiveDescendant.d.ts.map +1 -1
- package/lib/hooks/useActiveDescendant.js +6 -1
- package/lib/hooks/useActiveDescendant.js.map +1 -1
- package/lib/hooks/useInputFormatter.d.ts +12 -0
- package/lib/hooks/useInputFormatter.d.ts.map +1 -0
- package/lib/hooks/useInputFormatter.js +84 -0
- package/lib/hooks/useInputFormatter.js.map +1 -0
- package/lib/hooks/useOS.d.ts +7 -0
- package/lib/hooks/useOS.d.ts.map +1 -0
- package/lib/hooks/useOS.js +28 -0
- package/lib/hooks/useOS.js.map +1 -0
- package/lib/styles/gradients.d.ts +4 -1
- package/lib/styles/gradients.d.ts.map +1 -1
- package/lib/styles/gradients.js +46 -3
- package/lib/styles/gradients.js.map +1 -1
- package/lib/theme/theme.d.ts +18 -0
- package/lib/theme/theme.d.ts.map +1 -1
- package/lib/theme/themeDefinition.json +10 -0
- package/lib/theme/themes/darkTheme.json +6 -0
- package/package.json +2 -2
|
@@ -14,46 +14,32 @@ export function getFraction(value) {
|
|
|
14
14
|
const substrLength = decPos === -1 ? value.length : decPos;
|
|
15
15
|
return value.substring(substrLength);
|
|
16
16
|
}
|
|
17
|
-
export function getCleanedValue(value,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (value.startsWith('-.'))
|
|
27
|
-
value = `-0${value.substring(1)}`;
|
|
28
|
-
const match = value.match(new RegExp(`-?\\d*(\\.\\d{0,${maximumFractionDigits}})?`));
|
|
17
|
+
export function getCleanedValue(value, decimalSign, maximumFractionDigits) {
|
|
18
|
+
let rawValue = value.replace(new RegExp(`[^${decimalSign}\\d-]+`, 'gu'), '');
|
|
19
|
+
if (decimalSign && decimalSign !== '.')
|
|
20
|
+
rawValue = rawValue.replace(decimalSign, '.');
|
|
21
|
+
if (rawValue.startsWith('.'))
|
|
22
|
+
rawValue = `0${value}`;
|
|
23
|
+
if (rawValue.startsWith('-.'))
|
|
24
|
+
rawValue = `-0${value.substring(1)}`;
|
|
25
|
+
const match = rawValue.match(new RegExp(`-?\\d*(\\.\\d{0,${maximumFractionDigits}})?`));
|
|
29
26
|
if (match && match.length > 0) {
|
|
30
|
-
|
|
27
|
+
rawValue = match[0];
|
|
31
28
|
}
|
|
32
|
-
return
|
|
29
|
+
return rawValue;
|
|
33
30
|
}
|
|
34
31
|
export function getFormattedValue(value, locale, showGroupSeparators, maximumFractionDigits = 0) {
|
|
35
32
|
let valueNumber;
|
|
36
|
-
|
|
37
|
-
const cleanedValue = getCleanedValue(value, '.', maximumFractionDigits);
|
|
38
|
-
let integer = getInteger(cleanedValue);
|
|
33
|
+
const integer = getInteger(value);
|
|
39
34
|
const decimal = getDecimal(locale);
|
|
40
|
-
const fractionPart = getFraction(
|
|
41
|
-
if (
|
|
42
|
-
sign = '-';
|
|
43
|
-
integer = integer.substring(1);
|
|
44
|
-
}
|
|
45
|
-
if (!integer && maximumFractionDigits && fractionPart.startsWith(decimal)) {
|
|
46
|
-
integer = '0';
|
|
47
|
-
}
|
|
48
|
-
if (cleanedValue && !Number.isNaN(parseFloat(integer))) {
|
|
35
|
+
const fractionPart = getFraction(value).replace('.', decimal);
|
|
36
|
+
if (value && !Number.isNaN(parseFloat(integer))) {
|
|
49
37
|
valueNumber = BigInt(integer);
|
|
50
|
-
return (
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
.trim() +
|
|
54
|
-
(maximumFractionDigits ? fractionPart : ''));
|
|
38
|
+
return (new Intl.NumberFormat(locale, { useGrouping: showGroupSeparators })
|
|
39
|
+
.format(valueNumber)
|
|
40
|
+
.trim() + (maximumFractionDigits ? fractionPart : ''));
|
|
55
41
|
}
|
|
56
|
-
return
|
|
42
|
+
return value;
|
|
57
43
|
}
|
|
58
44
|
export function isValueInRange(value, min, max) {
|
|
59
45
|
return !((min !== undefined && value < min) || (max !== undefined && value > max));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/components/Number/utils.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,cAAc,CAAC;AAErC,MAAM,UAAU,UAAU,CAAC,K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ent":["import BigNumber from 'bignumber.js';\n\nexport function getInteger(value: string): string {\n const decPos = value.indexOf('.');\n const substrLength = decPos === -1 ? value.length : decPos;\n return value.substring(0, substrLength);\n}\n\nexport function getDecimal(locale: string, options?: Intl.NumberFormatOptions) {\n return (\n new Intl.NumberFormat(locale, options)\n .formatToParts(0.1)\n .filter((part: { type: string; value: string }) => part.type === 'decimal')[0]?.value || ''\n );\n}\n\nexport function getFraction(value: string) {\n const decPos = value.indexOf('.');\n const substrLength = decPos === -1 ? value.length : decPos;\n return value.substring(substrLength);\n}\n\nexport function getCleanedValue(\n value: string,\n decimalSign: string,\n maximumFractionDigits: number\n): string {\n let rawValue = value.replace(new RegExp(`[^${decimalSign}\\\\d-]+`, 'gu'), '');\n if (decimalSign && decimalSign !== '.') rawValue = rawValue.replace(decimalSign, '.');\n if (rawValue.startsWith('.')) rawValue = `0${value}`;\n if (rawValue.startsWith('-.')) rawValue = `-0${value.substring(1)}`;\n const match = rawValue.match(new RegExp(`-?\\\\d*(\\\\.\\\\d{0,${maximumFractionDigits}})?`));\n if (match && match.length > 0) {\n rawValue = match[0];\n }\n return rawValue;\n}\n\nexport function getFormattedValue(\n value: string,\n locale: string,\n showGroupSeparators: boolean,\n maximumFractionDigits: number = 0\n): string {\n let valueNumber;\n const integer = getInteger(value);\n const decimal = getDecimal(locale);\n const fractionPart = getFraction(value).replace('.', decimal);\n if (value && !Number.isNaN(parseFloat(integer))) {\n valueNumber = BigInt(integer);\n return (\n new Intl.NumberFormat(locale, { useGrouping: showGroupSeparators })\n .format(valueNumber)\n .trim() + (maximumFractionDigits ? fractionPart : '')\n );\n }\n return value;\n}\n\nexport function isValueInRange(\n value: number,\n min: number | undefined,\n max: number | undefined\n): boolean {\n return !((min !== undefined && value < min) || (max !== undefined && value > max));\n}\n\nexport function getIncrementedValue(value: string, min: number, max: number, step: number): string {\n const numberValue = parseFloat(value) || 0;\n const newValue = new BigNumber(numberValue).plus(step);\n if (isValueInRange(newValue.toNumber(), min, max)) {\n return newValue.toFixed();\n }\n if (min !== undefined && newValue.toNumber() < min) {\n return min.toString();\n }\n return value;\n}\n\nexport function getDecrementedValue(value: string, min: number, max: number, step: number): string {\n const numberValue = parseFloat(value) || 0;\n const newValue = new BigNumber(numberValue).minus(step);\n if (isValueInRange(newValue.toNumber(), min, max)) {\n return newValue.toFixed();\n }\n if (max !== undefined && newValue.toNumber() > max) {\n return max.toString();\n }\n return value;\n}\n\ninterface NumberFormatOptions {\n locale: string;\n options?: Intl.NumberFormatOptions;\n}\n\nexport function formatNumber(value: number | bigint, { locale, options }: NumberFormatOptions) {\n const formatter = new Intl.NumberFormat(locale, options);\n\n return formatter.format(value);\n}\n"]}
|
|
@@ -16,7 +16,7 @@ export interface PhoneInputProps extends FormControlProps, BaseProps, NoChildren
|
|
|
16
16
|
/** CCallback fired when the component gets focus */
|
|
17
17
|
onFocus?: (value: string) => void;
|
|
18
18
|
/** Ref for the wrapping element. */
|
|
19
|
-
ref?: Ref<
|
|
19
|
+
ref?: Ref<HTMLInputElement>;
|
|
20
20
|
}
|
|
21
21
|
declare const PhoneInput: FunctionComponent<PhoneInputProps & ForwardProps>;
|
|
22
22
|
export default PhoneInput;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PhoneInput.d.ts","sourceRoot":"","sources":["../../../src/components/Phone/PhoneInput.tsx"],"names":[],"mappings":"AAAA,OAAO,EAGL,iBAAiB,EAEjB,GAAG,
|
|
1
|
+
{"version":3,"file":"PhoneInput.d.ts","sourceRoot":"","sources":["../../../src/components/Phone/PhoneInput.tsx"],"names":[],"mappings":"AAAA,OAAO,EAGL,iBAAiB,EAEjB,GAAG,EAGJ,MAAM,OAAO,CAAC;AAKf,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,cAAc,EAAqB,MAAM,aAAa,CAAC;AAEzF,OAAO,EAAE,gBAAgB,EAAqB,MAAM,gBAAgB,CAAC;AAUrE,MAAM,WAAW,eAAgB,SAAQ,gBAAgB,EAAE,SAAS,EAAE,cAAc;IAClF;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,+CAA+C;IAC/C,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC5B,2CAA2C;IAC3C,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACnC,oDAAoD;IACpD,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACjC,oDAAoD;IACpD,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,oCAAoC;IACpC,GAAG,CAAC,EAAE,GAAG,CAAC,gBAAgB,CAAC,CAAC;CAC7B;AAwBD,QAAA,MAAM,UAAU,EAAE,iBAAiB,CAAC,eAAe,GAAG,YAAY,CA+GjE,CAAC;AAEF,eAAe,UAAU,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { forwardRef,
|
|
2
|
+
import { forwardRef, useCallback, useRef } from 'react';
|
|
3
3
|
import styled, { css } from 'styled-components';
|
|
4
|
-
import {
|
|
4
|
+
import { useConsolidatedRef, useDirection, useFocusWithin, useInputFormatter } from '../../hooks';
|
|
5
5
|
import { defaultThemeProp } from '../../theme';
|
|
6
6
|
import FormField from '../FormField';
|
|
7
7
|
import { StyledFormControl } from '../FormControl';
|
|
@@ -11,7 +11,7 @@ import Input from '../Input';
|
|
|
11
11
|
import { StyledSelect } from '../Select/Select';
|
|
12
12
|
import Flex from '../Flex';
|
|
13
13
|
import callingCodes from './callingCodes';
|
|
14
|
-
import {
|
|
14
|
+
import { convertToCallbackParameter, formatPhoneNumber, getPhoneNumberParts } from './utils';
|
|
15
15
|
const StyledPhoneInput = styled.div(() => {
|
|
16
16
|
const { start, end } = useDirection();
|
|
17
17
|
return css `
|
|
@@ -31,50 +31,33 @@ const StyledPhoneInput = styled.div(() => {
|
|
|
31
31
|
});
|
|
32
32
|
StyledPhoneInput.defaultProps = defaultThemeProp;
|
|
33
33
|
const PhoneInput = forwardRef((props, ref) => {
|
|
34
|
-
const {
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const [countryCode, setCountryCode] = useState(showCountryCode ? phoneNumberParts?.[0] || callingCodesList[0] : phoneNumberParts?.[0]);
|
|
39
|
-
const [phoneNumber, setPhoneNumber] = useState(phoneNumberParts ? formatPhoneNumber(phoneNumberParts[1], countryCode) : '');
|
|
40
|
-
useAfterInitialEffect(() => {
|
|
41
|
-
setCountryCode(phoneNumberParts?.[0]);
|
|
42
|
-
setPhoneNumber(phoneNumberParts ? formatPhoneNumber(phoneNumberParts[1], countryCode) : '');
|
|
43
|
-
}, [phoneNumberParts]);
|
|
44
|
-
const [cursor, setCursor] = useState(undefined);
|
|
45
|
-
const inputRef = useRef(null);
|
|
46
|
-
useEffect(() => {
|
|
47
|
-
if (cursor !== undefined)
|
|
48
|
-
inputRef.current?.setSelectionRange(cursor, cursor);
|
|
49
|
-
}, [cursor, inputRef.current]);
|
|
34
|
+
const { value, id, label, labelHidden, info, status, required, readOnly, disabled, showCountryCode = false, callingCodesList = callingCodes, onChange, onBlur, onFocus, ...restProps } = props;
|
|
35
|
+
const [countryCode = callingCodesList[0], subscriberNumber = ''] = value
|
|
36
|
+
? getPhoneNumberParts(value, callingCodesList)
|
|
37
|
+
: [];
|
|
50
38
|
const onFocusChange = (focused) => {
|
|
51
|
-
const callbackParam = convertToCallbackParameter(countryCode,
|
|
39
|
+
const callbackParam = convertToCallbackParameter(countryCode, subscriberNumber);
|
|
52
40
|
if (focused)
|
|
53
41
|
onFocus?.(callbackParam);
|
|
54
42
|
else
|
|
55
43
|
onBlur?.(callbackParam);
|
|
56
44
|
};
|
|
57
|
-
const containerRef = useRef(null);
|
|
58
|
-
useFocusWithin([containerRef], onFocusChange);
|
|
59
45
|
const onInputChange = (e) => {
|
|
60
|
-
|
|
61
|
-
const selectionStart = e.target.selectionStart;
|
|
62
|
-
const formatted = formatPhoneNumber(inputValue, countryCode);
|
|
63
|
-
setPhoneNumber(formatted);
|
|
64
|
-
if (countryCode) {
|
|
65
|
-
setCursor(calculateCursorPosition(formatted, phoneNumber, countryCode, selectionStart));
|
|
66
|
-
}
|
|
67
|
-
onChange?.(convertToCallbackParameter(countryCode, formatted));
|
|
46
|
+
onChange?.(convertToCallbackParameter(countryCode, e.target.value));
|
|
68
47
|
};
|
|
69
48
|
const onSelectChange = (e) => {
|
|
70
|
-
|
|
71
|
-
setCountryCode(selectValue);
|
|
72
|
-
setPhoneNumber(formatPhoneNumber(phoneNumber, selectValue));
|
|
73
|
-
onChange?.(convertToCallbackParameter(selectValue, phoneNumber));
|
|
49
|
+
onChange?.(convertToCallbackParameter(e.target.value, subscriberNumber));
|
|
74
50
|
};
|
|
51
|
+
const inputRef = useConsolidatedRef(ref);
|
|
52
|
+
const containerRef = useRef(null);
|
|
53
|
+
useFocusWithin([containerRef], onFocusChange);
|
|
54
|
+
const formatter = useCallback(number => {
|
|
55
|
+
return formatPhoneNumber(number.replace(/\D+/g, ''), countryCode);
|
|
56
|
+
}, [countryCode]);
|
|
57
|
+
const formatted = useInputFormatter(inputRef, subscriberNumber, formatter);
|
|
75
58
|
const codesOptions = (_jsx(_Fragment, { children: callingCodesList.map(code => (_jsx(Option, { value: code, children: code }, code))) }));
|
|
76
|
-
const Comp = (_jsxs(StyledPhoneInput, { as: StyledFormControl, forwardedAs: Flex, container: { alignItems: 'center', wrap: 'nowrap' }, ref: containerRef, status: status, readOnly: readOnly, disabled: disabled, children: [showCountryCode && (_jsx(Select, { value: countryCode, onChange: onSelectChange, required: required, status: status, readOnly: readOnly, disabled: disabled, children: codesOptions })), _jsx(Input, { ...restProps, inputMode: 'tel', ref: inputRef, value:
|
|
77
|
-
return label ? (_jsx(FormField, { as: 'fieldset', labelAs: 'legend', ...{ label, labelHidden, id, info,
|
|
59
|
+
const Comp = (_jsxs(StyledPhoneInput, { as: StyledFormControl, forwardedAs: Flex, container: { alignItems: 'center', wrap: 'nowrap' }, ref: containerRef, status: status, readOnly: readOnly, disabled: disabled, children: [showCountryCode && (_jsx(Select, { value: countryCode, onChange: onSelectChange, required: required, status: status, readOnly: readOnly, disabled: disabled, autocomplete: 'tel-country-code', children: codesOptions })), _jsx(Input, { ...restProps, inputMode: 'tel', ref: inputRef, value: formatted, onChange: onInputChange, required: required, status: status, readOnly: readOnly, disabled: disabled, autocomplete: 'tel-national' })] }));
|
|
60
|
+
return label ? (_jsx(FormField, { as: 'fieldset', labelAs: 'legend', ...{ label, labelHidden, id, info, status, required, disabled }, children: Comp })) : (Comp);
|
|
78
61
|
});
|
|
79
62
|
export default PhoneInput;
|
|
80
63
|
//# sourceMappingURL=PhoneInput.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PhoneInput.js","sourceRoot":"","sources":["../../../src/components/Phone/PhoneInput.tsx"],"names":[],"mappings":";AAAA,OAAO,EAEL,UAAU,EAIV,SAAS,EACT,OAAO,EACP,MAAM,EACN,QAAQ,EACT,MAAM,OAAO,CAAC;AACf,OAAO,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EAAE,qBAAqB,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAClF,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE/C,OAAO,SAAS,MAAM,cAAc,CAAC;AACrC,OAAO,EAAoB,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACrE,OAAO,WAAW,MAAM,uBAAuB,CAAC;AAChD,OAAO,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,KAAK,MAAM,UAAU,CAAC;AAC7B,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,IAAI,MAAM,SAAS,CAAC;AAE3B,OA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AAE,QAAQ,EAClB,QAAQ,EAAE,QAAQ,aAEjB,eAAe,IAAI,CAClB,KAAC,MAAM,IACL,KAAK,EAAE,WAAW,EAClB,QAAQ,EAAE,cAAc,EACxB,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,QAAQ,EAClB,QAAQ,EAAE,QAAQ,YAEjB,YAAY,GACN,CACV,EACD,KAAC,KAAK,OACA,SAAS,EACb,SAAS,EAAC,KAAK,EACf,GAAG,EAAE,QAAQ,EACb,KAAK,EAAE,WAAW,EAClB,QAAQ,EAAE,aAAa,EACvB,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,QAAQ,EAClB,QAAQ,EAAE,QAAQ,GAClB,IACe,CACpB,CAAC;IAEF,OAAO,KAAK,CAAC,CAAC,CAAC,CACb,KAAC,SAAS,IACR,EAAE,EAAC,UAAU,EACb,OAAO,EAAC,QAAQ,KACZ,EAAE,KAAK,EAAE,WAAW,EAAE,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAEpE,IAAI,GACK,CACb,CAAC,CAAC,CAAC,CACF,IAAI,CACL,CAAC;AACJ,CAAC,CACF,CAAC;AAEF,eAAe,UAAU,CAAC","sourcesContent":["import {\n ChangeEvent,\n forwardRef,\n FunctionComponent,\n PropsWithoutRef,\n Ref,\n useEffect,\n useMemo,\n useRef,\n useState\n} from 'react';\nimport styled, { css } from 'styled-components';\n\nimport { useAfterInitialEffect, useDirection, useFocusWithin } from '../../hooks';\nimport { defaultThemeProp } from '../../theme';\nimport { BaseProps, ForwardProps, NoChildrenProp, PropsWithDefaults } from '../../types';\nimport FormField from '../FormField';\nimport { FormControlProps, StyledFormControl } from '../FormControl';\nimport StyledInput from '../Input/Input.styles';\nimport Select, { Option } from '../Select';\nimport Input from '../Input';\nimport { StyledSelect } from '../Select/Select';\nimport Flex from '../Flex';\n\nimport callingCodes from './callingCodes';\nimport {\n calculateCursorPosition,\n convertToCallbackParameter,\n formatPhoneNumber,\n getPhoneNumberParts\n} from './utils';\n\nexport interface PhoneInputProps extends FormControlProps, BaseProps, NoChildrenProp {\n /**\n * Display select with countries calling codes\n * @default false\n */\n showCountryCode?: boolean;\n /** Changes lists of countries calling codes */\n callingCodesList?: string[];\n /** Callback fired on input value change */\n onChange?: (value: string) => void;\n /** Callback fired when the component loses focus */\n onBlur?: (value: string) => void;\n /** CCallback fired when the component gets focus */\n onFocus?: (value: string) => void;\n /** Ref for the wrapping element. */\n ref?: Ref<HTMLDivElement>;\n}\n\ntype PhoneInputPropsWithDefaults = PropsWithDefaults<PhoneInputProps>;\n\nconst StyledPhoneInput = styled.div<PhoneInputPropsWithDefaults>(() => {\n const { start, end } = useDirection();\n return css`\n border: 0;\n > ${StyledSelect} {\n max-width: max-content;\n border-inline-end: none;\n ${`border-top-${end}-radius`}: 0;\n ${`border-bottom-${end}-radius`}: 0;\n }\n\n > ${StyledSelect} + ${StyledInput} {\n ${`border-top-${start}-radius`}: 0;\n ${`border-bottom-${start}-radius`}: 0;\n }\n `;\n});\n\nStyledPhoneInput.defaultProps = defaultThemeProp;\n\nconst PhoneInput: FunctionComponent<PhoneInputProps & ForwardProps> = forwardRef(\n (props: PropsWithoutRef<PhoneInputProps>, ref: PhoneInputProps['ref']) => {\n const {\n defaultValue,\n value = defaultValue,\n id,\n label,\n labelHidden,\n info,\n status,\n required,\n readOnly,\n disabled,\n showCountryCode = false,\n callingCodesList = callingCodes,\n onChange,\n onBlur,\n onFocus,\n ...restProps\n } = props;\n\n const phoneNumberParts = useMemo(() => {\n return value ? getPhoneNumberParts(value, callingCodesList) : undefined;\n }, [callingCodesList, value]);\n\n const [countryCode, setCountryCode] = useState(\n showCountryCode ? phoneNumberParts?.[0] || callingCodesList[0] : phoneNumberParts?.[0]\n );\n\n const [phoneNumber, setPhoneNumber] = useState(\n phoneNumberParts ? formatPhoneNumber(phoneNumberParts[1], countryCode) : ''\n );\n\n useAfterInitialEffect(() => {\n setCountryCode(phoneNumberParts?.[0]);\n setPhoneNumber(phoneNumberParts ? formatPhoneNumber(phoneNumberParts[1], countryCode) : '');\n }, [phoneNumberParts]);\n\n const [cursor, setCursor] = useState<number | undefined>(undefined);\n const inputRef = useRef<HTMLInputElement>(null);\n\n useEffect(() => {\n if (cursor !== undefined) inputRef.current?.setSelectionRange(cursor, cursor);\n }, [cursor, inputRef.current]);\n\n const onFocusChange = (focused: boolean) => {\n const callbackParam = convertToCallbackParameter(countryCode, phoneNumber);\n if (focused) onFocus?.(callbackParam);\n else onBlur?.(callbackParam);\n };\n\n const containerRef = useRef<HTMLElement>(null);\n useFocusWithin([containerRef], onFocusChange);\n\n const onInputChange = (e: ChangeEvent<HTMLInputElement>) => {\n const inputValue = e.target.value;\n const selectionStart = e.target.selectionStart;\n const formatted = formatPhoneNumber(inputValue, countryCode);\n setPhoneNumber(formatted);\n if (countryCode) {\n setCursor(calculateCursorPosition(formatted, phoneNumber, countryCode, selectionStart));\n }\n onChange?.(convertToCallbackParameter(countryCode, formatted));\n };\n\n const onSelectChange = (e: ChangeEvent<HTMLSelectElement>) => {\n const selectValue = e.target.value;\n setCountryCode(selectValue);\n setPhoneNumber(formatPhoneNumber(phoneNumber, selectValue));\n onChange?.(convertToCallbackParameter(selectValue, phoneNumber));\n };\n\n const codesOptions = (\n <>\n {callingCodesList.map(code => (\n <Option key={code} value={code}>\n {code}\n </Option>\n ))}\n </>\n );\n\n const Comp = (\n <StyledPhoneInput\n as={StyledFormControl}\n forwardedAs={Flex as any}\n container={{ alignItems: 'center', wrap: 'nowrap' }}\n ref={containerRef}\n status={status}\n readOnly={readOnly}\n disabled={disabled}\n >\n {showCountryCode && (\n <Select\n value={countryCode}\n onChange={onSelectChange}\n required={required}\n status={status}\n readOnly={readOnly}\n disabled={disabled}\n >\n {codesOptions}\n </Select>\n )}\n <Input\n {...restProps}\n inputMode='tel'\n ref={inputRef}\n value={phoneNumber}\n onChange={onInputChange}\n required={required}\n status={status}\n readOnly={readOnly}\n disabled={disabled}\n />\n </StyledPhoneInput>\n );\n\n return label ? (\n <FormField\n as='fieldset'\n labelAs='legend'\n {...{ label, labelHidden, id, info, ref, status, required, disabled }}\n >\n {Comp}\n </FormField>\n ) : (\n Comp\n );\n }\n);\n\nexport default PhoneInput;\n"]}
|
|
1
|
+
{"version":3,"file":"PhoneInput.js","sourceRoot":"","sources":["../../../src/components/Phone/PhoneInput.tsx"],"names":[],"mappings":";AAAA,OAAO,EAEL,UAAU,EAIV,WAAW,EACX,MAAM,EACP,MAAM,OAAO,CAAC;AACf,OAAO,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAClG,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE/C,OAAO,SAAS,MAAM,cAAc,CAAC;AACrC,OAAO,EAAoB,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACrE,OAAO,WAAW,MAAM,uBAAuB,CAAC;AAChD,OAAO,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,KAAK,MAAM,UAAU,CAAC;AAC7B,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,IAAI,MAAM,SAAS,CAAC;AAE3B,OAAO,YAAY,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,0BAA0B,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAsB7F,MAAM,gBAAgB,GAAG,MAAM,CAAC,GAAG,CAA8B,GAAG,EAAE;IACpE,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,YAAY,EAAE,CAAC;IACtC,OAAO,GAAG,CAAA;;QAEJ,YAAY;;;QAGZ,cAAc,GAAG,SAAS;QAC1B,iBAAiB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AC,KAAK,EACf,GAAG,EAAE,QAAQ,EACb,KAAK,EAAE,SAAS,EAChB,QAAQ,EAAE,aAAa,EACvB,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,QAAQ,EAClB,QAAQ,EAAE,QAAQ,EAClB,YAAY,EAAC,cAAc,GAC3B,IACe,CACpB,CAAC;IAEF,OAAO,KAAK,CAAC,CAAC,CAAC,CACb,KAAC,SAAS,IACR,EAAE,EAAC,UAAU,EACb,OAAO,EAAC,QAAQ,KACZ,EAAE,KAAK,EAAE,WAAW,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAE/D,IAAI,GACK,CACb,CAAC,CAAC,CAAC,CACF,IAAI,CACL,CAAC;AACJ,CAAC,CACF,CAAC;AAEF,eAAe,UAAU,CAAC","sourcesContent":["import {\n ChangeEvent,\n forwardRef,\n FunctionComponent,\n PropsWithoutRef,\n Ref,\n useCallback,\n useRef\n} from 'react';\nimport styled, { css } from 'styled-components';\n\nimport { useConsolidatedRef, useDirection, useFocusWithin, useInputFormatter } from '../../hooks';\nimport { defaultThemeProp } from '../../theme';\nimport { BaseProps, ForwardProps, NoChildrenProp, PropsWithDefaults } from '../../types';\nimport FormField from '../FormField';\nimport { FormControlProps, StyledFormControl } from '../FormControl';\nimport StyledInput from '../Input/Input.styles';\nimport Select, { Option } from '../Select';\nimport Input from '../Input';\nimport { StyledSelect } from '../Select/Select';\nimport Flex from '../Flex';\n\nimport callingCodes from './callingCodes';\nimport { convertToCallbackParameter, formatPhoneNumber, getPhoneNumberParts } from './utils';\n\nexport interface PhoneInputProps extends FormControlProps, BaseProps, NoChildrenProp {\n /**\n * Display select with countries calling codes\n * @default false\n */\n showCountryCode?: boolean;\n /** Changes lists of countries calling codes */\n callingCodesList?: string[];\n /** Callback fired on input value change */\n onChange?: (value: string) => void;\n /** Callback fired when the component loses focus */\n onBlur?: (value: string) => void;\n /** CCallback fired when the component gets focus */\n onFocus?: (value: string) => void;\n /** Ref for the wrapping element. */\n ref?: Ref<HTMLInputElement>;\n}\n\ntype PhoneInputPropsWithDefaults = PropsWithDefaults<PhoneInputProps>;\n\nconst StyledPhoneInput = styled.div<PhoneInputPropsWithDefaults>(() => {\n const { start, end } = useDirection();\n return css`\n border: 0;\n > ${StyledSelect} {\n max-width: max-content;\n border-inline-end: none;\n ${`border-top-${end}-radius`}: 0;\n ${`border-bottom-${end}-radius`}: 0;\n }\n\n > ${StyledSelect} + ${StyledInput} {\n ${`border-top-${start}-radius`}: 0;\n ${`border-bottom-${start}-radius`}: 0;\n }\n `;\n});\n\nStyledPhoneInput.defaultProps = defaultThemeProp;\n\nconst PhoneInput: FunctionComponent<PhoneInputProps & ForwardProps> = forwardRef(\n (props: PropsWithoutRef<PhoneInputProps>, ref: PhoneInputProps['ref']) => {\n const {\n value,\n id,\n label,\n labelHidden,\n info,\n status,\n required,\n readOnly,\n disabled,\n showCountryCode = false,\n callingCodesList = callingCodes,\n onChange,\n onBlur,\n onFocus,\n ...restProps\n } = props;\n\n const [countryCode = callingCodesList[0], subscriberNumber = ''] = value\n ? getPhoneNumberParts(value, callingCodesList)\n : [];\n\n const onFocusChange = (focused: boolean) => {\n const callbackParam = convertToCallbackParameter(countryCode, subscriberNumber);\n if (focused) onFocus?.(callbackParam);\n else onBlur?.(callbackParam);\n };\n\n const onInputChange = (e: ChangeEvent<HTMLInputElement>) => {\n onChange?.(convertToCallbackParameter(countryCode, e.target.value));\n };\n\n const onSelectChange = (e: ChangeEvent<HTMLSelectElement>) => {\n onChange?.(convertToCallbackParameter(e.target.value, subscriberNumber));\n };\n\n const inputRef = useConsolidatedRef<HTMLInputElement>(ref);\n const containerRef = useRef<HTMLElement>(null);\n useFocusWithin([containerRef], onFocusChange);\n\n const formatter = useCallback(\n number => {\n return formatPhoneNumber(number.replace(/\\D+/g, ''), countryCode);\n },\n [countryCode]\n );\n\n const formatted = useInputFormatter(inputRef, subscriberNumber, formatter);\n\n const codesOptions = (\n <>\n {callingCodesList.map(code => (\n <Option key={code} value={code}>\n {code}\n </Option>\n ))}\n </>\n );\n\n const Comp = (\n <StyledPhoneInput\n as={StyledFormControl}\n forwardedAs={Flex as any}\n container={{ alignItems: 'center', wrap: 'nowrap' }}\n ref={containerRef}\n status={status}\n readOnly={readOnly}\n disabled={disabled}\n >\n {showCountryCode && (\n <Select\n value={countryCode}\n onChange={onSelectChange}\n required={required}\n status={status}\n readOnly={readOnly}\n disabled={disabled}\n autocomplete='tel-country-code'\n >\n {codesOptions}\n </Select>\n )}\n <Input\n {...restProps}\n inputMode='tel'\n ref={inputRef}\n value={formatted}\n onChange={onInputChange}\n required={required}\n status={status}\n readOnly={readOnly}\n disabled={disabled}\n autocomplete='tel-national'\n />\n </StyledPhoneInput>\n );\n\n return label ? (\n <FormField\n as='fieldset'\n labelAs='legend'\n {...{ label, labelHidden, id, info, status, required, disabled }}\n >\n {Comp}\n </FormField>\n ) : (\n Comp\n );\n }\n);\n\nexport default PhoneInput;\n"]}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
export declare const getPhoneNumberParts: (phoneNumber: string, codesList: string[]) => [string | undefined, string];
|
|
2
|
-
export declare const
|
|
2
|
+
export declare const cleanSubscriberNumber: (phoneNumber: string) => string;
|
|
3
3
|
export declare const getCountry: (countryCode: string, number: string) => import("libphonenumber-js").CountryCode | undefined;
|
|
4
4
|
export declare const formatPhoneNumber: (numberToFormat: string, countryCode?: string) => string;
|
|
5
5
|
export declare const convertToCallbackParameter: (codePart: string | undefined, numberPart: string) => string;
|
|
6
|
-
export declare const calculateCursorPosition: (newValue: string, prevValue: string, countryCode: string, prevCursorPosition: number | null) => number | undefined;
|
|
7
6
|
export declare const formatPhone: (value: string) => string | undefined;
|
|
8
7
|
export default getPhoneNumberParts;
|
|
9
8
|
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/components/Phone/utils.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,mBAAmB,gBACjB,MAAM,aACR,MAAM,EAAE,KAClB,CAAC,MAAM,GAAG,SAAS,EAAE,MAAM,CAK7B,CAAC;AAEF,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/components/Phone/utils.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,mBAAmB,gBACjB,MAAM,aACR,MAAM,EAAE,KAClB,CAAC,MAAM,GAAG,SAAS,EAAE,MAAM,CAK7B,CAAC;AAEF,eAAO,MAAM,qBAAqB,gBAAiB,MAAM,WAExD,CAAC;AAEF,eAAO,MAAM,UAAU,gBAAiB,MAAM,UAAU,MAAM,wDAI7D,CAAC;AAEF,eAAO,MAAM,iBAAiB,mBAAoB,MAAM,gBAAe,MAAM,WAO5E,CAAC;AAEF,eAAO,MAAM,0BAA0B,aAAc,MAAM,GAAG,SAAS,cAAc,MAAM,WAE1F,CAAC;AAEF,eAAO,MAAM,WAAW,UAAW,MAAM,uBAExC,CAAC;AAEF,eAAe,mBAAmB,CAAC"}
|
|
@@ -6,7 +6,7 @@ export const getPhoneNumberParts = (phoneNumber, codesList) => {
|
|
|
6
6
|
const subscriberNumber = phoneNumber.substring(countryCode?.length || 0);
|
|
7
7
|
return [countryCode, subscriberNumber];
|
|
8
8
|
};
|
|
9
|
-
export const
|
|
9
|
+
export const cleanSubscriberNumber = (phoneNumber) => {
|
|
10
10
|
return phoneNumber.replace(/\D/g, '');
|
|
11
11
|
};
|
|
12
12
|
export const getCountry = (countryCode, number) => {
|
|
@@ -15,7 +15,7 @@ export const getCountry = (countryCode, number) => {
|
|
|
15
15
|
return countryCode === '+1' ? 'US' : asYouTypeCode.getNumber()?.country;
|
|
16
16
|
};
|
|
17
17
|
export const formatPhoneNumber = (numberToFormat, countryCode = '') => {
|
|
18
|
-
let formatted =
|
|
18
|
+
let formatted = cleanSubscriberNumber(numberToFormat);
|
|
19
19
|
if (countryCode) {
|
|
20
20
|
const country = getCountry(countryCode, formatted);
|
|
21
21
|
formatted = formatIncompletePhoneNumber(formatted, country);
|
|
@@ -23,30 +23,7 @@ export const formatPhoneNumber = (numberToFormat, countryCode = '') => {
|
|
|
23
23
|
return formatted;
|
|
24
24
|
};
|
|
25
25
|
export const convertToCallbackParameter = (codePart, numberPart) => {
|
|
26
|
-
return [codePart,
|
|
27
|
-
};
|
|
28
|
-
export const calculateCursorPosition = (newValue, prevValue, countryCode, prevCursorPosition) => {
|
|
29
|
-
if (prevCursorPosition !== undefined && prevCursorPosition !== null) {
|
|
30
|
-
const country = getCountry(countryCode, cleanNumber(newValue));
|
|
31
|
-
let numbersCount = cleanNumber(prevValue.slice(0, prevCursorPosition - 1)).length + 1;
|
|
32
|
-
if (cleanNumber(prevValue).length > cleanNumber(newValue).length) {
|
|
33
|
-
numbersCount = cleanNumber(prevValue.slice(0, prevCursorPosition)).length;
|
|
34
|
-
}
|
|
35
|
-
let newCursorPosition = 0;
|
|
36
|
-
let count = 0;
|
|
37
|
-
const asYouTypeNew = new AsYouType(country);
|
|
38
|
-
asYouTypeNew.input(newValue);
|
|
39
|
-
while (newCursorPosition < asYouTypeNew.getTemplate().length) {
|
|
40
|
-
if (count === numbersCount) {
|
|
41
|
-
break;
|
|
42
|
-
}
|
|
43
|
-
if (asYouTypeNew.getTemplate()[newCursorPosition] === 'x') {
|
|
44
|
-
count += 1;
|
|
45
|
-
}
|
|
46
|
-
newCursorPosition += 1;
|
|
47
|
-
}
|
|
48
|
-
return newCursorPosition;
|
|
49
|
-
}
|
|
26
|
+
return [codePart, cleanSubscriberNumber(numberPart)].join('');
|
|
50
27
|
};
|
|
51
28
|
export const formatPhone = (value) => {
|
|
52
29
|
return parsePhoneNumber(value)?.formatInternational();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/components/Phone/utils.ts"],"names":[],"mappings":"AAAA,OAAO,gBAA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ePhoneNumber } from 'libphonenumber-js';\n\nexport const getPhoneNumberParts = (\n phoneNumber: string,\n codesList: string[]\n): [string | undefined, string] => {\n if (!phoneNumber.startsWith('+') || phoneNumber.startsWith('+0')) return [undefined, phoneNumber];\n const countryCode = codesList.find(code => phoneNumber.startsWith(code));\n const subscriberNumber = phoneNumber.substring(countryCode?.length || 0);\n return [countryCode, subscriberNumber];\n};\n\nexport const cleanSubscriberNumber = (phoneNumber: string) => {\n return phoneNumber.replace(/\\D/g, '');\n};\n\nexport const getCountry = (countryCode: string, number: string) => {\n const asYouTypeCode = new AsYouType();\n asYouTypeCode.input(countryCode + number);\n return countryCode === '+1' ? 'US' : asYouTypeCode.getNumber()?.country;\n};\n\nexport const formatPhoneNumber = (numberToFormat: string, countryCode: string = '') => {\n let formatted = cleanSubscriberNumber(numberToFormat);\n if (countryCode) {\n const country = getCountry(countryCode, formatted);\n formatted = formatIncompletePhoneNumber(formatted, country);\n }\n return formatted;\n};\n\nexport const convertToCallbackParameter = (codePart: string | undefined, numberPart: string) => {\n return [codePart, cleanSubscriberNumber(numberPart)].join('');\n};\n\nexport const formatPhone = (value: string) => {\n return parsePhoneNumber(value)?.formatInternational();\n};\n\nexport default getPhoneNumberParts;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Tabs.d.ts","sourceRoot":"","sources":["../../../src/components/Tabs/Tabs.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EAOlB,MAAM,OAAO,CAAC;AAOf,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG3C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAIzC,eAAO,MAAM,UAAU,yHAUtB,CAAC;AAIF;;;GAGG;AACH,QAAA,MAAM,IAAI,EAAE,iBAAiB,CAAC,SAAS,GAAG,YAAY,
|
|
1
|
+
{"version":3,"file":"Tabs.d.ts","sourceRoot":"","sources":["../../../src/components/Tabs/Tabs.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EAOlB,MAAM,OAAO,CAAC;AAOf,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG3C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAIzC,eAAO,MAAM,UAAU,yHAUtB,CAAC;AAIF;;;GAGG;AACH,QAAA,MAAM,IAAI,EAAE,iBAAiB,CAAC,SAAS,GAAG,YAAY,CAyGrD,CAAC;AAEF,eAAe,IAAI,CAAC"}
|
|
@@ -58,7 +58,7 @@ const Tabs = forwardRef(({ tabs = [], type = 'horizontal', currentTabId, onTabCl
|
|
|
58
58
|
}
|
|
59
59
|
return (_jsx(Flex, { ...restProps, container: {
|
|
60
60
|
direction: type !== 'horizontal' ? 'column' : undefined
|
|
61
|
-
}, item: { shrink: 0 }, as: StyledTabs, ref: ref, type: type, role: 'tablist', children: tabs.map((tab, i) => {
|
|
61
|
+
}, item: { shrink: 0 }, as: StyledTabs, ref: ref, type: type, role: 'tablist', "aria-orientation": type, children: tabs.map((tab, i) => {
|
|
62
62
|
const { href, name, count, id, disabled } = tab;
|
|
63
63
|
const selected = id === currentTabId;
|
|
64
64
|
return (_jsx(Tab, { id: id, "aria-selected": selected, "aria-controls": `${id}-panel`, content: name, count: typeof count === 'number' && (_jsx(Count, { variant: selected ? 'interactive' : badgeVariant, children: count })), href: href, inverted: inverted || false, selected: selected, onClick: (event) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Tabs.js","sourceRoot":"","sources":["../../../src/components/Tabs/Tabs.tsx"],"names":[],"mappings":";AAAA,OAAO,EAIL,UAAU,EAGV,MAAM,EACP,MAAM,OAAO,CAAC;AACf,OAAO,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,IAAI,MAAM,SAAS,CAAC;AAC3B,OAAO,IAAI,MAAM,SAAS,CAAC;A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CAAC","sourcesContent":["import {\n FunctionComponent,\n MouseEvent,\n KeyboardEvent,\n forwardRef,\n PropsWithoutRef,\n Ref,\n useRef\n} from 'react';\nimport styled, { css } from 'styled-components';\n\nimport { defaultThemeProp } from '../../theme';\nimport Flex from '../Flex';\nimport Link from '../Link';\nimport Count from '../Badges/Count';\nimport { ForwardProps } from '../../types';\n\nimport Tab from './Tab';\nimport { TabsProps } from './Tabs.types';\n\ntype TabItem = TabsProps['tabs'][number];\n\nexport const StyledTabs = styled.div<Partial<TabsProps>>(\n ({ type, theme }) => css`\n position: relative;\n overflow: ${type === 'horizontal' ? 'auto' : 'visible'};\n ${type === 'horizontal' &&\n css`\n border-bottom: 0.0625rem solid ${theme.base.palette['border-line']};\n height: max-content;\n `}\n `\n);\n\nStyledTabs.defaultProps = defaultThemeProp;\n\n/**\n * The tabs component will take a list of tab data objects and render out a list of tabs in either vertical or horizontal direction.\n * The tabs component handles which tab is active but will give you a callback function that allows you to make changes when a new tab is activated\n */\nconst Tabs: FunctionComponent<TabsProps & ForwardProps> = forwardRef(\n (\n {\n tabs = [],\n type = 'horizontal',\n currentTabId,\n onTabClick,\n inverted = false,\n ...restProps\n }: PropsWithoutRef<TabsProps>,\n ref: Ref<HTMLElement>\n ) => {\n // Used to keep a reference for every tab in order to focus them with key presses\n const tabRefs = useRef<(HTMLButtonElement | HTMLAnchorElement)[]>([]);\n\n // Handles arrow keypresses for changing the focus\n const changeTabFocus = (event: KeyboardEvent, index: number): void => {\n if (tabRefs.current?.length) {\n const { key } = event;\n if (key === 'ArrowRight' || key === 'ArrowDown') {\n const nextIndex = index + 1 < tabs.length ? index + 1 : 0;\n const nextTab = tabRefs.current[nextIndex] as HTMLButtonElement;\n\n if (!nextTab?.disabled) {\n nextTab?.focus();\n } else {\n changeTabFocus(event, nextIndex);\n }\n } else if (key === 'ArrowLeft' || key === 'ArrowUp') {\n const prevIndex = index === 0 ? tabs.length - 1 : index - 1;\n const prevTab = tabRefs.current[prevIndex] as HTMLButtonElement;\n if (!prevTab?.disabled) {\n prevTab?.focus();\n } else {\n changeTabFocus(event, prevIndex);\n }\n }\n }\n };\n\n let badgeVariant: 'inverted' | 'default';\n if (inverted) {\n badgeVariant = 'inverted';\n } else {\n badgeVariant = 'default';\n }\n\n return (\n <Flex\n {...restProps}\n container={{\n direction: type !== 'horizontal' ? 'column' : undefined\n }}\n item={{ shrink: 0 }}\n as={StyledTabs}\n ref={ref}\n type={type}\n role='tablist'\n aria-orientation={type}\n >\n {tabs.map((tab: TabItem, i) => {\n const { href, name, count, id, disabled } = tab;\n const selected = id === currentTabId;\n\n return (\n <Tab\n id={id}\n aria-selected={selected}\n aria-controls={`${id}-panel`}\n content={name}\n count={\n typeof count === 'number' && (\n <Count variant={selected ? 'interactive' : badgeVariant}>{count}</Count>\n )\n }\n href={href}\n inverted={inverted || false}\n selected={selected}\n onClick={(event: MouseEvent<HTMLButtonElement | HTMLAnchorElement>) => {\n onTabClick?.(id, event);\n }}\n role='tab'\n ref={(el: HTMLButtonElement | HTMLAnchorElement) => {\n tabRefs.current[i] = el;\n }}\n onKeyDown={(e: KeyboardEvent) => {\n if (['ArrowRight', 'ArrowDown', 'ArrowLeft', 'ArrowUp'].includes(e.key)) {\n e.preventDefault();\n }\n }}\n onKeyUp={(e: KeyboardEvent<any>) => {\n changeTabFocus(e, i);\n }}\n key={id}\n as={href ? Link : undefined}\n tabIndex={selected ? undefined : -1}\n aria-label={name}\n type={type}\n disabled={disabled}\n />\n );\n })}\n </Flex>\n );\n }\n);\n\nexport default Tabs;\n"]}
|
package/lib/hooks/index.d.ts
CHANGED
|
@@ -13,8 +13,10 @@ export { default as useEvent } from './useEvent';
|
|
|
13
13
|
export { default as useFocusTrap } from './useFocusTrap';
|
|
14
14
|
export { default as useFocusWithin } from './useFocusWithin';
|
|
15
15
|
export { default as useI18n } from './useI18n';
|
|
16
|
+
export { default as useInputFormatter } from './useInputFormatter';
|
|
16
17
|
export { default as useItemIntersection } from './useItemIntersection';
|
|
17
18
|
export { default as useLongPress } from './useLongPress';
|
|
19
|
+
export { default as useOS } from './useOS';
|
|
18
20
|
export { default as useOuterEvent } from './useOuterEvent';
|
|
19
21
|
export { default as useOverride } from './useOverride';
|
|
20
22
|
export { default as usePrevious } from './usePrevious';
|
package/lib/hooks/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,IAAI,mBAAmB,EAC9B,yBAAyB,EACzB,iBAAiB,EAClB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,OAAO,IAAI,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAC3E,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAC3D,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAC3D,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,EAAE,OAAO,IAAI,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AACrE,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,YAAY,CAAC;AACjD,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAE,OAAO,I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Y,CAAC;AACjD,OAAO,EAAE,OAAO,IAAI,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AACrE,OAAO,EAAE,OAAO,IAAI,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AACzE,OAAO,EAAE,OAAO,IAAI,0BAA0B,EAAE,MAAM,8BAA8B,CAAC;AACrF,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC"}
|
package/lib/hooks/index.js
CHANGED
|
@@ -13,8 +13,10 @@ export { default as useEvent } from './useEvent';
|
|
|
13
13
|
export { default as useFocusTrap } from './useFocusTrap';
|
|
14
14
|
export { default as useFocusWithin } from './useFocusWithin';
|
|
15
15
|
export { default as useI18n } from './useI18n';
|
|
16
|
+
export { default as useInputFormatter } from './useInputFormatter';
|
|
16
17
|
export { default as useItemIntersection } from './useItemIntersection';
|
|
17
18
|
export { default as useLongPress } from './useLongPress';
|
|
19
|
+
export { default as useOS } from './useOS';
|
|
18
20
|
export { default as useOuterEvent } from './useOuterEvent';
|
|
19
21
|
export { default as useOverride } from './useOverride';
|
|
20
22
|
export { default as usePrevious } from './usePrevious';
|
package/lib/hooks/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,IAAI,mBAAmB,EAE9B,iBAAiB,EAClB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,OAAO,IAAI,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAC3E,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAC3D,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAC3D,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,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A8B,CAAC;AACrF,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC","sourcesContent":["export {\n default as useActiveDescendant,\n UseActiveDescendantConfig,\n useLazyDescendant\n} from './useActiveDescendant';\nexport { default as useAfterInitialEffect } from './useAfterInitialEffect';\nexport { default as useArrows } from './useArrows';\nexport { default as useAutoResize } from './useAutoResize';\nexport { default as useBreakpoint } from './useBreakpoint';\nexport { default as useConfiguration } from './useConfiguration';\nexport { default as useConsolidatedRef } from './useConsolidatedRef';\nexport { default as useDirection } from './useDirection';\nexport { default as useDraggable } from './useDraggable';\nexport { default as useElement } from './useElement';\nexport { default as useEscape } from './useEscape';\nexport { default as useEvent } from './useEvent';\nexport { default as useFocusTrap } from './useFocusTrap';\nexport { default as useFocusWithin } from './useFocusWithin';\nexport { default as useI18n } from './useI18n';\nexport { default as useItemIntersection } from './useItemIntersection';\nexport { default as useLongPress } from './useLongPress';\nexport { default as useOuterEvent } from './useOuterEvent';\nexport { default as useOverride } from './useOverride';\nexport { default as usePrevious } from './usePrevious';\nexport { default as useScrollStick } from './useScrollStick';\nexport { default as useScrollToggle } from './useScrollToggle';\nexport { default as useTheme } from './useTheme';\nexport { default as useTransitionState } from './useTransitionState';\nexport { default as useTriggerableEffect } from './useTriggerableEffect';\nexport { default as useTriggerableLayoutEffect } from './useTriggerableLayoutEffect';\nexport { default as useUID } from './useUID';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,IAAI,mBAAmB,EAE9B,iBAAiB,EAClB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,OAAO,IAAI,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAC3E,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAC3D,OAAO,EA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CAAC;AAC/D,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,YAAY,CAAC;AACjD,OAAO,EAAE,OAAO,IAAI,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AACrE,OAAO,EAAE,OAAO,IAAI,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AACzE,OAAO,EAAE,OAAO,IAAI,0BAA0B,EAAE,MAAM,8BAA8B,CAAC;AACrF,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC","sourcesContent":["export {\n default as useActiveDescendant,\n UseActiveDescendantConfig,\n useLazyDescendant\n} from './useActiveDescendant';\nexport { default as useAfterInitialEffect } from './useAfterInitialEffect';\nexport { default as useArrows } from './useArrows';\nexport { default as useAutoResize } from './useAutoResize';\nexport { default as useBreakpoint } from './useBreakpoint';\nexport { default as useConfiguration } from './useConfiguration';\nexport { default as useConsolidatedRef } from './useConsolidatedRef';\nexport { default as useDirection } from './useDirection';\nexport { default as useDraggable } from './useDraggable';\nexport { default as useElement } from './useElement';\nexport { default as useEscape } from './useEscape';\nexport { default as useEvent } from './useEvent';\nexport { default as useFocusTrap } from './useFocusTrap';\nexport { default as useFocusWithin } from './useFocusWithin';\nexport { default as useI18n } from './useI18n';\nexport { default as useInputFormatter } from './useInputFormatter';\nexport { default as useItemIntersection } from './useItemIntersection';\nexport { default as useLongPress } from './useLongPress';\nexport { default as useOS } from './useOS';\nexport { default as useOuterEvent } from './useOuterEvent';\nexport { default as useOverride } from './useOverride';\nexport { default as usePrevious } from './usePrevious';\nexport { default as useScrollStick } from './useScrollStick';\nexport { default as useScrollToggle } from './useScrollToggle';\nexport { default as useTheme } from './useTheme';\nexport { default as useTransitionState } from './useTransitionState';\nexport { default as useTriggerableEffect } from './useTriggerableEffect';\nexport { default as useTriggerableLayoutEffect } from './useTriggerableLayoutEffect';\nexport { default as useUID } from './useUID';\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useActiveDescendant.d.ts","sourceRoot":"","sources":["../../src/hooks/useActiveDescendant.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,cAAc,EAAuB,MAAM,OAAO,CAAC;AAIjF,MAAM,WAAW,yBAAyB;IACxC,4DAA4D;IAC5D,OAAO,EAAE,WAAW,GAAG,IAAI,CAAC;IAC5B,wCAAwC;IACxC,KAAK,EAAE,WAAW,GAAG,IAAI,CAAC;IAC1B,6EAA6E;IAC7E,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,6CAA6C;IAC7C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;OAEG;IACH,iBAAiB,CAAC,EAAE,WAAW,GAAG,IAAI,CAAC;IACvC,qDAAqD;IACrD,oBAAoB,CAAC,EAAE,MAAM,IAAI,CAAC;IAClC;;OAEG;IACH,aAAa,CAAC,EAAE,WAAW,GAAG,IAAI,CAAC;IACnC,iDAAiD;IACjD,gBAAgB,CAAC,EAAE,MAAM,IAAI,CAAC;IAC9B;;;;;;OAMG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,+DAA+D;IAC/D,OAAO,CAAC,EAAE,CAAC,mBAAmB,EAAE,WAAW,KAAK,IAAI,CAAC;IACrD;;OAEG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,wDAAwD;IACxD,kBAAkB,CAAC,EAAE,MAAM,IAAI,CAAC;IAChC;;OAEG;IACH,yBAAyB,CAAC,EAAE,OAAO,CAAC;CACrC;AAED,aAAK,iBAAiB,GAAG,WAAW,GAAG,SAAS,CAAC;AACjD,aAAK,WAAW,GAAG,WAAW,EAAE,GAAG,IAAI,CAAC;AAExC,QAAA,MAAM,mBAAmB,6NAepB,yBAAyB;sBAGV,iBAAiB;iBACtB,WAAW;
|
|
1
|
+
{"version":3,"file":"useActiveDescendant.d.ts","sourceRoot":"","sources":["../../src/hooks/useActiveDescendant.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,cAAc,EAAuB,MAAM,OAAO,CAAC;A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({ focusEl, scope, scopeSelector, selector, focusDes
|
|
|
18
18
|
useEffect(() => {
|
|
19
19
|
if (!scope || !focusEl)
|
|
20
20
|
return;
|
|
21
|
-
|
|
21
|
+
const rootNode = focusEl.getRootNode();
|
|
22
|
+
let activeElement = rootNode.activeElement;
|
|
23
|
+
if (activeElement?.tagName === 'IFRAME') {
|
|
24
|
+
activeElement = activeElement.contentDocument?.activeElement;
|
|
25
|
+
}
|
|
26
|
+
scope.setAttribute('data-active-scope', activeElement === focusEl ? 'true' : 'false');
|
|
22
27
|
const onFocus = () => {
|
|
23
28
|
scope.setAttribute('data-active-scope', 'true');
|
|
24
29
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useActiveDescendant.js","sourceRoot":"","sources":["../../src/hooks/useActiveDescendant.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAkB,WAAW,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAEjF,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAgDrC,MAAM,mBAAmB,GAAG,CAC1B,EACE,OAAO,EACP,KAAK,EACL,aAAa,EACb,QAAQ,EACR,iBAAiB,EACjB,oBAAoB,EACpB,aAAa,EACb,gBAAgB,EAChB,mBAAmB,EACnB,OAAO,EACP,oBAAoB,EACpB,yBAAyB,GAAG,KAAK,EACjC,kBAAkB,EACQ,EAC5B,kBAAkC,EAAE,EAIpC,EAAE;IACF,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;IAC1C,MAAM,gBAAgB,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC;IACpC,MAAM,CAAC,sBAAsB,EAAE,yBAAyB,CAAC,GAAG,QAAQ,CAAgB,IAAI,CAAC,CAAC;IAC1F,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,QAAQ,CAAgB,IAAI,CAAC,CAAC;IACtE,MAAM,CAAC,WAAW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ACb,IAAI,OAAO,IAAI,WAAW,IAAI,WAAW,CAAC,MAAM,IAAI,gBAAgB,EAAE;YACpE,MAAM,cAAc,GAAG,CAAC,GAAG,WAAW,CAAC,CAAC;YACxC,MAAM,QAAQ,GAAG,cAAc,CAAC,GAAG,EAAE,CAAC;YACtC,MAAM,MAAM,GAAG,QAAQ,EAAE,EAAE,IAAI,SAAS,CAAC;YAEzC,IAAI,MAAM,KAAK,gBAAgB,CAAC,EAAE,EAAE;gBAClC,IAAI,QAAQ,EAAE;oBACZ,QAAQ,CAAC,SAAS,GAAG,QAAQ,CAAC,YAAY,GAAG,QAAQ,CAAC,YAAY,CAAC;iBACpE;gBAED,IAAI,aAAa,EAAE,EAAE,KAAK,gBAAgB,CAAC,EAAE,EAAE;oBAC7C,uEAAuE;oBACvE,UAAU,CAAC,GAAG,EAAE;wBACd,gBAAgB,CAAC,QAAQ,CAAC,CAAC;oBAC7B,CAAC,EAAE,CAAC,CAAC,CAAC;iBACP;aACF;YACD,mBAAmB;iBACd,IAAI,aAAa,EAAE,EAAE,KAAK,gBAAgB,CAAC,EAAE,EAAE;gBAClD,sEAAsE;gBACtE,2EAA2E;gBAC3E,UAAU,CAAC,GAAG,EAAE;oBACd,gBAAgB,CAAC,gBAAgB,CAAC,CAAC;gBACrC,CAAC,EAAE,CAAC,CAAC,CAAC;aACP;SACF;IACH,CAAC,EAAE,CAAC,OAAO,EAAE,WAAW,EAAE,wBAAwB,EAAE,gBAAgB,EAAE,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC;AAClG,CAAC,CAAC;AAEF,eAAe,mBAAmB,CAAC","sourcesContent":["import { useEffect, useState, DependencyList, useCallback, useRef } from 'react';\n\nimport { createUID } from '../utils';\n\nexport interface UseActiveDescendantConfig {\n /** DOM element which controls and has active descendants */\n focusEl: HTMLElement | null;\n /** Root scope for descendant options */\n scope: HTMLElement | null;\n /** Custom selector for specific descendant scope in root descendant scope */\n scopeSelector?: string;\n /** Custom selector for descendant options */\n selector?: string;\n /** One-time override for the focused descendant, triggers re-evaluation.\n * Include `clearFocusDescendantEl` for cleanup.\n */\n focusDescendantEl?: HTMLElement | null;\n /** Callback to clear focusDescendantEl after use. */\n clearFocusDescendant?: () => void;\n /** One-time override for focused element on NEXT re-evaluation of active descendants.\n * Include `clearFocusReturn` for cleanup.\n */\n focusReturnEl?: HTMLElement | null;\n /** Callback to clear focusReturnEl after use. */\n clearFocusReturn?: () => void;\n /**\n * Descendant ID used for continuous override of current active descendant,\n * used for external focus control / keyDown bindings (see RTE).\n * NOTE: setting currentDescendantId once effectively disables the keybindings of this hook.\n * If you only need to set the current element once, use focusReturnEl & clearFocusReturn.\n * @deprecated\n */\n currentDescendantId?: string;\n /** Explicit onClick handler called on keydown 'enter' event */\n onClick?: (currentDescendantEl: HTMLElement) => void;\n /** Prevent scrolling to the active descendant on the initial render.\n * Include `preventInitialScroll` for cleanup.\n */\n preventInitialScroll?: boolean;\n /** Callback to clear preventInitialScroll after use. */\n clearPreventScroll?: () => void;\n /** Boolean to prevent descendant evaluation.\n * @default false\n */\n pauseDescendantEvaluation?: boolean;\n}\n\ntype CurrentDescendant = HTMLElement | undefined;\ntype Descendants = HTMLElement[] | null;\n\nconst useActiveDescendant = (\n {\n focusEl,\n scope,\n scopeSelector,\n selector,\n focusDescendantEl,\n clearFocusDescendant,\n focusReturnEl,\n clearFocusReturn,\n currentDescendantId,\n onClick,\n preventInitialScroll,\n pauseDescendantEvaluation = false,\n clearPreventScroll\n }: UseActiveDescendantConfig,\n dependencyArray: DependencyList = []\n): {\n activeDescendant: CurrentDescendant;\n descendants: Descendants;\n} => {\n const [resetId, setResetId] = useState(0);\n const previousActiveId = useRef('');\n const [focusDescendantElIndex, setFocusDescendantElIndex] = useState<number | null>(null);\n const [currentIndex, setCurrentIndex] = useState<number | null>(null);\n const [descendants, setDescendants] = useState<Descendants>();\n\n const clearThenSetDescendants = useCallback(\n (setVal: NodeListOf<Element> | null) => {\n descendants?.forEach(node => {\n node.setAttribute('data-current', 'false');\n });\n\n setDescendants(\n setVal\n ? Array.from(setVal).filter((item): item is HTMLElement => item instanceof HTMLElement)\n : null\n );\n },\n [descendants]\n );\n\n // ## Toggle active scope data attr.\n useEffect(() => {\n if (!scope || !focusEl) return;\n\n scope.setAttribute('data-active-scope', document.activeElement === focusEl ? 'true' : 'false');\n\n const onFocus = () => {\n scope.setAttribute('data-active-scope', 'true');\n };\n const onBlur = () => {\n scope.setAttribute('data-active-scope', 'false');\n };\n\n focusEl.addEventListener('focus', onFocus);\n focusEl.addEventListener('blur', onBlur);\n\n return () => {\n focusEl.removeEventListener('focus', onFocus);\n focusEl.removeEventListener('blur', onBlur);\n };\n }, [scope, focusEl]);\n\n // ## Update descendants\n useEffect(() => {\n if (!pauseDescendantEvaluation) {\n // 0 second timeout added because descendantScope needs to be up to date before running query after dependencyArray change\n setTimeout(() => {\n let hasScope = scope;\n\n if (hasScope && hasScope instanceof HTMLElement) {\n if (scopeSelector) {\n hasScope = hasScope.querySelector(scopeSelector);\n }\n\n if (!hasScope) {\n clearThenSetDescendants(null);\n return;\n }\n\n if (selector) {\n clearThenSetDescendants(hasScope.querySelectorAll(selector));\n } else {\n clearThenSetDescendants(\n hasScope.querySelectorAll(\n 'a, button, input, textarea, select, details, [tabindex]:not([tabindex=\"-1\"])'\n )\n );\n }\n } else {\n clearThenSetDescendants(null);\n }\n }, 0);\n }\n }, [pauseDescendantEvaluation, scope, scopeSelector, selector, focusEl, ...dependencyArray]);\n\n // ## Set IDs and aria-owns\n useEffect(() => {\n const ownedIds: string[] = [];\n setCurrentIndex(0);\n\n if (descendants && descendants.length) {\n descendants.forEach(node => {\n node.id = node.id || createUID();\n ownedIds.push(node.id);\n });\n }\n\n focusEl?.setAttribute('aria-owns', ownedIds.join(' '));\n\n return () => {\n focusEl?.removeAttribute('aria-owns');\n };\n }, [focusEl, descendants]);\n\n // ## Bind focus el keyDown\n useEffect(() => {\n const onKeyDown = (e: KeyboardEvent) => {\n if (descendants && descendants.length) {\n switch (e.key) {\n case 'ArrowDown':\n e.preventDefault();\n // Focus next or first\n clearFocusReturn?.();\n clearPreventScroll?.();\n if (currentIndex !== null && currentIndex + 1 < descendants.length) {\n setCurrentIndex(currentIndex + 1);\n } else {\n setCurrentIndex(0);\n }\n break;\n case 'ArrowUp':\n e.preventDefault();\n // Focus previous or last\n clearFocusReturn?.();\n clearPreventScroll?.();\n if (currentIndex !== null && currentIndex - 1 > -1) {\n setCurrentIndex(currentIndex - 1);\n } else {\n setCurrentIndex(descendants.length - 1);\n }\n break;\n case 'Enter':\n e.preventDefault();\n // Click focused item\n if (descendants && descendants.length && currentIndex !== null) {\n if (onClick) {\n onClick(descendants[currentIndex]);\n break;\n }\n\n const nodeName = descendants[currentIndex].nodeName.toLowerCase();\n if (nodeName === 'input' || nodeName === 'button' || nodeName === 'a') {\n descendants[currentIndex].click();\n } else {\n descendants[currentIndex].querySelector<HTMLElement>('button, a, input')?.click();\n }\n }\n break;\n default:\n break;\n }\n }\n };\n\n // Do not rebind once / if `currentDescendantId` control is defined\n if (focusEl && descendants?.length && !currentDescendantId) {\n focusEl.addEventListener('keydown', onKeyDown);\n }\n\n return () => {\n focusEl?.removeEventListener('keydown', onKeyDown);\n };\n }, [focusEl, currentIndex, descendants]);\n\n // ## Handle independent control update\n useEffect(() => {\n if (descendants && currentDescendantId) {\n descendants.forEach((node, index) => {\n if (node.id === currentDescendantId) {\n setCurrentIndex(index);\n }\n });\n }\n }, [currentDescendantId, descendants]);\n\n // ## Set and scroll to current descendant\n useEffect(() => {\n const nextIndex = focusDescendantElIndex || currentIndex;\n const focusReturnId = focusReturnEl?.id;\n let focusReturnIndex;\n const focusDescendantElId = focusDescendantEl?.id;\n let focusDescendantIndex;\n let foundFocusDescendantEl = false;\n\n if (descendants && descendants.length) {\n // clear previous & catch focusReturn / focusDescendantEl index\n descendants.forEach((node, index) => {\n if (focusDescendantElIndex === null && node.id === focusDescendantElId) {\n focusDescendantIndex = index;\n foundFocusDescendantEl = true;\n setFocusDescendantElIndex(index);\n }\n if (node.id === focusReturnId) {\n focusReturnIndex = index;\n }\n node.setAttribute('data-current', 'false');\n });\n\n // early return for focusReturn / focusDescendantEl\n if (focusReturnIndex && focusReturnIndex !== currentIndex) {\n setCurrentIndex(focusReturnIndex);\n clearFocusReturn?.();\n return;\n }\n if (foundFocusDescendantEl && focusDescendantIndex !== undefined) {\n // hard reset to continue focusDescendantEl process even if it is the active descendant\n setResetId(Math.random());\n setCurrentIndex(focusDescendantIndex);\n return;\n }\n\n // set new\n if (nextIndex !== null && descendants[nextIndex]) {\n const itemEl = descendants[nextIndex!];\n itemEl.setAttribute('data-current', 'true');\n focusEl?.setAttribute('aria-activedescendant', itemEl.id);\n\n // scroll to element\n if (itemEl.id !== previousActiveId.current && !preventInitialScroll) {\n const scrollTo: (this: Element, arg: boolean) => void =\n Element.prototype.scrollIntoViewIfNeeded ?? Element.prototype.scrollIntoView;\n\n scrollTo?.call(itemEl, false);\n }\n\n // focusDescendantEl cleanup\n if (focusDescendantElIndex !== null) {\n setFocusDescendantElIndex(null);\n clearFocusDescendant?.();\n }\n\n previousActiveId.current = itemEl.id;\n }\n }\n\n return () => {\n focusEl?.removeAttribute('aria-activedescendant');\n };\n }, [descendants, currentIndex, focusDescendantEl, focusEl, resetId]);\n\n return {\n activeDescendant: currentIndex !== null && descendants ? descendants[currentIndex] : undefined,\n descendants: descendants || null\n };\n};\n\nexport const useLazyDescendant = ({\n loading,\n descendants,\n previousActiveDescendant,\n activeDescendant,\n focusReturnEl,\n setFocusReturnEl,\n scrollEl\n}: {\n loading: boolean;\n descendants: Descendants;\n previousActiveDescendant: CurrentDescendant;\n activeDescendant: CurrentDescendant;\n focusReturnEl: UseActiveDescendantConfig['focusReturnEl'];\n setFocusReturnEl: (el: HTMLElement | null | undefined) => void;\n scrollEl?: HTMLElement | null;\n}) => {\n // Bump scroll & set return element when descendant focused in loading state.\n useEffect(() => {\n if (loading && descendants && descendants.length && activeDescendant) {\n const descendantList = [...descendants];\n const lastItem = descendantList.pop();\n const lastId = lastItem?.id || undefined;\n\n if (lastId === activeDescendant.id) {\n if (scrollEl) {\n scrollEl.scrollTop = scrollEl.scrollHeight - scrollEl.offsetHeight;\n }\n\n if (focusReturnEl?.id !== activeDescendant.id) {\n // Set focus return to AD on 'nextTick', to avoid reset of AD on update\n setTimeout(() => {\n setFocusReturnEl(lastItem);\n }, 0);\n }\n }\n // If not last item\n else if (focusReturnEl?.id !== activeDescendant.id) {\n // Set focus return to AD on 'nextTick' to avoid reset of AD on update\n // Do not D.R.Y to avoid conflicting calls with immediately prior elements.\n setTimeout(() => {\n setFocusReturnEl(activeDescendant);\n }, 0);\n }\n }\n }, [loading, descendants, previousActiveDescendant, activeDescendant, scrollEl, focusReturnEl]);\n};\n\nexport default useActiveDescendant;\n"]}
|
|
1
|
+
{"version":3,"file":"useActiveDescendant.js","sourceRoot":"","sources":["../../src/hooks/useActiveDescendant.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAkB,WAAW,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAEjF,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAgDrC,MAAM,mBAAmB,GAAG,CAC1B,EACE,OAAO,EACP,KAAK,EACL,aAAa,EACb,QAAQ,EACR,iBAAiB,EACjB,oBAAoB,EACpB,aAAa,EACb,gBAAgB,EAChB,mBAAmB,EACnB,OAAO,EACP,oBAAoB,EACpB,yBAAyB,GAAG,KAAK,EACjC,kBAAkB,EACQ,EAC5B,kBAAkC,EAAE,EAIpC,EAAE;IACF,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;IAC1C,MAAM,gBAAgB,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC;IACpC,MAAM,CAAC,sBAAsB,EAAE,yBAAyB,CAAC,GAAG,QAAQ,CAAgB,IAAI,CAAC,CAAC;IAC1F,MAAM,CAAC,YAAY,EAAE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G,CAAC,CAAC;AAEF,eAAe,mBAAmB,CAAC","sourcesContent":["import { useEffect, useState, DependencyList, useCallback, useRef } from 'react';\n\nimport { createUID } from '../utils';\n\nexport interface UseActiveDescendantConfig {\n /** DOM element which controls and has active descendants */\n focusEl: HTMLElement | null;\n /** Root scope for descendant options */\n scope: HTMLElement | null;\n /** Custom selector for specific descendant scope in root descendant scope */\n scopeSelector?: string;\n /** Custom selector for descendant options */\n selector?: string;\n /** One-time override for the focused descendant, triggers re-evaluation.\n * Include `clearFocusDescendantEl` for cleanup.\n */\n focusDescendantEl?: HTMLElement | null;\n /** Callback to clear focusDescendantEl after use. */\n clearFocusDescendant?: () => void;\n /** One-time override for focused element on NEXT re-evaluation of active descendants.\n * Include `clearFocusReturn` for cleanup.\n */\n focusReturnEl?: HTMLElement | null;\n /** Callback to clear focusReturnEl after use. */\n clearFocusReturn?: () => void;\n /**\n * Descendant ID used for continuous override of current active descendant,\n * used for external focus control / keyDown bindings (see RTE).\n * NOTE: setting currentDescendantId once effectively disables the keybindings of this hook.\n * If you only need to set the current element once, use focusReturnEl & clearFocusReturn.\n * @deprecated\n */\n currentDescendantId?: string;\n /** Explicit onClick handler called on keydown 'enter' event */\n onClick?: (currentDescendantEl: HTMLElement) => void;\n /** Prevent scrolling to the active descendant on the initial render.\n * Include `preventInitialScroll` for cleanup.\n */\n preventInitialScroll?: boolean;\n /** Callback to clear preventInitialScroll after use. */\n clearPreventScroll?: () => void;\n /** Boolean to prevent descendant evaluation.\n * @default false\n */\n pauseDescendantEvaluation?: boolean;\n}\n\ntype CurrentDescendant = HTMLElement | undefined;\ntype Descendants = HTMLElement[] | null;\n\nconst useActiveDescendant = (\n {\n focusEl,\n scope,\n scopeSelector,\n selector,\n focusDescendantEl,\n clearFocusDescendant,\n focusReturnEl,\n clearFocusReturn,\n currentDescendantId,\n onClick,\n preventInitialScroll,\n pauseDescendantEvaluation = false,\n clearPreventScroll\n }: UseActiveDescendantConfig,\n dependencyArray: DependencyList = []\n): {\n activeDescendant: CurrentDescendant;\n descendants: Descendants;\n} => {\n const [resetId, setResetId] = useState(0);\n const previousActiveId = useRef('');\n const [focusDescendantElIndex, setFocusDescendantElIndex] = useState<number | null>(null);\n const [currentIndex, setCurrentIndex] = useState<number | null>(null);\n const [descendants, setDescendants] = useState<Descendants>();\n\n const clearThenSetDescendants = useCallback(\n (setVal: NodeListOf<Element> | null) => {\n descendants?.forEach(node => {\n node.setAttribute('data-current', 'false');\n });\n\n setDescendants(\n setVal\n ? Array.from(setVal).filter((item): item is HTMLElement => item instanceof HTMLElement)\n : null\n );\n },\n [descendants]\n );\n\n // ## Toggle active scope data attr.\n useEffect(() => {\n if (!scope || !focusEl) return;\n const rootNode = focusEl.getRootNode() as Document;\n let activeElement: Element | null | undefined = rootNode.activeElement;\n if (activeElement?.tagName === 'IFRAME') {\n activeElement = (activeElement as HTMLIFrameElement).contentDocument?.activeElement;\n }\n scope.setAttribute('data-active-scope', activeElement === focusEl ? 'true' : 'false');\n\n const onFocus = () => {\n scope.setAttribute('data-active-scope', 'true');\n };\n const onBlur = () => {\n scope.setAttribute('data-active-scope', 'false');\n };\n\n focusEl.addEventListener('focus', onFocus);\n focusEl.addEventListener('blur', onBlur);\n\n return () => {\n focusEl.removeEventListener('focus', onFocus);\n focusEl.removeEventListener('blur', onBlur);\n };\n }, [scope, focusEl]);\n\n // ## Update descendants\n useEffect(() => {\n if (!pauseDescendantEvaluation) {\n // 0 second timeout added because descendantScope needs to be up to date before running query after dependencyArray change\n setTimeout(() => {\n let hasScope = scope;\n\n if (hasScope && hasScope instanceof HTMLElement) {\n if (scopeSelector) {\n hasScope = hasScope.querySelector(scopeSelector);\n }\n\n if (!hasScope) {\n clearThenSetDescendants(null);\n return;\n }\n\n if (selector) {\n clearThenSetDescendants(hasScope.querySelectorAll(selector));\n } else {\n clearThenSetDescendants(\n hasScope.querySelectorAll(\n 'a, button, input, textarea, select, details, [tabindex]:not([tabindex=\"-1\"])'\n )\n );\n }\n } else {\n clearThenSetDescendants(null);\n }\n }, 0);\n }\n }, [pauseDescendantEvaluation, scope, scopeSelector, selector, focusEl, ...dependencyArray]);\n\n // ## Set IDs and aria-owns\n useEffect(() => {\n const ownedIds: string[] = [];\n setCurrentIndex(0);\n\n if (descendants && descendants.length) {\n descendants.forEach(node => {\n node.id = node.id || createUID();\n ownedIds.push(node.id);\n });\n }\n\n focusEl?.setAttribute('aria-owns', ownedIds.join(' '));\n\n return () => {\n focusEl?.removeAttribute('aria-owns');\n };\n }, [focusEl, descendants]);\n\n // ## Bind focus el keyDown\n useEffect(() => {\n const onKeyDown = (e: KeyboardEvent) => {\n if (descendants && descendants.length) {\n switch (e.key) {\n case 'ArrowDown':\n e.preventDefault();\n // Focus next or first\n clearFocusReturn?.();\n clearPreventScroll?.();\n if (currentIndex !== null && currentIndex + 1 < descendants.length) {\n setCurrentIndex(currentIndex + 1);\n } else {\n setCurrentIndex(0);\n }\n break;\n case 'ArrowUp':\n e.preventDefault();\n // Focus previous or last\n clearFocusReturn?.();\n clearPreventScroll?.();\n if (currentIndex !== null && currentIndex - 1 > -1) {\n setCurrentIndex(currentIndex - 1);\n } else {\n setCurrentIndex(descendants.length - 1);\n }\n break;\n case 'Enter':\n e.preventDefault();\n // Click focused item\n if (descendants && descendants.length && currentIndex !== null) {\n if (onClick) {\n onClick(descendants[currentIndex]);\n break;\n }\n\n const nodeName = descendants[currentIndex].nodeName.toLowerCase();\n if (nodeName === 'input' || nodeName === 'button' || nodeName === 'a') {\n descendants[currentIndex].click();\n } else {\n descendants[currentIndex].querySelector<HTMLElement>('button, a, input')?.click();\n }\n }\n break;\n default:\n break;\n }\n }\n };\n\n // Do not rebind once / if `currentDescendantId` control is defined\n if (focusEl && descendants?.length && !currentDescendantId) {\n focusEl.addEventListener('keydown', onKeyDown);\n }\n\n return () => {\n focusEl?.removeEventListener('keydown', onKeyDown);\n };\n }, [focusEl, currentIndex, descendants]);\n\n // ## Handle independent control update\n useEffect(() => {\n if (descendants && currentDescendantId) {\n descendants.forEach((node, index) => {\n if (node.id === currentDescendantId) {\n setCurrentIndex(index);\n }\n });\n }\n }, [currentDescendantId, descendants]);\n\n // ## Set and scroll to current descendant\n useEffect(() => {\n const nextIndex = focusDescendantElIndex || currentIndex;\n const focusReturnId = focusReturnEl?.id;\n let focusReturnIndex;\n const focusDescendantElId = focusDescendantEl?.id;\n let focusDescendantIndex;\n let foundFocusDescendantEl = false;\n\n if (descendants && descendants.length) {\n // clear previous & catch focusReturn / focusDescendantEl index\n descendants.forEach((node, index) => {\n if (focusDescendantElIndex === null && node.id === focusDescendantElId) {\n focusDescendantIndex = index;\n foundFocusDescendantEl = true;\n setFocusDescendantElIndex(index);\n }\n if (node.id === focusReturnId) {\n focusReturnIndex = index;\n }\n node.setAttribute('data-current', 'false');\n });\n\n // early return for focusReturn / focusDescendantEl\n if (focusReturnIndex && focusReturnIndex !== currentIndex) {\n setCurrentIndex(focusReturnIndex);\n clearFocusReturn?.();\n return;\n }\n if (foundFocusDescendantEl && focusDescendantIndex !== undefined) {\n // hard reset to continue focusDescendantEl process even if it is the active descendant\n setResetId(Math.random());\n setCurrentIndex(focusDescendantIndex);\n return;\n }\n\n // set new\n if (nextIndex !== null && descendants[nextIndex]) {\n const itemEl = descendants[nextIndex!];\n itemEl.setAttribute('data-current', 'true');\n focusEl?.setAttribute('aria-activedescendant', itemEl.id);\n\n // scroll to element\n if (itemEl.id !== previousActiveId.current && !preventInitialScroll) {\n const scrollTo: (this: Element, arg: boolean) => void =\n Element.prototype.scrollIntoViewIfNeeded ?? Element.prototype.scrollIntoView;\n\n scrollTo?.call(itemEl, false);\n }\n\n // focusDescendantEl cleanup\n if (focusDescendantElIndex !== null) {\n setFocusDescendantElIndex(null);\n clearFocusDescendant?.();\n }\n\n previousActiveId.current = itemEl.id;\n }\n }\n\n return () => {\n focusEl?.removeAttribute('aria-activedescendant');\n };\n }, [descendants, currentIndex, focusDescendantEl, focusEl, resetId]);\n\n return {\n activeDescendant: currentIndex !== null && descendants ? descendants[currentIndex] : undefined,\n descendants: descendants || null\n };\n};\n\nexport const useLazyDescendant = ({\n loading,\n descendants,\n previousActiveDescendant,\n activeDescendant,\n focusReturnEl,\n setFocusReturnEl,\n scrollEl\n}: {\n loading: boolean;\n descendants: Descendants;\n previousActiveDescendant: CurrentDescendant;\n activeDescendant: CurrentDescendant;\n focusReturnEl: UseActiveDescendantConfig['focusReturnEl'];\n setFocusReturnEl: (el: HTMLElement | null | undefined) => void;\n scrollEl?: HTMLElement | null;\n}) => {\n // Bump scroll & set return element when descendant focused in loading state.\n useEffect(() => {\n if (loading && descendants && descendants.length && activeDescendant) {\n const descendantList = [...descendants];\n const lastItem = descendantList.pop();\n const lastId = lastItem?.id || undefined;\n\n if (lastId === activeDescendant.id) {\n if (scrollEl) {\n scrollEl.scrollTop = scrollEl.scrollHeight - scrollEl.offsetHeight;\n }\n\n if (focusReturnEl?.id !== activeDescendant.id) {\n // Set focus return to AD on 'nextTick', to avoid reset of AD on update\n setTimeout(() => {\n setFocusReturnEl(lastItem);\n }, 0);\n }\n }\n // If not last item\n else if (focusReturnEl?.id !== activeDescendant.id) {\n // Set focus return to AD on 'nextTick' to avoid reset of AD on update\n // Do not D.R.Y to avoid conflicting calls with immediately prior elements.\n setTimeout(() => {\n setFocusReturnEl(activeDescendant);\n }, 0);\n }\n }\n }, [loading, descendants, previousActiveDescendant, activeDescendant, scrollEl, focusReturnEl]);\n};\n\nexport default useActiveDescendant;\n"]}
|