@molecule/app-ui-react-native 2.0.1 → 2.2.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/README.md +81 -10
- package/dist/components/Alert.d.ts.map +1 -1
- package/dist/components/Alert.js +2 -3
- package/dist/components/Button.d.ts +3 -1
- package/dist/components/Button.d.ts.map +1 -1
- package/dist/components/Button.js +35 -3
- package/dist/components/Skeleton.d.ts.map +1 -1
- package/dist/components/Skeleton.js +13 -12
- package/dist/components/Spinner.js +2 -2
- package/dist/elements/Anchor.d.ts +15 -0
- package/dist/elements/Anchor.d.ts.map +1 -0
- package/dist/elements/Anchor.js +57 -0
- package/dist/elements/Box.d.ts +19 -0
- package/dist/elements/Box.d.ts.map +1 -0
- package/dist/elements/Box.js +157 -0
- package/dist/elements/CheckField.d.ts +18 -0
- package/dist/elements/CheckField.d.ts.map +1 -0
- package/dist/elements/CheckField.js +103 -0
- package/dist/elements/Disclosure.d.ts +11 -0
- package/dist/elements/Disclosure.d.ts.map +1 -0
- package/dist/elements/Disclosure.js +67 -0
- package/dist/elements/Fieldset.d.ts +16 -0
- package/dist/elements/Fieldset.d.ts.map +1 -0
- package/dist/elements/Fieldset.js +34 -0
- package/dist/elements/FileField.d.ts +12 -0
- package/dist/elements/FileField.d.ts.map +1 -0
- package/dist/elements/FileField.js +111 -0
- package/dist/elements/FormArea.d.ts +18 -0
- package/dist/elements/FormArea.d.ts.map +1 -0
- package/dist/elements/FormArea.js +42 -0
- package/dist/elements/GlyphIcon.d.ts +19 -0
- package/dist/elements/GlyphIcon.d.ts.map +1 -0
- package/dist/elements/GlyphIcon.js +43 -0
- package/dist/elements/Image.d.ts +17 -0
- package/dist/elements/Image.d.ts.map +1 -0
- package/dist/elements/Image.js +26 -0
- package/dist/elements/Link.d.ts +20 -0
- package/dist/elements/Link.d.ts.map +1 -0
- package/dist/elements/Link.js +53 -0
- package/dist/elements/Pressable.d.ts +18 -0
- package/dist/elements/Pressable.d.ts.map +1 -0
- package/dist/elements/Pressable.js +108 -0
- package/dist/elements/PulseDots.d.ts +15 -0
- package/dist/elements/PulseDots.d.ts.map +1 -0
- package/dist/elements/PulseDots.js +78 -0
- package/dist/elements/RangeField.d.ts +11 -0
- package/dist/elements/RangeField.d.ts.map +1 -0
- package/dist/elements/RangeField.js +266 -0
- package/dist/elements/RichText.d.ts +23 -0
- package/dist/elements/RichText.d.ts.map +1 -0
- package/dist/elements/RichText.js +55 -0
- package/dist/elements/Rule.d.ts +11 -0
- package/dist/elements/Rule.d.ts.map +1 -0
- package/dist/elements/Rule.js +19 -0
- package/dist/elements/ScrollArea.d.ts +21 -0
- package/dist/elements/ScrollArea.d.ts.map +1 -0
- package/dist/elements/ScrollArea.js +101 -0
- package/dist/elements/SelectAction.d.ts +20 -0
- package/dist/elements/SelectAction.d.ts.map +1 -0
- package/dist/elements/SelectAction.js +117 -0
- package/dist/elements/SelectField.d.ts +11 -0
- package/dist/elements/SelectField.d.ts.map +1 -0
- package/dist/elements/SelectField.js +220 -0
- package/dist/elements/SelectMenu.d.ts +19 -0
- package/dist/elements/SelectMenu.d.ts.map +1 -0
- package/dist/elements/SelectMenu.js +122 -0
- package/dist/elements/Svg.d.ts +155 -0
- package/dist/elements/Svg.d.ts.map +1 -0
- package/dist/elements/Svg.js +535 -0
- package/dist/elements/TableBox.d.ts +19 -0
- package/dist/elements/TableBox.d.ts.map +1 -0
- package/dist/elements/TableBox.js +87 -0
- package/dist/elements/TableCell.d.ts +16 -0
- package/dist/elements/TableCell.d.ts.map +1 -0
- package/dist/elements/TableCell.js +69 -0
- package/dist/elements/Text.d.ts +17 -0
- package/dist/elements/Text.d.ts.map +1 -0
- package/dist/elements/Text.js +96 -0
- package/dist/elements/TextField.d.ts +25 -0
- package/dist/elements/TextField.d.ts.map +1 -0
- package/dist/elements/TextField.js +257 -0
- package/dist/elements/accessibility.d.ts +36 -0
- package/dist/elements/accessibility.d.ts.map +1 -0
- package/dist/elements/accessibility.js +244 -0
- package/dist/elements/className.d.ts +45 -0
- package/dist/elements/className.d.ts.map +1 -0
- package/dist/elements/className.js +8 -0
- package/dist/elements/decimal.d.ts +5 -0
- package/dist/elements/decimal.d.ts.map +1 -0
- package/dist/elements/decimal.js +11 -0
- package/dist/elements/documentUrl.d.ts +35 -0
- package/dist/elements/documentUrl.d.ts.map +1 -0
- package/dist/elements/documentUrl.js +89 -0
- package/dist/elements/drag-drop.d.ts +11 -0
- package/dist/elements/drag-drop.d.ts.map +1 -0
- package/dist/elements/drag-drop.js +277 -0
- package/dist/elements/drag.d.ts +53 -0
- package/dist/elements/drag.d.ts.map +1 -0
- package/dist/elements/drag.js +520 -0
- package/dist/elements/dragModifiers.d.ts +22 -0
- package/dist/elements/dragModifiers.d.ts.map +1 -0
- package/dist/elements/dragModifiers.js +62 -0
- package/dist/elements/events.d.ts +45 -0
- package/dist/elements/events.d.ts.map +1 -0
- package/dist/elements/events.js +101 -0
- package/dist/elements/fieldBounds.d.ts +5 -0
- package/dist/elements/fieldBounds.d.ts.map +1 -0
- package/dist/elements/fieldBounds.js +60 -0
- package/dist/elements/flow.d.ts +36 -0
- package/dist/elements/flow.d.ts.map +1 -0
- package/dist/elements/flow.js +47 -0
- package/dist/elements/form.d.ts +48 -0
- package/dist/elements/form.d.ts.map +1 -0
- package/dist/elements/form.js +112 -0
- package/dist/elements/grid.d.ts +96 -0
- package/dist/elements/grid.d.ts.map +1 -0
- package/dist/elements/grid.js +272 -0
- package/dist/elements/index.d.ts +36 -0
- package/dist/elements/index.d.ts.map +1 -0
- package/dist/elements/index.js +35 -0
- package/dist/elements/inherited.d.ts +181 -0
- package/dist/elements/inherited.d.ts.map +1 -0
- package/dist/elements/inherited.js +512 -0
- package/dist/elements/labels.d.ts +39 -0
- package/dist/elements/labels.d.ts.map +1 -0
- package/dist/elements/labels.js +163 -0
- package/dist/elements/measure.d.ts +27 -0
- package/dist/elements/measure.d.ts.map +1 -0
- package/dist/elements/measure.js +24 -0
- package/dist/elements/outsidePress.d.ts +31 -0
- package/dist/elements/outsidePress.d.ts.map +1 -0
- package/dist/elements/outsidePress.js +94 -0
- package/dist/elements/paste.d.ts +22 -0
- package/dist/elements/paste.d.ts.map +1 -0
- package/dist/elements/paste.js +49 -0
- package/dist/elements/press.d.ts +41 -0
- package/dist/elements/press.d.ts.map +1 -0
- package/dist/elements/press.js +131 -0
- package/dist/elements/rangeKeyboard.d.ts +41 -0
- package/dist/elements/rangeKeyboard.d.ts.map +1 -0
- package/dist/elements/rangeKeyboard.js +58 -0
- package/dist/elements/rangeValues.d.ts +20 -0
- package/dist/elements/rangeValues.d.ts.map +1 -0
- package/dist/elements/rangeValues.js +101 -0
- package/dist/elements/ref.d.ts +16 -0
- package/dist/elements/ref.d.ts.map +1 -0
- package/dist/elements/ref.js +22 -0
- package/dist/elements/reveal.d.ts +8 -0
- package/dist/elements/reveal.d.ts.map +1 -0
- package/dist/elements/reveal.js +10 -0
- package/dist/elements/scrollReveal.d.ts +19 -0
- package/dist/elements/scrollReveal.d.ts.map +1 -0
- package/dist/elements/scrollReveal.js +52 -0
- package/dist/elements/selectOptions.d.ts +25 -0
- package/dist/elements/selectOptions.d.ts.map +1 -0
- package/dist/elements/selectOptions.js +60 -0
- package/dist/elements/style.d.ts +30 -0
- package/dist/elements/style.d.ts.map +1 -0
- package/dist/elements/style.js +368 -0
- package/dist/elements/svgHover.d.ts +41 -0
- package/dist/elements/svgHover.d.ts.map +1 -0
- package/dist/elements/svgHover.js +152 -0
- package/dist/elements/table.d.ts +52 -0
- package/dist/elements/table.d.ts.map +1 -0
- package/dist/elements/table.js +104 -0
- package/dist/elements/types.d.ts +25 -0
- package/dist/elements/types.d.ts.map +1 -0
- package/dist/elements/types.js +8 -0
- package/dist/elements/viewport.d.ts +53 -0
- package/dist/elements/viewport.d.ts.map +1 -0
- package/dist/elements/viewport.js +62 -0
- package/dist/elements/wheelZoom.d.ts +12 -0
- package/dist/elements/wheelZoom.d.ts.map +1 -0
- package/dist/elements/wheelZoom.js +8 -0
- package/dist/index.d.ts +9 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -4
- package/dist/platform/focus.d.ts +31 -0
- package/dist/platform/focus.d.ts.map +1 -0
- package/dist/platform/focus.js +93 -0
- package/dist/platform/index.d.ts +31 -0
- package/dist/platform/index.d.ts.map +1 -0
- package/dist/platform/index.js +179 -0
- package/dist/platform/layout.d.ts +13 -0
- package/dist/platform/layout.d.ts.map +1 -0
- package/dist/platform/layout.js +15 -0
- package/dist/platform/position.d.ts +5 -0
- package/dist/platform/position.d.ts.map +1 -0
- package/dist/platform/position.js +177 -0
- package/dist/platform/stack.d.ts +86 -0
- package/dist/platform/stack.d.ts.map +1 -0
- package/dist/platform/stack.js +318 -0
- package/package.json +67 -8
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* ScrollArea: a container that scrolls.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { createContext, useCallback, useContext, useMemo, useRef, useState, } from 'react';
|
|
8
|
+
import { I18nManager, Platform, ScrollView, View, } from 'react-native';
|
|
9
|
+
import { useDialogAccessibility } from '../platform/focus.js';
|
|
10
|
+
import { layoutChanged, ScrollViewportScope } from '../platform/layout.js';
|
|
11
|
+
import { nativeAccessibility } from './accessibility.js';
|
|
12
|
+
import { useGridCell } from './grid.js';
|
|
13
|
+
import { dataAttributes, ElementScope, layoutChildren, splitClasses, useInheritedText, useTagClasses, } from './inherited.js';
|
|
14
|
+
import { useNativeRef } from './ref.js';
|
|
15
|
+
import { ScrollRevealScope, useScrollReveal } from './scrollReveal.js';
|
|
16
|
+
import { nativeStyle } from './style.js';
|
|
17
|
+
/** The scroll view fills its frame and is bounded by it, so the frame's size limits scroll. */
|
|
18
|
+
const FILL = { flexGrow: 1, flexShrink: 1 };
|
|
19
|
+
/** The viewport available to a horizontal scroll area's content, once measured. */
|
|
20
|
+
export const HorizontalViewport = createContext(null);
|
|
21
|
+
/**
|
|
22
|
+
* A scrolling container: a native `View` frame with the element's classes, role and ref, holding
|
|
23
|
+
* a `ScrollView` in `direction` that the frame's size bounds.
|
|
24
|
+
*
|
|
25
|
+
* @param props - See {@link ScrollAreaProps}.
|
|
26
|
+
* @returns The element.
|
|
27
|
+
*/
|
|
28
|
+
export function ScrollArea(props) {
|
|
29
|
+
const { as, direction, className, style, webStyle, children, ref, tabIndex, onScroll } = props;
|
|
30
|
+
const split = splitClasses(useTagClasses(className, as ?? 'div'), useInheritedText(), dataAttributes(props));
|
|
31
|
+
const text = split.text;
|
|
32
|
+
const both = direction === 'both';
|
|
33
|
+
const horizontal = both || direction === 'horizontal';
|
|
34
|
+
const outer = useScrollReveal(horizontal);
|
|
35
|
+
const inner = useScrollReveal(false);
|
|
36
|
+
const { reveal: revealOuter } = outer;
|
|
37
|
+
const { reveal: revealInner } = inner;
|
|
38
|
+
const [viewport, setViewport] = useState();
|
|
39
|
+
const offset = useRef({ x: 0, y: 0 });
|
|
40
|
+
const frame = useRef(null);
|
|
41
|
+
const forward = useNativeRef(ref);
|
|
42
|
+
const setFrame = useCallback((view) => {
|
|
43
|
+
frame.current = view;
|
|
44
|
+
forward?.(view);
|
|
45
|
+
}, [forward]);
|
|
46
|
+
const parent = useContext(ScrollViewportScope);
|
|
47
|
+
const scrollViewport = useMemo(() => ({ target: frame, parent }), [parent]);
|
|
48
|
+
const reveal = useCallback((element) => {
|
|
49
|
+
revealOuter(element);
|
|
50
|
+
if (both)
|
|
51
|
+
revealInner(element);
|
|
52
|
+
}, [both, revealInner, revealOuter]);
|
|
53
|
+
/** Updates positioned surfaces and reports the current scroll offset. */
|
|
54
|
+
function publish(event, xAxis) {
|
|
55
|
+
layoutChanged();
|
|
56
|
+
if (!onScroll)
|
|
57
|
+
return;
|
|
58
|
+
const position = physicalOffset(event.nativeEvent);
|
|
59
|
+
const previous = offset.current;
|
|
60
|
+
const next = {
|
|
61
|
+
x: xAxis ? position.x : both ? previous.x : 0,
|
|
62
|
+
y: xAxis ? (both ? previous.y : 0) : position.y,
|
|
63
|
+
};
|
|
64
|
+
if (previous.x === next.x && previous.y === next.y)
|
|
65
|
+
return;
|
|
66
|
+
offset.current = next;
|
|
67
|
+
onScroll(next);
|
|
68
|
+
}
|
|
69
|
+
const dialog = props['aria-modal'] === true || props['aria-modal'] === 'true';
|
|
70
|
+
const dialogAccessibility = useDialogAccessibility(props);
|
|
71
|
+
const content = (_jsx(HorizontalViewport.Provider, { value: horizontal ? { width: viewport?.width } : null, children: _jsx(ScrollRevealScope.Provider, { value: reveal, children: _jsx(ScrollViewportScope.Provider, { value: scrollViewport, children: layoutChildren(children, split) }) }) }));
|
|
72
|
+
return (_jsx(ElementScope, { split: split, text: text, children: _jsx(View, { ...nativeAccessibility(props, as), ...dialogAccessibility, ref: setFrame, collapsable: false, tabIndex: tabIndex === undefined ? undefined : tabIndex < 0 ? -1 : 0, className: split.view || undefined, style: [
|
|
73
|
+
{ flexShrink: 1 },
|
|
74
|
+
dialog && { height: '100%' },
|
|
75
|
+
nativeStyle(style, webStyle),
|
|
76
|
+
useGridCell(),
|
|
77
|
+
], children: _jsx(ScrollView, { ref: outer.ref, onScroll: (event) => {
|
|
78
|
+
outer.onScroll(event);
|
|
79
|
+
publish(event, horizontal);
|
|
80
|
+
}, scrollEventThrottle: 16, horizontal: horizontal, nestedScrollEnabled: both || undefined, showsVerticalScrollIndicator: both ? false : undefined, style: FILL,
|
|
81
|
+
// A handled tap, including a suggestion press, reaches its control on the first tap.
|
|
82
|
+
keyboardShouldPersistTaps: "handled", onLayout: horizontal ? (event) => setViewport(event.nativeEvent.layout) : undefined, children: both ? (_jsx(ScrollView, { ref: inner.ref, nestedScrollEnabled: true, showsHorizontalScrollIndicator: false,
|
|
83
|
+
// Bound the vertical viewport while allowing its content to size the horizontal axis.
|
|
84
|
+
style: { height: viewport?.height ?? '100%', minWidth: viewport?.width }, keyboardShouldPersistTaps: "handled", scrollEventThrottle: 16, onScroll: (event) => {
|
|
85
|
+
inner.onScroll(event);
|
|
86
|
+
publish(event, false);
|
|
87
|
+
}, children: content })) : (content) }) }) }));
|
|
88
|
+
}
|
|
89
|
+
/** RN dispatches physical x on Android and also flips the iOS RTL offset before dispatch. */
|
|
90
|
+
function physicalOffset({ contentInset, contentOffset, contentSize, layoutMeasurement, }) {
|
|
91
|
+
// The mirrored UIScrollView's right inset lands on the physical left.
|
|
92
|
+
const left = Platform.OS === 'ios' && I18nManager.isRTL ? contentInset.right : contentInset.left;
|
|
93
|
+
return {
|
|
94
|
+
x: clampOffset(contentOffset.x + left, contentSize.width - layoutMeasurement.width),
|
|
95
|
+
y: clampOffset(contentOffset.y + contentInset.top, contentSize.height - layoutMeasurement.height),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/** Clamps platform overscroll to the content's physical extent. */
|
|
99
|
+
function clampOffset(value, extent) {
|
|
100
|
+
return Math.min(Math.max(0, extent), Math.max(0, value));
|
|
101
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type GestureResponderEvent, type PressableProps, type ViewProps } from 'react-native';
|
|
2
|
+
import type { ElementHandle, KeyboardEventHandler, KitKeyboardEvent } from '@molecule/app-ui';
|
|
3
|
+
/** Focuses both the keyboard target and, when requested, the screen reader target. */
|
|
4
|
+
export interface SelectActionHandle extends ElementHandle {
|
|
5
|
+
focus(announce?: boolean): void;
|
|
6
|
+
}
|
|
7
|
+
interface SelectActionProps extends ViewProps, Pick<PressableProps, 'onHoverIn' | 'onHoverOut'> {
|
|
8
|
+
keyboard: boolean;
|
|
9
|
+
lockFocus?: number;
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
onActivate: (event?: GestureResponderEvent) => void;
|
|
12
|
+
onFocusChange?: (focused: boolean) => void;
|
|
13
|
+
onHardwareKeyDown?: KeyboardEventHandler;
|
|
14
|
+
onHardwareKeyUp?: KeyboardEventHandler;
|
|
15
|
+
onNavigate?: (event: KitKeyboardEvent) => void;
|
|
16
|
+
}
|
|
17
|
+
/** A press target with a single optional hardware and accessibility focus host. */
|
|
18
|
+
export declare const SelectAction: import("react").ForwardRefExoticComponent<SelectActionProps & import("react").RefAttributes<SelectActionHandle>>;
|
|
19
|
+
export {};
|
|
20
|
+
//# sourceMappingURL=SelectAction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SelectAction.d.ts","sourceRoot":"","sources":["../../src/elements/SelectAction.tsx"],"names":[],"mappings":"AASA,OAAO,EAGL,KAAK,qBAAqB,EAG1B,KAAK,cAAc,EAInB,KAAK,SAAS,EACf,MAAM,cAAc,CAAA;AAErB,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AAS7F,sFAAsF;AACtF,MAAM,WAAW,kBAAmB,SAAQ,aAAa;IACvD,KAAK,CAAC,QAAQ,CAAC,EAAE,OAAO,GAAG,IAAI,CAAA;CAChC;AAED,UAAU,iBAAkB,SAAQ,SAAS,EAAE,IAAI,CAAC,cAAc,EAAE,WAAW,GAAG,YAAY,CAAC;IAC7F,QAAQ,EAAE,OAAO,CAAA;IACjB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,UAAU,EAAE,CAAC,KAAK,CAAC,EAAE,qBAAqB,KAAK,IAAI,CAAA;IACnD,aAAa,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAA;IAC1C,iBAAiB,CAAC,EAAE,oBAAoB,CAAA;IACxC,eAAe,CAAC,EAAE,oBAAoB,CAAA;IACtC,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,IAAI,CAAA;CAC/C;AAED,mFAAmF;AACnF,eAAO,MAAM,YAAY,kHA+KvB,CAAA"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { createElement as _createElement } from "react";
|
|
3
|
+
import { forwardRef, useEffect, useImperativeHandle, useLayoutEffect, useRef, useState, } from 'react';
|
|
4
|
+
import { AccessibilityInfo, findNodeHandle, Platform, Pressable as NativePressable, StyleSheet, UIManager, View, } from 'react-native';
|
|
5
|
+
import { getNativeKeyboardView, rangeKeyboardEvent, } from './rangeKeyboard.js';
|
|
6
|
+
/** A press target with a single optional hardware and accessibility focus host. */
|
|
7
|
+
export const SelectAction = forwardRef(function SelectAction({ keyboard: useHardwareKeyboard, lockFocus, disabled = false, tabIndex, onActivate, onFocusChange, onHardwareKeyDown, onHardwareKeyUp, onNavigate, onHoverIn, onHoverOut, children, className, style, ...props }, ref) {
|
|
8
|
+
const KeyboardView = useHardwareKeyboard ? getNativeKeyboardView() : undefined;
|
|
9
|
+
const keyboard = Boolean(KeyboardView);
|
|
10
|
+
const stock = useRef(null);
|
|
11
|
+
const hardware = useRef(null);
|
|
12
|
+
const [focusRequest, requestFocus] = useState(null);
|
|
13
|
+
const [hostRevision, changeHost] = useState(0);
|
|
14
|
+
const focused = useRef(false);
|
|
15
|
+
const held = useRef(null);
|
|
16
|
+
const cleanupFrame = useRef(null);
|
|
17
|
+
const hidden = props['aria-hidden'] === true;
|
|
18
|
+
const canFocus = !disabled && (tabIndex !== -1 || focusRequest !== null);
|
|
19
|
+
const clearKey = () => {
|
|
20
|
+
held.current = null;
|
|
21
|
+
if (cleanupFrame.current !== null) {
|
|
22
|
+
cancelAnimationFrame(cleanupFrame.current);
|
|
23
|
+
cleanupFrame.current = null;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
useEffect(() => clearKey, []);
|
|
27
|
+
const reportFocus = (next) => {
|
|
28
|
+
if (focused.current === next)
|
|
29
|
+
return;
|
|
30
|
+
focused.current = next;
|
|
31
|
+
if (!next) {
|
|
32
|
+
clearKey();
|
|
33
|
+
requestFocus(null);
|
|
34
|
+
}
|
|
35
|
+
onFocusChange?.(next);
|
|
36
|
+
};
|
|
37
|
+
useImperativeHandle(ref, () => ({
|
|
38
|
+
focus(announce = false) {
|
|
39
|
+
if (!disabled)
|
|
40
|
+
requestFocus({ announce });
|
|
41
|
+
},
|
|
42
|
+
blur() {
|
|
43
|
+
requestFocus(null);
|
|
44
|
+
reportFocus(false);
|
|
45
|
+
// Neither the native keyboard view nor stock iOS View exposes an imperative blur command.
|
|
46
|
+
changeHost((revision) => revision + 1);
|
|
47
|
+
},
|
|
48
|
+
}));
|
|
49
|
+
useLayoutEffect(() => {
|
|
50
|
+
if (disabled) {
|
|
51
|
+
clearKey();
|
|
52
|
+
requestFocus(null);
|
|
53
|
+
}
|
|
54
|
+
}, [disabled]);
|
|
55
|
+
useLayoutEffect(() => {
|
|
56
|
+
const host = keyboard ? hardware.current : stock.current;
|
|
57
|
+
if (!focusRequest || disabled || !host)
|
|
58
|
+
return;
|
|
59
|
+
if (Platform.OS === 'web') {
|
|
60
|
+
host.focus();
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const tag = findNodeHandle(host);
|
|
64
|
+
if (tag === null)
|
|
65
|
+
return;
|
|
66
|
+
if (keyboard)
|
|
67
|
+
UIManager.dispatchViewManagerCommand(tag, 'rnekKeyboardFocus', []);
|
|
68
|
+
else if (Platform.OS !== 'android' && Platform.OS !== 'ios')
|
|
69
|
+
host.focus();
|
|
70
|
+
// Android's stock focus request is the View's hasTVPreferredFocus prop. On iOS the
|
|
71
|
+
// stock route can request accessibility focus, while Full Keyboard Access owns hardware focus.
|
|
72
|
+
if (!hidden && (focusRequest.announce || (!keyboard && Platform.OS === 'ios')))
|
|
73
|
+
AccessibilityInfo.setAccessibilityFocus(tag);
|
|
74
|
+
}, [disabled, focusRequest, hidden, keyboard]);
|
|
75
|
+
const press = (event) => {
|
|
76
|
+
if (!disabled && !held.current && cleanupFrame.current === null)
|
|
77
|
+
onActivate(event);
|
|
78
|
+
};
|
|
79
|
+
const keyDown = (native) => {
|
|
80
|
+
if (disabled)
|
|
81
|
+
return;
|
|
82
|
+
const event = rangeKeyboardEvent(native);
|
|
83
|
+
onHardwareKeyDown?.(event);
|
|
84
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
85
|
+
if (!held.current)
|
|
86
|
+
held.current = { key: event.key, canceled: event.defaultPrevented };
|
|
87
|
+
}
|
|
88
|
+
else if (!event.defaultPrevented) {
|
|
89
|
+
onNavigate?.(event);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
const keyUp = (native) => {
|
|
93
|
+
if (disabled)
|
|
94
|
+
return;
|
|
95
|
+
const event = rangeKeyboardEvent(native);
|
|
96
|
+
onHardwareKeyUp?.(event);
|
|
97
|
+
const activation = held.current;
|
|
98
|
+
if (!activation || activation.key !== event.key)
|
|
99
|
+
return;
|
|
100
|
+
clearKey();
|
|
101
|
+
// Some native focus engines send an extra click for the same key. An actual touch starts
|
|
102
|
+
// with press-in and can clear the guard before activating.
|
|
103
|
+
cleanupFrame.current = requestAnimationFrame(() => {
|
|
104
|
+
cleanupFrame.current = null;
|
|
105
|
+
});
|
|
106
|
+
if (!activation.canceled && !event.defaultPrevented)
|
|
107
|
+
onActivate();
|
|
108
|
+
};
|
|
109
|
+
if (!KeyboardView) {
|
|
110
|
+
return (_createElement(NativePressable, { ...props, key: hostRevision, ref: stock, className: className, style: style, accessible: !hidden && (Platform.OS !== 'android' || canFocus), importantForAccessibility: hidden ? 'no-hide-descendants' : 'yes', disabled: disabled, focusable: canFocus, tabIndex: canFocus ? 0 : -1, hasTVPreferredFocus: Platform.OS === 'android' && focusRequest !== null && !disabled, onPress: press, onFocus: () => reportFocus(true), onBlur: () => reportFocus(false), onHoverIn: onHoverIn, onHoverOut: onHoverOut }, children));
|
|
111
|
+
}
|
|
112
|
+
return (_jsxs(View, { className: className, style: style, accessible: false, children: [children, _createElement(KeyboardView, { ...props, key: hostRevision, ref: hardware, style: StyleSheet.absoluteFill, accessible: !hidden, importantForAccessibility: hidden ? 'no-hide-descendants' : 'yes', canBeFocused: canFocus, haloEffect: true, lockFocus: lockFocus, hasKeyDownPress: true, hasKeyUpPress: true, onKeyDownPress: keyDown, onKeyUpPress: keyUp, hasOnFocusChanged: true, onFocusChange: ({ nativeEvent }) => reportFocus(nativeEvent.isFocused), accessibilityActions: !disabled && !hidden ? [{ name: 'activate' }] : undefined, onAccessibilityAction: ({ nativeEvent }) => {
|
|
113
|
+
if (nativeEvent.actionName === 'activate')
|
|
114
|
+
press();
|
|
115
|
+
}, onAccessibilityTap: () => press() },
|
|
116
|
+
_jsx(NativePressable, { style: StyleSheet.absoluteFill, disabled: disabled, accessible: false, importantForAccessibility: "no-hide-descendants", focusable: false, onPressIn: clearKey, onPress: press, onHoverIn: onHoverIn, onHoverOut: onHoverOut }))] }));
|
|
117
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SelectField: a native choice trigger and form control.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { ElementHandle, SelectFieldProps } from '@molecule/app-ui';
|
|
7
|
+
/**
|
|
8
|
+
* Draws a choice trigger, opens its native modal and registers its selection as one form field.
|
|
9
|
+
*/
|
|
10
|
+
export declare const SelectField: import("react").ForwardRefExoticComponent<SelectFieldProps & import("react").RefAttributes<ElementHandle>>;
|
|
11
|
+
//# sourceMappingURL=SelectField.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SelectField.d.ts","sourceRoot":"","sources":["../../src/elements/SelectField.tsx"],"names":[],"mappings":"AAAA;;;;GAIG;AAeH,OAAO,KAAK,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AA6BvE;;GAEG;AACH,eAAO,MAAM,WAAW,4GA0QvB,CAAA"}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* SelectField: a native choice trigger and form control.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { forwardRef, useCallback, useContext, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState, } from 'react';
|
|
8
|
+
import { Platform, Text as NativeText } from 'react-native';
|
|
9
|
+
import { useTranslation } from '@molecule/app-react';
|
|
10
|
+
import { nativeAccessibility } from './accessibility.js';
|
|
11
|
+
import { fieldEvent } from './events.js';
|
|
12
|
+
import { registerInline } from './flow.js';
|
|
13
|
+
import { DisabledScope, FormScope, useFieldValue, useFormRegistration } from './form.js';
|
|
14
|
+
import { useGridCell } from './grid.js';
|
|
15
|
+
import { dataAttributes, ElementScope, replacedFlow, splitClasses, useInheritedText, useLineStart, useTagClasses, } from './inherited.js';
|
|
16
|
+
import { useLabelable, useReferencedLabelText } from './labels.js';
|
|
17
|
+
import { activatePress, pressProps } from './press.js';
|
|
18
|
+
import { hasRangeKeyboardView } from './rangeKeyboard.js';
|
|
19
|
+
import { SelectAction } from './SelectAction.js';
|
|
20
|
+
import { SelectMenu } from './SelectMenu.js';
|
|
21
|
+
import { choicesForValue, reconcileChoices, selectRows, } from './selectOptions.js';
|
|
22
|
+
import { nativeStyle } from './style.js';
|
|
23
|
+
/**
|
|
24
|
+
* Draws a choice trigger, opens its native modal and registers its selection as one form field.
|
|
25
|
+
*/
|
|
26
|
+
export const SelectField = forwardRef(function SelectField(props, ref) {
|
|
27
|
+
const { id, name, options, value, required, autoFocus, onClick, onChange, onFocus, onBlur } = props;
|
|
28
|
+
const { t, formatNumber } = useTranslation();
|
|
29
|
+
const form = useContext(FormScope);
|
|
30
|
+
const fieldsetDisabled = useContext(DisabledScope);
|
|
31
|
+
const disabled = fieldsetDisabled || props.disabled === true;
|
|
32
|
+
const multiple = props.multiple === true;
|
|
33
|
+
const { rows, choices } = useMemo(() => selectRows(options), [options]);
|
|
34
|
+
const [mountDefault] = useState(() => {
|
|
35
|
+
const initial = props.defaultValue;
|
|
36
|
+
return typeof initial === 'string' ? initial : initial?.slice();
|
|
37
|
+
});
|
|
38
|
+
const controlled = useMemo(() => (value === undefined ? undefined : choicesForValue(choices, multiple, value)), [choices, multiple, value]);
|
|
39
|
+
const reset = useMemo(() => choicesForValue(choices, multiple, mountDefault), [choices, mountDefault, multiple]);
|
|
40
|
+
const field = useFieldValue(controlled, reset);
|
|
41
|
+
const selected = reconcileChoices(choices, field.displayed, multiple);
|
|
42
|
+
const read = () => reconcileChoices(choices, field.read(), multiple);
|
|
43
|
+
useLayoutEffect(() => {
|
|
44
|
+
if (controlled !== undefined)
|
|
45
|
+
return;
|
|
46
|
+
const previous = field.displayed;
|
|
47
|
+
if (selected.length !== previous.length ||
|
|
48
|
+
selected.some((choice, index) => choice.key !== previous[index].key ||
|
|
49
|
+
choice.uniqueValue !== previous[index].uniqueValue)) {
|
|
50
|
+
field.change(selected, () => undefined);
|
|
51
|
+
}
|
|
52
|
+
}, [controlled, field, selected]);
|
|
53
|
+
const trigger = useRef(null);
|
|
54
|
+
const [keyboard] = useState(hasRangeKeyboardView);
|
|
55
|
+
const [open, setOpen] = useState(false);
|
|
56
|
+
const openNow = useRef(false);
|
|
57
|
+
const restorePending = useRef(false);
|
|
58
|
+
const visible = open && !disabled;
|
|
59
|
+
const dismiss = () => {
|
|
60
|
+
if (!openNow.current)
|
|
61
|
+
return;
|
|
62
|
+
openNow.current = false;
|
|
63
|
+
restorePending.current = !disabled;
|
|
64
|
+
setOpen(false);
|
|
65
|
+
};
|
|
66
|
+
const restore = useCallback(() => {
|
|
67
|
+
if (!restorePending.current || openNow.current || disabled)
|
|
68
|
+
return;
|
|
69
|
+
restorePending.current = false;
|
|
70
|
+
trigger.current?.focus(true);
|
|
71
|
+
}, [disabled]);
|
|
72
|
+
useLayoutEffect(() => {
|
|
73
|
+
if (disabled) {
|
|
74
|
+
openNow.current = false;
|
|
75
|
+
restorePending.current = false;
|
|
76
|
+
if (open)
|
|
77
|
+
setOpen(false);
|
|
78
|
+
}
|
|
79
|
+
else if (!open && restorePending.current && Platform.OS !== 'ios') {
|
|
80
|
+
const frame = requestAnimationFrame(restore);
|
|
81
|
+
return () => cancelAnimationFrame(frame);
|
|
82
|
+
}
|
|
83
|
+
return undefined;
|
|
84
|
+
}, [disabled, open, restore]);
|
|
85
|
+
useLayoutEffect(() => () => {
|
|
86
|
+
openNow.current = false;
|
|
87
|
+
restorePending.current = false;
|
|
88
|
+
}, []);
|
|
89
|
+
const activate = (event) => {
|
|
90
|
+
if (disabled || openNow.current)
|
|
91
|
+
return;
|
|
92
|
+
activatePress(onClick, () => {
|
|
93
|
+
openNow.current = true;
|
|
94
|
+
restorePending.current = false;
|
|
95
|
+
setOpen(true);
|
|
96
|
+
}, event);
|
|
97
|
+
};
|
|
98
|
+
const focus = () => {
|
|
99
|
+
if (disabled)
|
|
100
|
+
return;
|
|
101
|
+
if (openNow.current)
|
|
102
|
+
dismiss();
|
|
103
|
+
else
|
|
104
|
+
trigger.current?.focus(true);
|
|
105
|
+
};
|
|
106
|
+
const blur = () => trigger.current?.blur();
|
|
107
|
+
useImperativeHandle(ref, () => ({ focus, blur }));
|
|
108
|
+
useLayoutEffect(() => {
|
|
109
|
+
if (autoFocus && !disabled)
|
|
110
|
+
trigger.current?.focus(true);
|
|
111
|
+
}, [autoFocus, disabled]);
|
|
112
|
+
const label = useLabelable(id, () => activate());
|
|
113
|
+
const referenceName = useReferencedLabelText(props['aria-labelledby']);
|
|
114
|
+
const description = useReferencedLabelText(props['aria-describedby']);
|
|
115
|
+
const snapshot = (selection) => ({
|
|
116
|
+
id,
|
|
117
|
+
name: name ?? '',
|
|
118
|
+
type: multiple ? 'select-multiple' : 'select-one',
|
|
119
|
+
multiple,
|
|
120
|
+
value: selection[0]?.option.value ?? '',
|
|
121
|
+
selectedIndex: selection[0]?.index ?? -1,
|
|
122
|
+
selectedOptions: selection.map(({ option }) => ({
|
|
123
|
+
value: option.value,
|
|
124
|
+
label: option.label,
|
|
125
|
+
disabled: option.disabled ?? false,
|
|
126
|
+
})),
|
|
127
|
+
focus,
|
|
128
|
+
blur,
|
|
129
|
+
});
|
|
130
|
+
const choose = (choice) => {
|
|
131
|
+
if (disabled || !openNow.current || choice.option.disabled)
|
|
132
|
+
return;
|
|
133
|
+
const current = read();
|
|
134
|
+
const alreadySelected = current.some((row) => row.key === choice.key);
|
|
135
|
+
if (!multiple) {
|
|
136
|
+
dismiss();
|
|
137
|
+
if (alreadySelected)
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const next = multiple
|
|
141
|
+
? alreadySelected
|
|
142
|
+
? current.filter((row) => row.key !== choice.key)
|
|
143
|
+
: [...current, choice].sort((a, b) => a.index - b.index)
|
|
144
|
+
: [choice];
|
|
145
|
+
field.change(next, () => {
|
|
146
|
+
const changed = snapshot(next);
|
|
147
|
+
onChange?.(fieldEvent(changed));
|
|
148
|
+
if (props.multiple)
|
|
149
|
+
props.onValueChange?.(next.map((row) => row.option.value));
|
|
150
|
+
else
|
|
151
|
+
props.onValueChange?.(changed.value);
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
useFormRegistration(form?.fields, {
|
|
155
|
+
entries: () => !name || disabled
|
|
156
|
+
? []
|
|
157
|
+
: read().flatMap(({ option }) => (option.disabled ? [] : [[name, option.value]])),
|
|
158
|
+
valid: () => {
|
|
159
|
+
if (disabled || !required)
|
|
160
|
+
return true;
|
|
161
|
+
const selection = read();
|
|
162
|
+
const first = selection[0];
|
|
163
|
+
return (!!first && (multiple || first.index !== 0 || first.grouped || first.option.value !== ''));
|
|
164
|
+
},
|
|
165
|
+
focus,
|
|
166
|
+
reset: field.reset,
|
|
167
|
+
});
|
|
168
|
+
const title = t('ui.select.title', undefined, { defaultValue: 'Select' });
|
|
169
|
+
const display = multiple
|
|
170
|
+
? t('ui.select.selectionCount', { count: formatNumber(selected.length) }, { count: selected.length, defaultValue: '{{count}} selected' })
|
|
171
|
+
: (selected[0]?.option.label ??
|
|
172
|
+
t('ui.select.placeholder', undefined, { defaultValue: 'Select…' }));
|
|
173
|
+
const accessibleName = referenceName ?? props['aria-label'] ?? label ?? props.title ?? (name || title);
|
|
174
|
+
const accessibility = nativeAccessibility({
|
|
175
|
+
...props,
|
|
176
|
+
role: props.role ?? 'combobox',
|
|
177
|
+
'aria-label': accessibleName,
|
|
178
|
+
'aria-labelledby': referenceName ? undefined : props['aria-labelledby'],
|
|
179
|
+
'aria-disabled': disabled || props['aria-disabled'],
|
|
180
|
+
'aria-expanded': visible,
|
|
181
|
+
});
|
|
182
|
+
const hint = [
|
|
183
|
+
description,
|
|
184
|
+
props.title !== accessibleName ? props.title : undefined,
|
|
185
|
+
(required || enabledAria(props['aria-required'])) &&
|
|
186
|
+
t('ui.a11y.required', undefined, { defaultValue: 'Required field' }),
|
|
187
|
+
enabledAria(props['aria-invalid']) &&
|
|
188
|
+
t('ui.a11y.invalid', undefined, { defaultValue: 'Invalid value' }),
|
|
189
|
+
enabledAria(props['aria-readonly']) &&
|
|
190
|
+
t('ui.a11y.readonly', undefined, { defaultValue: 'Read-only field' }),
|
|
191
|
+
accessibility.accessibilityHint,
|
|
192
|
+
]
|
|
193
|
+
.filter(Boolean)
|
|
194
|
+
.join(', ');
|
|
195
|
+
const split = splitClasses(useTagClasses(props.className, props.as), useInheritedText(), dataAttributes(props));
|
|
196
|
+
const line = useLineStart(replacedFlow(props.className, props.as));
|
|
197
|
+
const grid = useGridCell();
|
|
198
|
+
const { color, fontSize, fontWeight, fontStyle, letterSpacing, lineHeight, textAlign, textTransform, textDecorationLine, ...viewStyle } = nativeStyle(props.style, props.webStyle) ?? {};
|
|
199
|
+
const { onHoverIn, onHoverOut } = pressProps(props);
|
|
200
|
+
const focusChanged = (gained) => {
|
|
201
|
+
const handler = gained ? onFocus : onBlur;
|
|
202
|
+
handler?.(Object.assign(fieldEvent(snapshot(read())), { relatedTarget: null }));
|
|
203
|
+
};
|
|
204
|
+
return (_jsxs(ElementScope, { split: split, children: [_jsx(SelectAction, { ...accessibility, ref: trigger, keyboard: keyboard, disabled: disabled, tabIndex: props.tabIndex === undefined ? undefined : props.tabIndex >= 0 ? 0 : -1, className: split.view || undefined, style: [{ minHeight: 36, justifyContent: 'center' }, viewStyle, line, grid], accessibilityHint: hint || undefined, accessibilityValue: { text: props['aria-valuetext'] ?? display }, onActivate: activate, onFocusChange: focusChanged, onHoverIn: onHoverIn, onHoverOut: onHoverOut, onHardwareKeyDown: props.onKeyDown, onHardwareKeyUp: props.onKeyUp, children: _jsx(NativeText, { accessible: false, importantForAccessibility: "no", className: split.text || undefined, numberOfLines: split.lines ?? undefined, style: {
|
|
205
|
+
color,
|
|
206
|
+
fontSize,
|
|
207
|
+
fontWeight,
|
|
208
|
+
fontStyle,
|
|
209
|
+
letterSpacing,
|
|
210
|
+
lineHeight,
|
|
211
|
+
textAlign,
|
|
212
|
+
textTransform,
|
|
213
|
+
textDecorationLine,
|
|
214
|
+
}, children: display }) }), _jsx(SelectMenu, { visible: visible, keyboard: keyboard, name: accessibleName, multiple: multiple, rows: rows, choices: choices, selected: selected, choose: choose, dismiss: dismiss, onDismiss: restore, onKeyDown: props.onKeyDown, onKeyUp: props.onKeyUp })] }));
|
|
215
|
+
});
|
|
216
|
+
/** Reads a true ARIA flag in either of its public spellings. */
|
|
217
|
+
function enabledAria(value) {
|
|
218
|
+
return value === true || value === 'true';
|
|
219
|
+
}
|
|
220
|
+
registerInline(SelectField, (props) => replacedFlow(props.className, 'select'));
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type ReactElement } from 'react';
|
|
2
|
+
import { type InteractionProps } from '@molecule/app-ui';
|
|
3
|
+
import type { SelectChoice, SelectRow } from './selectOptions.js';
|
|
4
|
+
interface SelectMenuProps extends Pick<InteractionProps, 'onKeyDown' | 'onKeyUp'> {
|
|
5
|
+
visible: boolean;
|
|
6
|
+
keyboard: boolean;
|
|
7
|
+
name: string;
|
|
8
|
+
multiple: boolean;
|
|
9
|
+
rows: readonly SelectRow[];
|
|
10
|
+
choices: readonly SelectChoice[];
|
|
11
|
+
selected: readonly SelectChoice[];
|
|
12
|
+
choose: (choice: SelectChoice) => void;
|
|
13
|
+
dismiss: () => void;
|
|
14
|
+
onDismiss: () => void;
|
|
15
|
+
}
|
|
16
|
+
/** The native modal owns focus while open, including focus requests for rows outside the window. */
|
|
17
|
+
export declare function SelectMenu({ visible, keyboard, name, multiple, rows, choices, selected, choose, dismiss, onDismiss, onKeyDown, onKeyUp, }: SelectMenuProps): ReactElement;
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=SelectMenu.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SelectMenu.d.ts","sourceRoot":"","sources":["../../src/elements/SelectMenu.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAAiD,MAAM,OAAO,CAAA;AAcxF,OAAO,EAAe,KAAK,gBAAgB,EAAyB,MAAM,kBAAkB,CAAA;AAK5F,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAIjE,UAAU,eAAgB,SAAQ,IAAI,CAAC,gBAAgB,EAAE,WAAW,GAAG,SAAS,CAAC;IAC/E,OAAO,EAAE,OAAO,CAAA;IAChB,QAAQ,EAAE,OAAO,CAAA;IACjB,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,OAAO,CAAA;IACjB,IAAI,EAAE,SAAS,SAAS,EAAE,CAAA;IAC1B,OAAO,EAAE,SAAS,YAAY,EAAE,CAAA;IAChC,QAAQ,EAAE,SAAS,YAAY,EAAE,CAAA;IACjC,MAAM,EAAE,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI,CAAA;IACtC,OAAO,EAAE,MAAM,IAAI,CAAA;IACnB,SAAS,EAAE,MAAM,IAAI,CAAA;CACtB;AAED,oGAAoG;AACpG,wBAAgB,UAAU,CAAC,EACzB,OAAO,EACP,QAAQ,EACR,IAAI,EACJ,QAAQ,EACR,IAAI,EACJ,OAAO,EACP,QAAQ,EACR,MAAM,EACN,OAAO,EACP,SAAS,EACT,SAAS,EACT,OAAO,GACR,EAAE,eAAe,GAAG,YAAY,CAoNhC"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useLayoutEffect, useMemo, useRef } from 'react';
|
|
3
|
+
import { AccessibilityInfo, FlatList, Modal, Pressable as NativePressable, StyleSheet, Text as NativeText, useWindowDimensions, View, } from 'react-native';
|
|
4
|
+
import { useTranslation } from '@molecule/app-react';
|
|
5
|
+
import { getClassMap } from '@molecule/app-ui';
|
|
6
|
+
import { splitClasses } from './inherited.js';
|
|
7
|
+
import { RANGE_DIRECTIONAL_KEYS } from './rangeKeyboard.js';
|
|
8
|
+
import { SelectAction } from './SelectAction.js';
|
|
9
|
+
const LOCK_MODAL_KEYS = RANGE_DIRECTIONAL_KEYS | 0b110000;
|
|
10
|
+
/** The native modal owns focus while open, including focus requests for rows outside the window. */
|
|
11
|
+
export function SelectMenu({ visible, keyboard, name, multiple, rows, choices, selected, choose, dismiss, onDismiss, onKeyDown, onKeyUp, }) {
|
|
12
|
+
const { t } = useTranslation();
|
|
13
|
+
const cm = getClassMap();
|
|
14
|
+
const { height, fontScale } = useWindowDimensions();
|
|
15
|
+
const rowHeight = Math.ceil(Math.max(48, 40 * fontScale + 8));
|
|
16
|
+
const list = useRef(null);
|
|
17
|
+
const actions = useRef(new Map());
|
|
18
|
+
const close = useRef(null);
|
|
19
|
+
const pending = useRef(null);
|
|
20
|
+
const scheduled = useRef(null);
|
|
21
|
+
const mounted = useRef(true);
|
|
22
|
+
const enabled = useMemo(() => choices.filter((choice) => !choice.option.disabled), [choices]);
|
|
23
|
+
const initial = selected.find((choice) => !choice.option.disabled) ?? enabled[0];
|
|
24
|
+
const selection = new Set(selected.map((choice) => choice.key));
|
|
25
|
+
const cancelFocus = () => {
|
|
26
|
+
if (scheduled.current !== null)
|
|
27
|
+
cancelAnimationFrame(scheduled.current);
|
|
28
|
+
scheduled.current = null;
|
|
29
|
+
pending.current = null;
|
|
30
|
+
};
|
|
31
|
+
useLayoutEffect(() => {
|
|
32
|
+
mounted.current = true;
|
|
33
|
+
return () => {
|
|
34
|
+
mounted.current = false;
|
|
35
|
+
cancelFocus();
|
|
36
|
+
};
|
|
37
|
+
}, []);
|
|
38
|
+
useLayoutEffect(() => {
|
|
39
|
+
if (!visible)
|
|
40
|
+
cancelFocus();
|
|
41
|
+
}, [visible]);
|
|
42
|
+
const scheduleFocus = useCallback(() => {
|
|
43
|
+
if (scheduled.current !== null || !pending.current)
|
|
44
|
+
return;
|
|
45
|
+
scheduled.current = requestAnimationFrame(() => {
|
|
46
|
+
scheduled.current = null;
|
|
47
|
+
const next = pending.current;
|
|
48
|
+
if (!mounted.current || !next)
|
|
49
|
+
return;
|
|
50
|
+
const action = next.key === undefined ? close.current : actions.current.get(next.key);
|
|
51
|
+
if (!action)
|
|
52
|
+
return;
|
|
53
|
+
pending.current = null;
|
|
54
|
+
action.focus(next.announce);
|
|
55
|
+
});
|
|
56
|
+
}, []);
|
|
57
|
+
const focus = useCallback((choice, announce) => {
|
|
58
|
+
pending.current = { key: choice?.key, announce };
|
|
59
|
+
if (choice)
|
|
60
|
+
list.current?.scrollToIndex({ index: choice.listIndex, animated: false });
|
|
61
|
+
scheduleFocus();
|
|
62
|
+
}, [scheduleFocus]);
|
|
63
|
+
useLayoutEffect(() => {
|
|
64
|
+
const key = pending.current?.key;
|
|
65
|
+
if (visible && key !== undefined && !enabled.some((choice) => choice.key === key))
|
|
66
|
+
focus(initial, true);
|
|
67
|
+
}, [visible, enabled, initial, focus]);
|
|
68
|
+
const navigate = (event, current) => {
|
|
69
|
+
if (event.key === 'Escape') {
|
|
70
|
+
dismiss();
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const index = enabled.findIndex((choice) => choice.key === current?.key);
|
|
74
|
+
if (event.key === 'Tab') {
|
|
75
|
+
const size = enabled.length + 1;
|
|
76
|
+
const slot = index === -1 ? enabled.length : index;
|
|
77
|
+
focus(enabled[(slot + (event.shiftKey ? -1 : 1) + size) % size], false);
|
|
78
|
+
}
|
|
79
|
+
else if (enabled.length > 0) {
|
|
80
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowRight')
|
|
81
|
+
focus(enabled[(index + 1) % enabled.length], false);
|
|
82
|
+
if (event.key === 'ArrowUp' || event.key === 'ArrowLeft')
|
|
83
|
+
focus(enabled[index <= 0 ? enabled.length - 1 : index - 1], false);
|
|
84
|
+
if (event.key === 'Home')
|
|
85
|
+
focus(enabled[0], false);
|
|
86
|
+
if (event.key === 'End')
|
|
87
|
+
focus(enabled[enabled.length - 1], false);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
const closeLabel = multiple
|
|
91
|
+
? t('common.done', undefined, { defaultValue: 'Done' })
|
|
92
|
+
: t('common.close', undefined, { defaultValue: 'Close' });
|
|
93
|
+
const listHeight = Math.min(rows.length * rowHeight, Math.max(rowHeight, height / 2 - rowHeight * 2));
|
|
94
|
+
const closeClasses = splitClasses(cm.dropdownItem, '');
|
|
95
|
+
return (_jsx(Modal, { visible: visible, transparent: true, onRequestClose: dismiss, onDismiss: onDismiss, onShow: () => {
|
|
96
|
+
if (!visible || !mounted.current)
|
|
97
|
+
return;
|
|
98
|
+
AccessibilityInfo.announceForAccessibility(t('ui.select.expanded', undefined, { defaultValue: 'Options menu opened' }));
|
|
99
|
+
focus(initial, true);
|
|
100
|
+
}, children: visible && (_jsxs(View, { style: StyleSheet.absoluteFill, children: [_jsx(NativePressable, { className: cm.dialogOverlay, style: StyleSheet.absoluteFill, onPress: dismiss, accessible: false, focusable: false, importantForAccessibility: "no-hide-descendants" }), _jsxs(View, { className: cm.actionSheet, style: SHEET_STYLE, role: "dialog", accessibilityViewIsModal: true, onAccessibilityEscape: dismiss, children: [_jsx(View, { className: cm.actionSheetHeader, children: _jsx(NativeText, { className: cm.dialogTitle, role: "heading", children: name }) }), _jsx(FlatList, { ref: list, data: rows, style: { height: listHeight, flexGrow: 0 }, initialScrollIndex: initial?.listIndex, getItemLayout: (_data, index) => ({
|
|
101
|
+
index,
|
|
102
|
+
length: rowHeight,
|
|
103
|
+
offset: rowHeight * index,
|
|
104
|
+
}), keyExtractor: (item) => item.key, renderItem: ({ item }) => {
|
|
105
|
+
if ('heading' in item) {
|
|
106
|
+
return (_jsx(View, { style: { height: rowHeight, justifyContent: 'center' }, children: _jsx(NativeText, { className: cm.textMuted, role: "heading", numberOfLines: 2, children: item.heading }) }));
|
|
107
|
+
}
|
|
108
|
+
const marked = selection.has(item.key);
|
|
109
|
+
const { label, disabled } = item.option;
|
|
110
|
+
const classes = splitClasses(cm.cn(cm.dropdownItem, marked && cm.textPrimary, disabled && cm.dropdownItemDisabled), '');
|
|
111
|
+
return (_jsx(SelectAction, { ref: (action) => {
|
|
112
|
+
if (action) {
|
|
113
|
+
actions.current.set(item.key, action);
|
|
114
|
+
if (pending.current?.key === item.key)
|
|
115
|
+
scheduleFocus();
|
|
116
|
+
}
|
|
117
|
+
else
|
|
118
|
+
actions.current.delete(item.key);
|
|
119
|
+
}, style: { height: rowHeight, justifyContent: 'center' }, className: classes.view || undefined, role: "option", "aria-label": label, "aria-selected": marked, "aria-disabled": disabled ?? false, keyboard: keyboard, lockFocus: LOCK_MODAL_KEYS, disabled: disabled, onActivate: () => choose(item), onHardwareKeyDown: onKeyDown, onHardwareKeyUp: onKeyUp, onNavigate: (event) => navigate(event, item), children: _jsx(NativeText, { accessible: false, importantForAccessibility: "no", className: classes.text || undefined, numberOfLines: 2, children: label }) }));
|
|
120
|
+
} }), _jsx(SelectAction, { ref: close, className: closeClasses.view || undefined, style: { minHeight: rowHeight, justifyContent: 'center' }, role: "button", "aria-label": closeLabel, keyboard: keyboard, lockFocus: LOCK_MODAL_KEYS, onActivate: dismiss, onHardwareKeyDown: onKeyDown, onHardwareKeyUp: onKeyUp, onNavigate: (event) => navigate(event), children: _jsx(NativeText, { accessible: false, importantForAccessibility: "no", className: closeClasses.text || undefined, children: closeLabel }) })] })] })) }));
|
|
121
|
+
}
|
|
122
|
+
const SHEET_STYLE = { position: 'absolute', bottom: 0, left: 0, right: 0 };
|