@bareboard/react-native 0.0.0-stage → 0.1.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/LICENSE +22 -0
- package/README.md +24 -2
- package/dist/components/button/button.d.ts +12 -0
- package/dist/components/button/button.js +10 -0
- package/dist/components/checkbox/checkbox.d.ts +5 -0
- package/dist/components/checkbox/checkbox.js +7 -0
- package/dist/components/close-button/close-button.d.ts +5 -0
- package/dist/components/close-button/close-button.js +14 -0
- package/dist/components/combobox/combobox-machine-glue.d.ts +6 -0
- package/dist/components/combobox/combobox-machine-glue.js +16 -0
- package/dist/components/combobox/combobox-machine.d.ts +143 -0
- package/dist/components/combobox/combobox-machine.js +377 -0
- package/dist/components/combobox/combobox.d.ts +66 -0
- package/dist/components/combobox/combobox.js +199 -0
- package/dist/components/data-interactive/data-interactive.d.ts +4 -0
- package/dist/components/data-interactive/data-interactive.js +8 -0
- package/dist/components/description/description.d.ts +4 -0
- package/dist/components/description/description.js +6 -0
- package/dist/components/dialog/dialog.d.ts +22 -0
- package/dist/components/dialog/dialog.js +27 -0
- package/dist/components/disclosure/disclosure.d.ts +35 -0
- package/dist/components/disclosure/disclosure.js +35 -0
- package/dist/components/field/field.d.ts +6 -0
- package/dist/components/field/field.js +9 -0
- package/dist/components/fieldset/fieldset.d.ts +3 -0
- package/dist/components/fieldset/fieldset.js +3 -0
- package/dist/components/focus-trap/focus-trap.d.ts +5 -0
- package/dist/components/focus-trap/focus-trap.js +5 -0
- package/dist/components/input/input.d.ts +6 -0
- package/dist/components/input/input.js +9 -0
- package/dist/components/label/label.d.ts +6 -0
- package/dist/components/label/label.js +9 -0
- package/dist/components/legend/legend.d.ts +6 -0
- package/dist/components/legend/legend.js +9 -0
- package/dist/components/listbox/listbox-machine-glue.d.ts +6 -0
- package/dist/components/listbox/listbox-machine-glue.js +16 -0
- package/dist/components/listbox/listbox-machine.d.ts +140 -0
- package/dist/components/listbox/listbox-machine.js +347 -0
- package/dist/components/listbox/listbox.d.ts +53 -0
- package/dist/components/listbox/listbox.js +91 -0
- package/dist/components/listbox-label/listbox-label.d.ts +2 -0
- package/dist/components/listbox-label/listbox-label.js +2 -0
- package/dist/components/listbox-selected-option/listbox-selected-option.d.ts +7 -0
- package/dist/components/listbox-selected-option/listbox-selected-option.js +16 -0
- package/dist/components/menu/menu-machine-glue.d.ts +6 -0
- package/dist/components/menu/menu-machine-glue.js +16 -0
- package/dist/components/menu/menu-machine.d.ts +109 -0
- package/dist/components/menu/menu-machine.js +238 -0
- package/dist/components/menu/menu.d.ts +34 -0
- package/dist/components/menu/menu.js +60 -0
- package/dist/components/menu-heading/menu-heading.d.ts +4 -0
- package/dist/components/menu-heading/menu-heading.js +6 -0
- package/dist/components/menu-section/menu-section.d.ts +4 -0
- package/dist/components/menu-section/menu-section.js +6 -0
- package/dist/components/menu-separator/menu-separator.d.ts +4 -0
- package/dist/components/menu-separator/menu-separator.js +6 -0
- package/dist/components/popover/popover-machine-glue.d.ts +6 -0
- package/dist/components/popover/popover-machine-glue.js +16 -0
- package/dist/components/popover/popover-machine.d.ts +30 -0
- package/dist/components/popover/popover-machine.js +49 -0
- package/dist/components/popover/popover.d.ts +25 -0
- package/dist/components/popover/popover.js +37 -0
- package/dist/components/popover-backdrop/popover-backdrop.d.ts +6 -0
- package/dist/components/popover-backdrop/popover-backdrop.js +14 -0
- package/dist/components/popover-group/popover-group.d.ts +4 -0
- package/dist/components/popover-group/popover-group.js +6 -0
- package/dist/components/popover-overlay/popover-overlay.d.ts +4 -0
- package/dist/components/popover-overlay/popover-overlay.js +6 -0
- package/dist/components/portal/portal.d.ts +9 -0
- package/dist/components/portal/portal.js +8 -0
- package/dist/components/radio-group/radio-group.d.ts +29 -0
- package/dist/components/radio-group/radio-group.js +36 -0
- package/dist/components/select/select.d.ts +7 -0
- package/dist/components/select/select.js +10 -0
- package/dist/components/switch/switch.d.ts +20 -0
- package/dist/components/switch/switch.js +24 -0
- package/dist/components/tab-panels/tab-panels.d.ts +4 -0
- package/dist/components/tab-panels/tab-panels.js +6 -0
- package/dist/components/tabs/tabs.d.ts +29 -0
- package/dist/components/tabs/tabs.js +40 -0
- package/dist/components/textarea/textarea.d.ts +5 -0
- package/dist/components/textarea/textarea.js +7 -0
- package/dist/components/transition/transition.d.ts +21 -0
- package/dist/components/transition/transition.js +34 -0
- package/dist/hooks/use-controllable.d.ts +1 -0
- package/dist/hooks/use-controllable.js +34 -0
- package/dist/hooks/use-event.d.ts +1 -0
- package/dist/hooks/use-event.js +8 -0
- package/dist/hooks/use-iso-morphic-effect.d.ts +2 -0
- package/dist/hooks/use-iso-morphic-effect.js +3 -0
- package/dist/hooks/use-latest-value.d.ts +1 -0
- package/dist/hooks/use-latest-value.js +9 -0
- package/dist/hooks/use-on-unmount.d.ts +1 -0
- package/dist/hooks/use-on-unmount.js +18 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +33 -0
- package/dist/internal/close-provider.d.ts +6 -0
- package/dist/internal/close-provider.js +9 -0
- package/dist/internal/disabled.d.ts +5 -0
- package/dist/internal/disabled.js +9 -0
- package/dist/internal/toggle.d.ts +17 -0
- package/dist/internal/toggle.js +17 -0
- package/dist/machine.d.ts +33 -0
- package/dist/machine.js +107 -0
- package/dist/machines/stack-machine.d.ts +32 -0
- package/dist/machines/stack-machine.js +51 -0
- package/dist/react-glue.d.ts +2 -0
- package/dist/react-glue.js +9 -0
- package/dist/utils/calculate-active-index.d.ts +25 -0
- package/dist/utils/calculate-active-index.js +74 -0
- package/dist/utils/default-map.d.ts +5 -0
- package/dist/utils/default-map.js +15 -0
- package/dist/utils/disposables.d.ts +31 -0
- package/dist/utils/disposables.js +68 -0
- package/dist/utils/find-active-index.d.ts +3 -0
- package/dist/utils/find-active-index.js +12 -0
- package/dist/utils/match.d.ts +1 -0
- package/dist/utils/match.js +12 -0
- package/dist/utils/micro-task.d.ts +1 -0
- package/dist/utils/micro-task.js +13 -0
- package/dist/utils/render.d.ts +3 -0
- package/dist/utils/render.js +4 -0
- package/dist/utils/sort-by-order.d.ts +16 -0
- package/dist/utils/sort-by-order.js +33 -0
- package/dist/utils/typeahead.d.ts +1 -0
- package/dist/utils/typeahead.js +11 -0
- package/package.json +43 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Tailwind Labs
|
|
4
|
+
Copyright (c) 2026 bareboard contributors
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @bareboard/react-native
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A set of completely unstyled, fully accessible UI components for React Native, ported from [Headless UI](https://github.com/tailwindlabs/headlessui).
|
|
4
|
+
|
|
5
|
+
> **Early stage:** `@bareboard/react-native` is experimental and its API may change without notice. Testing on iOS and Android devices is still in progress.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add @bareboard/react-native
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
The package needs `react` and `react-native` in your app. It has no styles: components render your children and accept `style` props.
|
|
14
|
+
|
|
15
|
+
## Components
|
|
16
|
+
|
|
17
|
+
`Button`, `Checkbox`, `Combobox`, `Dialog`, `Disclosure`, `Listbox`, `Menu`, `Popover`, `RadioGroup`, `Select`, `Switch`, `Tabs`, `Textarea`, `Transition` and more.
|
|
18
|
+
|
|
19
|
+
## Limits of the React Native port
|
|
20
|
+
|
|
21
|
+
Some parts of the Headless UI API, such as positioning with `anchor`, scale transitions and the `as` prop, have no equivalent here. The full list is in the [repository README](https://github.com/jbrxl/bareboard#limits-of-the-react-native-port).
|
|
22
|
+
|
|
23
|
+
## License
|
|
24
|
+
|
|
25
|
+
MIT. Copyright (c) 2020 Tailwind Labs, as in the original Headless UI project. Copyright (c) 2026 bareboard contributors.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type ForwardRefExoticComponent, type RefAttributes } from 'react';
|
|
2
|
+
import { type PressableProps, type ViewInstance } from 'react-native';
|
|
3
|
+
import { type RenderChildren } from '../../utils/render';
|
|
4
|
+
export type ButtonRenderPropArg = {
|
|
5
|
+
disabled: boolean;
|
|
6
|
+
active: boolean;
|
|
7
|
+
};
|
|
8
|
+
export type ButtonProps = Omit<PressableProps, 'children' | 'disabled'> & {
|
|
9
|
+
disabled?: boolean;
|
|
10
|
+
children?: RenderChildren<ButtonRenderPropArg>;
|
|
11
|
+
};
|
|
12
|
+
export declare const Button: ForwardRefExoticComponent<ButtonProps & RefAttributes<ViewInstance>>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { forwardRef } from 'react';
|
|
3
|
+
import { Pressable } from 'react-native';
|
|
4
|
+
import { useDisabled } from '../../internal/disabled';
|
|
5
|
+
import { resolveChildren } from '../../utils/render';
|
|
6
|
+
export const Button = forwardRef(function ButtonFn(props, ref) {
|
|
7
|
+
let providedDisabled = useDisabled();
|
|
8
|
+
let { disabled = providedDisabled || false, children, ...theirProps } = props;
|
|
9
|
+
return (_jsx(Pressable, { ...theirProps, ref: ref, disabled: disabled, accessibilityRole: "button", accessibilityState: { ...theirProps.accessibilityState, disabled }, children: ({ pressed }) => resolveChildren(children, { disabled, active: pressed }) }));
|
|
10
|
+
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type ForwardRefExoticComponent, type RefAttributes } from 'react';
|
|
2
|
+
import { type ViewInstance } from 'react-native';
|
|
3
|
+
import { type ToggleProps } from '../../internal/toggle';
|
|
4
|
+
export type CheckboxProps = Omit<ToggleProps, 'role'>;
|
|
5
|
+
export declare const Checkbox: ForwardRefExoticComponent<CheckboxProps & RefAttributes<ViewInstance>>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { forwardRef } from 'react';
|
|
3
|
+
import {} from 'react-native';
|
|
4
|
+
import { Toggle } from '../../internal/toggle';
|
|
5
|
+
export const Checkbox = forwardRef(function CheckboxFn(props, ref) {
|
|
6
|
+
return _jsx(Toggle, { ...props, ref: ref, role: "checkbox" });
|
|
7
|
+
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type ForwardRefExoticComponent, type RefAttributes } from 'react';
|
|
2
|
+
import { type ViewInstance } from 'react-native';
|
|
3
|
+
import { type ButtonProps } from '../button/button';
|
|
4
|
+
export type CloseButtonProps = ButtonProps;
|
|
5
|
+
export declare const CloseButton: ForwardRefExoticComponent<CloseButtonProps & RefAttributes<ViewInstance>>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { forwardRef } from 'react';
|
|
3
|
+
import {} from 'react-native';
|
|
4
|
+
import { useClose } from '../../internal/close-provider';
|
|
5
|
+
import { Button } from '../button/button';
|
|
6
|
+
export const CloseButton = forwardRef(function CloseButtonFn(props, ref) {
|
|
7
|
+
let close = useClose();
|
|
8
|
+
let { onPress, ...theirProps } = props;
|
|
9
|
+
let handlePress = (event) => {
|
|
10
|
+
onPress?.(event);
|
|
11
|
+
close();
|
|
12
|
+
};
|
|
13
|
+
return _jsx(Button, { ...theirProps, ref: ref, onPress: handlePress });
|
|
14
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ComboboxMachine } from './combobox-machine';
|
|
2
|
+
export declare const ComboboxContext: import("react").Context<ComboboxMachine<unknown> | null>;
|
|
3
|
+
export declare function useComboboxMachineContext<T>(component: string): ComboboxMachine<T>;
|
|
4
|
+
export declare function useComboboxMachine({ id }: {
|
|
5
|
+
id: string;
|
|
6
|
+
}): ComboboxMachine<unknown>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createContext, useContext, useMemo } from 'react';
|
|
2
|
+
import { useOnUnmount } from '../../hooks/use-on-unmount';
|
|
3
|
+
import { ComboboxMachine } from './combobox-machine';
|
|
4
|
+
export const ComboboxContext = createContext(null);
|
|
5
|
+
export function useComboboxMachineContext(component) {
|
|
6
|
+
let context = useContext(ComboboxContext);
|
|
7
|
+
if (context === null) {
|
|
8
|
+
throw new Error(`<${component} /> is missing a parent <Combobox /> component.`);
|
|
9
|
+
}
|
|
10
|
+
return context;
|
|
11
|
+
}
|
|
12
|
+
export function useComboboxMachine({ id }) {
|
|
13
|
+
let machine = useMemo(() => ComboboxMachine.new({ id }), []);
|
|
14
|
+
useOnUnmount(() => machine.dispose());
|
|
15
|
+
return machine;
|
|
16
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { Machine } from '../../machine';
|
|
2
|
+
import { Focus } from '../../utils/calculate-active-index';
|
|
3
|
+
interface MutableRefObject<T> {
|
|
4
|
+
current: T;
|
|
5
|
+
}
|
|
6
|
+
export declare enum ComboboxState {
|
|
7
|
+
Open = 0,
|
|
8
|
+
Closed = 1
|
|
9
|
+
}
|
|
10
|
+
export declare enum ValueMode {
|
|
11
|
+
Single = 0,
|
|
12
|
+
Multi = 1
|
|
13
|
+
}
|
|
14
|
+
export declare enum ActivationTrigger {
|
|
15
|
+
Pointer = 0,
|
|
16
|
+
Focus = 1,
|
|
17
|
+
Other = 2
|
|
18
|
+
}
|
|
19
|
+
export type ComboboxOptionDataRef<T> = MutableRefObject<{
|
|
20
|
+
disabled: boolean;
|
|
21
|
+
value: T;
|
|
22
|
+
order?: number;
|
|
23
|
+
}>;
|
|
24
|
+
export interface State<T> {
|
|
25
|
+
id: string;
|
|
26
|
+
dataRef: MutableRefObject<{
|
|
27
|
+
value: unknown;
|
|
28
|
+
defaultValue: unknown;
|
|
29
|
+
disabled: boolean;
|
|
30
|
+
mode: ValueMode;
|
|
31
|
+
onChange: (value: T) => void;
|
|
32
|
+
onClose?: () => void;
|
|
33
|
+
compare(a: unknown, z: unknown): boolean;
|
|
34
|
+
isSelected(value: unknown): boolean;
|
|
35
|
+
virtual: {
|
|
36
|
+
options: T[];
|
|
37
|
+
disabled: (value: T) => boolean;
|
|
38
|
+
} | null;
|
|
39
|
+
calculateIndex(value: unknown): number;
|
|
40
|
+
}>;
|
|
41
|
+
virtual: {
|
|
42
|
+
options: T[];
|
|
43
|
+
disabled: (value: T) => boolean;
|
|
44
|
+
} | null;
|
|
45
|
+
comboboxState: ComboboxState;
|
|
46
|
+
defaultToFirstOption: boolean;
|
|
47
|
+
options: {
|
|
48
|
+
id: string;
|
|
49
|
+
top?: number;
|
|
50
|
+
dataRef: ComboboxOptionDataRef<T>;
|
|
51
|
+
}[];
|
|
52
|
+
activeOptionIndex: number | null;
|
|
53
|
+
activationTrigger: ActivationTrigger;
|
|
54
|
+
isTyping: boolean;
|
|
55
|
+
}
|
|
56
|
+
export declare enum ActionTypes {
|
|
57
|
+
OpenCombobox = 0,
|
|
58
|
+
CloseCombobox = 1,
|
|
59
|
+
GoToOption = 2,
|
|
60
|
+
SetTyping = 3,
|
|
61
|
+
RegisterOption = 4,
|
|
62
|
+
UnregisterOption = 5,
|
|
63
|
+
SetOptionsTop = 6,
|
|
64
|
+
DefaultToFirstOption = 7,
|
|
65
|
+
SetActivationTrigger = 8,
|
|
66
|
+
UpdateVirtualConfiguration = 9
|
|
67
|
+
}
|
|
68
|
+
type Actions<T> = {
|
|
69
|
+
type: ActionTypes.CloseCombobox;
|
|
70
|
+
} | {
|
|
71
|
+
type: ActionTypes.OpenCombobox;
|
|
72
|
+
} | {
|
|
73
|
+
type: ActionTypes.GoToOption;
|
|
74
|
+
focus: Focus.Specific;
|
|
75
|
+
idx: number;
|
|
76
|
+
trigger?: ActivationTrigger;
|
|
77
|
+
} | {
|
|
78
|
+
type: ActionTypes.SetTyping;
|
|
79
|
+
isTyping: boolean;
|
|
80
|
+
} | {
|
|
81
|
+
type: ActionTypes.GoToOption;
|
|
82
|
+
focus: Exclude<Focus, Focus.Specific>;
|
|
83
|
+
trigger?: ActivationTrigger;
|
|
84
|
+
} | {
|
|
85
|
+
type: ActionTypes.RegisterOption;
|
|
86
|
+
payload: {
|
|
87
|
+
id: string;
|
|
88
|
+
dataRef: ComboboxOptionDataRef<T>;
|
|
89
|
+
};
|
|
90
|
+
} | {
|
|
91
|
+
type: ActionTypes.UnregisterOption;
|
|
92
|
+
id: string;
|
|
93
|
+
} | {
|
|
94
|
+
type: ActionTypes.SetOptionsTop;
|
|
95
|
+
tops: {
|
|
96
|
+
id: string;
|
|
97
|
+
top: number;
|
|
98
|
+
}[];
|
|
99
|
+
} | {
|
|
100
|
+
type: ActionTypes.DefaultToFirstOption;
|
|
101
|
+
value: boolean;
|
|
102
|
+
} | {
|
|
103
|
+
type: ActionTypes.SetActivationTrigger;
|
|
104
|
+
trigger: ActivationTrigger;
|
|
105
|
+
} | {
|
|
106
|
+
type: ActionTypes.UpdateVirtualConfiguration;
|
|
107
|
+
options: T[];
|
|
108
|
+
disabled: ((value: any) => boolean) | null;
|
|
109
|
+
};
|
|
110
|
+
export declare class ComboboxMachine<T> extends Machine<State<T>, Actions<T>> {
|
|
111
|
+
static new<T>({ id, virtual, }: {
|
|
112
|
+
id: string;
|
|
113
|
+
virtual?: {
|
|
114
|
+
options: T[];
|
|
115
|
+
disabled?: (value: T) => boolean;
|
|
116
|
+
} | null;
|
|
117
|
+
}): ComboboxMachine<T>;
|
|
118
|
+
constructor(initialState: State<T>);
|
|
119
|
+
actions: {
|
|
120
|
+
onChange: (newValue: T) => void;
|
|
121
|
+
setOptionTop: (id: string, top: number) => void;
|
|
122
|
+
registerOption: (id: string, dataRef: ComboboxOptionDataRef<T>) => () => void;
|
|
123
|
+
goToOption: (focus: {
|
|
124
|
+
focus: Focus.Specific;
|
|
125
|
+
idx: number;
|
|
126
|
+
} | {
|
|
127
|
+
focus: Exclude<Focus, Focus.Specific>;
|
|
128
|
+
}, trigger?: ActivationTrigger) => void;
|
|
129
|
+
setIsTyping: (isTyping: boolean) => void;
|
|
130
|
+
closeCombobox: () => void;
|
|
131
|
+
openCombobox: () => void;
|
|
132
|
+
setActivationTrigger: (trigger: ActivationTrigger) => void;
|
|
133
|
+
selectActiveOption: () => void;
|
|
134
|
+
};
|
|
135
|
+
selectors: {
|
|
136
|
+
activeDescendantId: (state: State<T>) => string | undefined;
|
|
137
|
+
activeOptionIndex: (state: State<T>) => number | null;
|
|
138
|
+
activeOption: (state: State<T>) => T | null;
|
|
139
|
+
isActive: (state: State<T>, value: T, id: string) => boolean;
|
|
140
|
+
};
|
|
141
|
+
reduce(state: Readonly<State<T>>, action: Actions<T>): State<T>;
|
|
142
|
+
}
|
|
143
|
+
export {};
|
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
import { Machine, batch } from '../../machine';
|
|
2
|
+
import { ActionTypes as StackActionTypes, stackMachines } from '../../machines/stack-machine';
|
|
3
|
+
import { Focus, calculateActiveIndex } from '../../utils/calculate-active-index';
|
|
4
|
+
import { match } from '../../utils/match';
|
|
5
|
+
import { findActiveIndex } from '../../utils/find-active-index';
|
|
6
|
+
import { placeAtTops, sortByOrder } from '../../utils/sort-by-order';
|
|
7
|
+
export var ComboboxState;
|
|
8
|
+
(function (ComboboxState) {
|
|
9
|
+
ComboboxState[ComboboxState["Open"] = 0] = "Open";
|
|
10
|
+
ComboboxState[ComboboxState["Closed"] = 1] = "Closed";
|
|
11
|
+
})(ComboboxState || (ComboboxState = {}));
|
|
12
|
+
export var ValueMode;
|
|
13
|
+
(function (ValueMode) {
|
|
14
|
+
ValueMode[ValueMode["Single"] = 0] = "Single";
|
|
15
|
+
ValueMode[ValueMode["Multi"] = 1] = "Multi";
|
|
16
|
+
})(ValueMode || (ValueMode = {}));
|
|
17
|
+
export var ActivationTrigger;
|
|
18
|
+
(function (ActivationTrigger) {
|
|
19
|
+
ActivationTrigger[ActivationTrigger["Pointer"] = 0] = "Pointer";
|
|
20
|
+
ActivationTrigger[ActivationTrigger["Focus"] = 1] = "Focus";
|
|
21
|
+
ActivationTrigger[ActivationTrigger["Other"] = 2] = "Other";
|
|
22
|
+
})(ActivationTrigger || (ActivationTrigger = {}));
|
|
23
|
+
export var ActionTypes;
|
|
24
|
+
(function (ActionTypes) {
|
|
25
|
+
ActionTypes[ActionTypes["OpenCombobox"] = 0] = "OpenCombobox";
|
|
26
|
+
ActionTypes[ActionTypes["CloseCombobox"] = 1] = "CloseCombobox";
|
|
27
|
+
ActionTypes[ActionTypes["GoToOption"] = 2] = "GoToOption";
|
|
28
|
+
ActionTypes[ActionTypes["SetTyping"] = 3] = "SetTyping";
|
|
29
|
+
ActionTypes[ActionTypes["RegisterOption"] = 4] = "RegisterOption";
|
|
30
|
+
ActionTypes[ActionTypes["UnregisterOption"] = 5] = "UnregisterOption";
|
|
31
|
+
ActionTypes[ActionTypes["SetOptionsTop"] = 6] = "SetOptionsTop";
|
|
32
|
+
ActionTypes[ActionTypes["DefaultToFirstOption"] = 7] = "DefaultToFirstOption";
|
|
33
|
+
ActionTypes[ActionTypes["SetActivationTrigger"] = 8] = "SetActivationTrigger";
|
|
34
|
+
ActionTypes[ActionTypes["UpdateVirtualConfiguration"] = 9] = "UpdateVirtualConfiguration";
|
|
35
|
+
})(ActionTypes || (ActionTypes = {}));
|
|
36
|
+
let reducers = {
|
|
37
|
+
[ActionTypes.CloseCombobox](state) {
|
|
38
|
+
if (state.dataRef.current?.disabled)
|
|
39
|
+
return state;
|
|
40
|
+
if (state.comboboxState === ComboboxState.Closed)
|
|
41
|
+
return state;
|
|
42
|
+
return {
|
|
43
|
+
...state,
|
|
44
|
+
activeOptionIndex: null,
|
|
45
|
+
comboboxState: ComboboxState.Closed,
|
|
46
|
+
isTyping: false,
|
|
47
|
+
// Clear the last known activation trigger. If a user interacts with the combobox using a
|
|
48
|
+
// pointer and it closes, the next interaction must not inherit that trigger.
|
|
49
|
+
activationTrigger: ActivationTrigger.Other,
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
[ActionTypes.OpenCombobox](state) {
|
|
53
|
+
if (state.dataRef.current?.disabled)
|
|
54
|
+
return state;
|
|
55
|
+
if (state.comboboxState === ComboboxState.Open)
|
|
56
|
+
return state;
|
|
57
|
+
// Check if we have a selected value that we can make active. Falsy values such as 0 are valid.
|
|
58
|
+
let value = state.dataRef.current?.value;
|
|
59
|
+
if (value !== undefined && value !== null) {
|
|
60
|
+
let idx = state.dataRef.current.calculateIndex(value);
|
|
61
|
+
if (idx !== -1) {
|
|
62
|
+
return {
|
|
63
|
+
...state,
|
|
64
|
+
activeOptionIndex: idx,
|
|
65
|
+
comboboxState: ComboboxState.Open,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
...state,
|
|
71
|
+
comboboxState: ComboboxState.Open,
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
[ActionTypes.SetTyping](state, action) {
|
|
75
|
+
if (state.isTyping === action.isTyping)
|
|
76
|
+
return state;
|
|
77
|
+
return { ...state, isTyping: action.isTyping };
|
|
78
|
+
},
|
|
79
|
+
[ActionTypes.GoToOption](state, action) {
|
|
80
|
+
if (state.dataRef.current?.disabled)
|
|
81
|
+
return state;
|
|
82
|
+
// Options are only mounted while the combobox is open, so there is nothing to go to when closed
|
|
83
|
+
if (state.comboboxState === ComboboxState.Closed)
|
|
84
|
+
return state;
|
|
85
|
+
if (state.virtual) {
|
|
86
|
+
let { options, disabled } = state.virtual;
|
|
87
|
+
let activeOptionIndex = action.focus === Focus.Specific
|
|
88
|
+
? action.idx
|
|
89
|
+
: calculateActiveIndex(action, {
|
|
90
|
+
resolveItems: () => options,
|
|
91
|
+
resolveActiveIndex: () => state.activeOptionIndex,
|
|
92
|
+
resolveDisabled: disabled,
|
|
93
|
+
resolveId() {
|
|
94
|
+
throw new Error('Function not implemented.');
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
let activationTrigger = action.trigger ?? ActivationTrigger.Other;
|
|
98
|
+
if (state.activeOptionIndex === activeOptionIndex &&
|
|
99
|
+
state.activationTrigger === activationTrigger) {
|
|
100
|
+
return state;
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
...state,
|
|
104
|
+
activeOptionIndex,
|
|
105
|
+
activationTrigger,
|
|
106
|
+
isTyping: false,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
// Like the listbox and the menu, a `null` active option starts from the edge: Next goes to the
|
|
110
|
+
// first option and Previous to the last, as on the web.
|
|
111
|
+
let activeOptionIndex = action.focus === Focus.Specific
|
|
112
|
+
? action.idx
|
|
113
|
+
: calculateActiveIndex(action, {
|
|
114
|
+
resolveItems: () => state.options,
|
|
115
|
+
resolveActiveIndex: () => state.activeOptionIndex,
|
|
116
|
+
resolveId: (item) => item.id,
|
|
117
|
+
resolveDisabled: (item) => item.dataRef.current.disabled,
|
|
118
|
+
});
|
|
119
|
+
let activationTrigger = action.trigger ?? ActivationTrigger.Other;
|
|
120
|
+
if (state.activeOptionIndex === activeOptionIndex &&
|
|
121
|
+
state.activationTrigger === activationTrigger) {
|
|
122
|
+
return state;
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
...state,
|
|
126
|
+
isTyping: false,
|
|
127
|
+
activeOptionIndex,
|
|
128
|
+
activationTrigger,
|
|
129
|
+
};
|
|
130
|
+
},
|
|
131
|
+
[ActionTypes.RegisterOption]: (state, action) => {
|
|
132
|
+
let options = sortByOrder([...state.options, action.payload]);
|
|
133
|
+
// Sorting can move the active option, so look it up again.
|
|
134
|
+
let activeOptionIndex = findActiveIndex(state.options, state.activeOptionIndex, options);
|
|
135
|
+
// Check if we need to make the newly registered option active.
|
|
136
|
+
if (state.activeOptionIndex === null) {
|
|
137
|
+
if (state.dataRef.current.isSelected?.(action.payload.dataRef.current.value)) {
|
|
138
|
+
activeOptionIndex = options.indexOf(action.payload);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
...state,
|
|
143
|
+
options,
|
|
144
|
+
activeOptionIndex,
|
|
145
|
+
activationTrigger: ActivationTrigger.Other,
|
|
146
|
+
};
|
|
147
|
+
},
|
|
148
|
+
[ActionTypes.UnregisterOption]: (state, action) => {
|
|
149
|
+
let options = state.options.filter((option) => option.id !== action.id);
|
|
150
|
+
return {
|
|
151
|
+
...state,
|
|
152
|
+
options,
|
|
153
|
+
activeOptionIndex: findActiveIndex(state.options, state.activeOptionIndex, options),
|
|
154
|
+
activationTrigger: ActivationTrigger.Other,
|
|
155
|
+
};
|
|
156
|
+
},
|
|
157
|
+
[ActionTypes.SetOptionsTop]: (state, action) => {
|
|
158
|
+
let options = placeAtTops(state.options, action.tops);
|
|
159
|
+
if (!options)
|
|
160
|
+
return state;
|
|
161
|
+
return {
|
|
162
|
+
...state,
|
|
163
|
+
options,
|
|
164
|
+
activeOptionIndex: findActiveIndex(state.options, state.activeOptionIndex, options),
|
|
165
|
+
};
|
|
166
|
+
},
|
|
167
|
+
[ActionTypes.DefaultToFirstOption]: (state, action) => {
|
|
168
|
+
if (state.defaultToFirstOption === action.value)
|
|
169
|
+
return state;
|
|
170
|
+
return {
|
|
171
|
+
...state,
|
|
172
|
+
defaultToFirstOption: action.value,
|
|
173
|
+
};
|
|
174
|
+
},
|
|
175
|
+
[ActionTypes.SetActivationTrigger]: (state, action) => {
|
|
176
|
+
if (state.activationTrigger === action.trigger) {
|
|
177
|
+
return state;
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
...state,
|
|
181
|
+
activationTrigger: action.trigger,
|
|
182
|
+
};
|
|
183
|
+
},
|
|
184
|
+
[ActionTypes.UpdateVirtualConfiguration]: (state, action) => {
|
|
185
|
+
if (state.virtual === null) {
|
|
186
|
+
return {
|
|
187
|
+
...state,
|
|
188
|
+
virtual: { options: action.options, disabled: action.disabled ?? (() => false) },
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
if (state.virtual.options === action.options && state.virtual.disabled === action.disabled) {
|
|
192
|
+
return state;
|
|
193
|
+
}
|
|
194
|
+
let adjustedActiveOptionIndex = state.activeOptionIndex;
|
|
195
|
+
if (state.activeOptionIndex !== null) {
|
|
196
|
+
let idx = action.options.indexOf(state.virtual.options[state.activeOptionIndex]);
|
|
197
|
+
if (idx !== -1) {
|
|
198
|
+
adjustedActiveOptionIndex = idx;
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
adjustedActiveOptionIndex = null;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return {
|
|
205
|
+
...state,
|
|
206
|
+
activeOptionIndex: adjustedActiveOptionIndex,
|
|
207
|
+
virtual: { options: action.options, disabled: action.disabled ?? (() => false) },
|
|
208
|
+
};
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
export class ComboboxMachine extends Machine {
|
|
212
|
+
static new({ id, virtual = null, }) {
|
|
213
|
+
return new ComboboxMachine({
|
|
214
|
+
id,
|
|
215
|
+
// @ts-expect-error TODO: Re-structure such that we don't need to ignore this
|
|
216
|
+
dataRef: { current: {} },
|
|
217
|
+
comboboxState: ComboboxState.Closed,
|
|
218
|
+
isTyping: false,
|
|
219
|
+
options: [],
|
|
220
|
+
virtual: virtual
|
|
221
|
+
? { options: virtual.options, disabled: virtual.disabled ?? (() => false) }
|
|
222
|
+
: null,
|
|
223
|
+
activeOptionIndex: null,
|
|
224
|
+
activationTrigger: ActivationTrigger.Other,
|
|
225
|
+
defaultToFirstOption: false,
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
constructor(initialState) {
|
|
229
|
+
super(initialState);
|
|
230
|
+
// When the combobox is open, and it's not on the top of the hierarchy, we
|
|
231
|
+
// should close it again.
|
|
232
|
+
{
|
|
233
|
+
let id = this.state.id;
|
|
234
|
+
let stackMachine = stackMachines.get(null);
|
|
235
|
+
this.disposables.add(stackMachine.on(StackActionTypes.Push, (state) => {
|
|
236
|
+
if (!stackMachine.selectors.isTop(state, id) &&
|
|
237
|
+
this.state.comboboxState === ComboboxState.Open) {
|
|
238
|
+
this.actions.closeCombobox();
|
|
239
|
+
}
|
|
240
|
+
}));
|
|
241
|
+
this.on(ActionTypes.OpenCombobox, () => stackMachine.actions.push(id));
|
|
242
|
+
this.on(ActionTypes.CloseCombobox, () => stackMachine.actions.pop(id));
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
actions = {
|
|
246
|
+
onChange: (newValue) => {
|
|
247
|
+
let { onChange, compare, mode, value } = this.state.dataRef.current;
|
|
248
|
+
return match(mode, {
|
|
249
|
+
[ValueMode.Single]: () => {
|
|
250
|
+
return onChange?.(newValue);
|
|
251
|
+
},
|
|
252
|
+
[ValueMode.Multi]: () => {
|
|
253
|
+
let copy = value.slice();
|
|
254
|
+
let idx = copy.findIndex((item) => compare(item, newValue));
|
|
255
|
+
if (idx === -1) {
|
|
256
|
+
copy.push(newValue);
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
copy.splice(idx, 1);
|
|
260
|
+
}
|
|
261
|
+
return onChange?.(copy);
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
},
|
|
265
|
+
// Batched version, so the options that mount together are sorted once
|
|
266
|
+
setOptionTop: batch(() => {
|
|
267
|
+
let tops = [];
|
|
268
|
+
return [
|
|
269
|
+
(id, top) => tops.push({ id, top }),
|
|
270
|
+
() => this.send({ type: ActionTypes.SetOptionsTop, tops: tops.splice(0) }),
|
|
271
|
+
];
|
|
272
|
+
}),
|
|
273
|
+
registerOption: (id, dataRef) => {
|
|
274
|
+
this.send({ type: ActionTypes.RegisterOption, payload: { id, dataRef } });
|
|
275
|
+
return () => {
|
|
276
|
+
// When we are unregistering the currently active option, then we also have to make sure to
|
|
277
|
+
// reset the `defaultToFirstOption` flag, so that visually something is selected and the next
|
|
278
|
+
// time you press a key it goes to the proper next or previous option in the list.
|
|
279
|
+
if (this.state.activeOptionIndex ===
|
|
280
|
+
this.state.dataRef.current.calculateIndex(dataRef.current.value)) {
|
|
281
|
+
this.send({ type: ActionTypes.DefaultToFirstOption, value: true });
|
|
282
|
+
}
|
|
283
|
+
this.send({ type: ActionTypes.UnregisterOption, id });
|
|
284
|
+
};
|
|
285
|
+
},
|
|
286
|
+
goToOption: (focus, trigger) => {
|
|
287
|
+
this.send({ type: ActionTypes.DefaultToFirstOption, value: false });
|
|
288
|
+
return this.send({ type: ActionTypes.GoToOption, ...focus, trigger });
|
|
289
|
+
},
|
|
290
|
+
setIsTyping: (isTyping) => {
|
|
291
|
+
this.send({ type: ActionTypes.SetTyping, isTyping });
|
|
292
|
+
},
|
|
293
|
+
closeCombobox: () => {
|
|
294
|
+
this.send({ type: ActionTypes.CloseCombobox });
|
|
295
|
+
this.send({ type: ActionTypes.DefaultToFirstOption, value: false });
|
|
296
|
+
this.state.dataRef.current.onClose?.();
|
|
297
|
+
},
|
|
298
|
+
openCombobox: () => {
|
|
299
|
+
this.send({ type: ActionTypes.OpenCombobox });
|
|
300
|
+
this.send({ type: ActionTypes.DefaultToFirstOption, value: true });
|
|
301
|
+
},
|
|
302
|
+
setActivationTrigger: (trigger) => {
|
|
303
|
+
this.send({ type: ActionTypes.SetActivationTrigger, trigger });
|
|
304
|
+
},
|
|
305
|
+
selectActiveOption: () => {
|
|
306
|
+
let activeOptionIndex = this.selectors.activeOptionIndex(this.state);
|
|
307
|
+
if (activeOptionIndex === null)
|
|
308
|
+
return;
|
|
309
|
+
this.actions.setIsTyping(false);
|
|
310
|
+
if (this.state.virtual) {
|
|
311
|
+
this.actions.onChange(this.state.virtual.options[activeOptionIndex]);
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
let { dataRef } = this.state.options[activeOptionIndex];
|
|
315
|
+
this.actions.onChange(dataRef.current.value);
|
|
316
|
+
}
|
|
317
|
+
// It could happen that the `activeOptionIndex` stored in state is actually null, but we are
|
|
318
|
+
// getting the fallback active option back instead.
|
|
319
|
+
this.actions.goToOption({ focus: Focus.Specific, idx: activeOptionIndex });
|
|
320
|
+
},
|
|
321
|
+
};
|
|
322
|
+
selectors = {
|
|
323
|
+
activeDescendantId: (state) => {
|
|
324
|
+
const activeOptionIndex = this.selectors.activeOptionIndex(state);
|
|
325
|
+
if (activeOptionIndex === null) {
|
|
326
|
+
return undefined;
|
|
327
|
+
}
|
|
328
|
+
if (!state.virtual) {
|
|
329
|
+
return state.options[activeOptionIndex]?.id;
|
|
330
|
+
}
|
|
331
|
+
return state.options.find((option) => {
|
|
332
|
+
return (!option.dataRef.current.disabled &&
|
|
333
|
+
state.dataRef.current.compare(option.dataRef.current.value, state.virtual.options[activeOptionIndex]));
|
|
334
|
+
})?.id;
|
|
335
|
+
},
|
|
336
|
+
activeOptionIndex: (state) => {
|
|
337
|
+
if (state.defaultToFirstOption &&
|
|
338
|
+
state.activeOptionIndex === null &&
|
|
339
|
+
(state.virtual ? state.virtual.options.length > 0 : state.options.length > 0)) {
|
|
340
|
+
if (state.virtual) {
|
|
341
|
+
let { options, disabled } = state.virtual;
|
|
342
|
+
let activeOptionIndex = options.findIndex((option) => !(disabled?.(option) ?? false));
|
|
343
|
+
if (activeOptionIndex !== -1) {
|
|
344
|
+
return activeOptionIndex;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
let activeOptionIndex = state.options.findIndex((option) => {
|
|
348
|
+
return !option.dataRef.current.disabled;
|
|
349
|
+
});
|
|
350
|
+
if (activeOptionIndex !== -1) {
|
|
351
|
+
return activeOptionIndex;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return state.activeOptionIndex;
|
|
355
|
+
},
|
|
356
|
+
activeOption: (state) => {
|
|
357
|
+
let activeOptionIndex = this.selectors.activeOptionIndex(state);
|
|
358
|
+
return activeOptionIndex === null
|
|
359
|
+
? null
|
|
360
|
+
: state.virtual
|
|
361
|
+
? state.virtual.options[activeOptionIndex ?? 0]
|
|
362
|
+
: (state.options[activeOptionIndex]?.dataRef.current.value ?? null);
|
|
363
|
+
},
|
|
364
|
+
isActive: (state, value, id) => {
|
|
365
|
+
let activeOptionIndex = this.selectors.activeOptionIndex(state);
|
|
366
|
+
if (activeOptionIndex === null)
|
|
367
|
+
return false;
|
|
368
|
+
if (state.virtual) {
|
|
369
|
+
return activeOptionIndex === state.dataRef.current.calculateIndex(value);
|
|
370
|
+
}
|
|
371
|
+
return state.options[activeOptionIndex]?.id === id;
|
|
372
|
+
},
|
|
373
|
+
};
|
|
374
|
+
reduce(state, action) {
|
|
375
|
+
return match(action.type, reducers, state, action);
|
|
376
|
+
}
|
|
377
|
+
}
|