@ankhorage/zora 28.5.3 → 28.6.1
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/CHANGELOG.md +12 -0
- package/README.md +1 -1
- package/dist/capabilities/index.js +73 -3
- package/dist/capabilities/index.js.map +3 -3
- package/dist/features/form/select/adapters/inbound/Select.native.d.ts.map +1 -1
- package/dist/features/form/select/adapters/inbound/Select.native.js +71 -8
- package/dist/features/form/select/adapters/inbound/Select.native.js.map +1 -1
- package/dist/features/form/select/adapters/inbound/Select.web.d.ts.map +1 -1
- package/dist/features/form/select/adapters/inbound/Select.web.js +5 -3
- package/dist/features/form/select/adapters/inbound/Select.web.js.map +1 -1
- package/dist/features/form/select/composition/SelectSearchInput.d.ts +10 -0
- package/dist/features/form/select/composition/SelectSearchInput.d.ts.map +1 -0
- package/dist/features/form/select/composition/SelectSearchInput.js +11 -0
- package/dist/features/form/select/composition/SelectSearchInput.js.map +1 -0
- package/dist/features/form/select/composition/getVisibleSelectOptions.d.ts +4 -0
- package/dist/features/form/select/composition/getVisibleSelectOptions.d.ts.map +1 -0
- package/dist/features/form/select/composition/getVisibleSelectOptions.js +8 -0
- package/dist/features/form/select/composition/getVisibleSelectOptions.js.map +1 -0
- package/dist/features/form/select/composition/useSelectController.d.ts +3 -0
- package/dist/features/form/select/composition/useSelectController.d.ts.map +1 -1
- package/dist/features/form/select/composition/useSelectController.js +11 -2
- package/dist/features/form/select/composition/useSelectController.js.map +1 -1
- package/dist/features/form/select/selectMeta.d.ts +99 -1
- package/dist/features/form/select/selectMeta.d.ts.map +1 -1
- package/dist/features/form/select/selectMeta.js +71 -1
- package/dist/features/form/select/selectMeta.js.map +1 -1
- package/dist/features/registry/bindableComponentMeta.d.ts +79 -1
- package/dist/features/registry/bindableComponentMeta.d.ts.map +1 -1
- package/dist/features/registry/bindableComponentMeta.js +59 -1
- package/dist/features/registry/bindableComponentMeta.js.map +1 -1
- package/dist/types/select.d.ts +10 -0
- package/dist/types/select.d.ts.map +1 -1
- package/dist/types/select.js.map +1 -1
- package/package.json +28 -2
- package/src/features/form/select/adapters/inbound/Select.native.test.tsx +322 -0
- package/src/features/form/select/adapters/inbound/Select.native.tsx +135 -13
- package/src/features/form/select/adapters/inbound/Select.web.tsx +6 -3
- package/src/features/form/select/composition/SelectSearchInput.tsx +33 -0
- package/src/features/form/select/composition/getVisibleSelectOptions.test.ts +22 -0
- package/src/features/form/select/composition/getVisibleSelectOptions.ts +13 -0
- package/src/features/form/select/composition/useSelectController.ts +25 -2
- package/src/features/form/select/public.test.ts +31 -1
- package/src/features/form/select/selectMeta.ts +71 -1
- package/src/features/registry/bindableComponentMeta.test.ts +6 -0
- package/src/features/registry/bindableComponentMeta.ts +59 -1
- package/src/features/registry/featureManifest.test.ts +10 -0
- package/src/types/select.ts +10 -0
- package/web-dist/chunks/{Accordion-sj1n06dw.js → Accordion-9dk62zn9.js} +84 -24
- package/web-dist/chunks/Accordion-ar8qe05j.js +53 -0
- package/web-dist/chunks/{Accordion-ge7cjtwy.js → Accordion-mpb070cf.js} +3 -3
- package/web-dist/chunks/{Accordion-554f42yj.js → Accordion-sj30964p.js} +4 -4
- package/web-dist/chunks/{Accordion-2bygfpwg.js → Accordion-xhxv2v0g.js} +62 -2
- package/web-dist/chunks/{RichTextClient-70qt7037.js → RichTextClient-7qqj2dpw.js} +3 -4
- package/web-dist/components/auth-screen/AuthScreen.js +2 -3
- package/web-dist/components/color-hue-scale/ColorHueScale.js +4 -3
- package/web-dist/components/color-picker/ColorPicker.js +4 -3
- package/web-dist/components/color-tone/ColorTone.js +4 -3
- package/web-dist/components/color-wheel/ColorWheel.js +4 -3
- package/web-dist/components/forgot-password-form/ForgotPasswordForm.js +2 -3
- package/web-dist/components/o-auth-provider-button/OAuthProviderButton.js +2 -3
- package/web-dist/components/o-auth-provider-list/OAuthProviderList.js +2 -3
- package/web-dist/components/otp-form/OtpForm.js +2 -3
- package/web-dist/components/rich-text/RichText.js +2 -2
- package/web-dist/components/search-input/SearchInput.js +5 -52
- package/web-dist/components/select/Select.js +3 -1
- package/web-dist/components/select/Select.types-0.d.ts +10 -0
- package/web-dist/components/sign-in-form/SignInForm.js +2 -3
- package/web-dist/components/sign-up-form/SignUpForm.js +2 -3
- package/web-dist/components/text-input/TextInput.js +4 -5
- package/web-dist/manifest.json +39 -41
- package/web-dist/chunks/Accordion-49d0frg7.js +0 -66
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { afterAll, expect, mock, test } from 'bun:test';
|
|
2
|
+
import { Window } from 'happy-dom';
|
|
3
|
+
import React, { act } from 'react';
|
|
4
|
+
import { createRoot } from 'react-dom/client';
|
|
5
|
+
|
|
6
|
+
const browser = new Window();
|
|
7
|
+
const globals = ['window', 'document', 'Node', 'navigator', 'IS_REACT_ACT_ENVIRONMENT'] as const;
|
|
8
|
+
const previousGlobals = globals.map(
|
|
9
|
+
(key) => [key, Object.getOwnPropertyDescriptor(globalThis, key)] as const,
|
|
10
|
+
);
|
|
11
|
+
Object.assign(globalThis, {
|
|
12
|
+
IS_REACT_ACT_ENVIRONMENT: true,
|
|
13
|
+
Node: browser.Node,
|
|
14
|
+
document: browser.document,
|
|
15
|
+
navigator: browser.navigator,
|
|
16
|
+
window: browser,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
let renderSheetContent: React.Dispatch<React.SetStateAction<React.ReactNode>> | undefined;
|
|
20
|
+
let activeSheet:
|
|
21
|
+
| {
|
|
22
|
+
readonly content: React.ReactNode;
|
|
23
|
+
readonly onDismiss?: () => void;
|
|
24
|
+
}
|
|
25
|
+
| undefined;
|
|
26
|
+
const dismissCalls: number[] = [];
|
|
27
|
+
|
|
28
|
+
/*** Replaces the active request as another shared BottomSheet consumer would. */
|
|
29
|
+
function replaceSheet(content: React.ReactNode) {
|
|
30
|
+
activeSheet = { content };
|
|
31
|
+
renderSheetContent?.(content);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/*** Dismisses the active request through the canonical BottomSheet controller. */
|
|
35
|
+
function dismissSheet() {
|
|
36
|
+
const sheet = activeSheet;
|
|
37
|
+
activeSheet = undefined;
|
|
38
|
+
renderSheetContent?.(null);
|
|
39
|
+
sheet?.onDismiss?.();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const bottomSheetController = {
|
|
43
|
+
dismiss: () => {
|
|
44
|
+
dismissCalls.push(1);
|
|
45
|
+
dismissSheet();
|
|
46
|
+
},
|
|
47
|
+
present: ({ content, onDismiss }: { content: React.ReactNode; onDismiss?: () => void }) => {
|
|
48
|
+
activeSheet = { content, onDismiss };
|
|
49
|
+
renderSheetContent?.(content);
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
await mock.module('react-native', () => ({
|
|
54
|
+
StyleSheet: { create: <T,>(styles: T) => styles },
|
|
55
|
+
}));
|
|
56
|
+
await mock.module('../../../../bottom-sheet/public', () => ({
|
|
57
|
+
BottomSheetFlatList: ({
|
|
58
|
+
data,
|
|
59
|
+
ListHeaderComponent,
|
|
60
|
+
renderItem,
|
|
61
|
+
}: {
|
|
62
|
+
data: readonly { label: string; value: string }[];
|
|
63
|
+
ListHeaderComponent: React.ReactNode;
|
|
64
|
+
renderItem: (info: { item: { label: string; value: string } }) => React.ReactNode;
|
|
65
|
+
}) => (
|
|
66
|
+
<div data-testid="select-sheet">
|
|
67
|
+
{ListHeaderComponent}
|
|
68
|
+
{data.map((item) => (
|
|
69
|
+
<React.Fragment key={item.value}>{renderItem({ item })}</React.Fragment>
|
|
70
|
+
))}
|
|
71
|
+
</div>
|
|
72
|
+
),
|
|
73
|
+
useBottomSheet: () => bottomSheetController,
|
|
74
|
+
}));
|
|
75
|
+
await mock.module('../../../../theme/adapters/inbound/withZoraThemeScope', () => ({
|
|
76
|
+
withZoraThemeScope: <P,>(Component: React.ComponentType<P>) => Component,
|
|
77
|
+
}));
|
|
78
|
+
await mock.module('../../composition/SelectField', () => ({
|
|
79
|
+
SelectField: ({ children }: { children: React.ReactNode }) => <>{children}</>,
|
|
80
|
+
}));
|
|
81
|
+
await mock.module('../../composition/SelectOptionRow', () => ({
|
|
82
|
+
SelectOptionRow: ({
|
|
83
|
+
onSelect,
|
|
84
|
+
option,
|
|
85
|
+
}: {
|
|
86
|
+
onSelect: (value: string) => void;
|
|
87
|
+
option: { label: string; value: string };
|
|
88
|
+
}) => <button onClick={() => onSelect(option.value)}>{option.label}</button>,
|
|
89
|
+
}));
|
|
90
|
+
await mock.module('../../composition/SelectSearchInput', () => ({
|
|
91
|
+
SelectSearchInput: ({
|
|
92
|
+
onSearchChange,
|
|
93
|
+
props,
|
|
94
|
+
searchValue,
|
|
95
|
+
}: {
|
|
96
|
+
onSearchChange: (value: string) => void;
|
|
97
|
+
props: { searchable?: boolean };
|
|
98
|
+
searchValue: string;
|
|
99
|
+
}) =>
|
|
100
|
+
props.searchable ? (
|
|
101
|
+
<input
|
|
102
|
+
aria-label="Search options"
|
|
103
|
+
onInput={(event) => onSearchChange(event.currentTarget.value)}
|
|
104
|
+
value={searchValue}
|
|
105
|
+
/>
|
|
106
|
+
) : null,
|
|
107
|
+
}));
|
|
108
|
+
await mock.module('../../composition/SelectTrigger', () => ({
|
|
109
|
+
SelectTrigger: ({ onPress }: { onPress: () => void }) => <button onClick={onPress}>Open</button>,
|
|
110
|
+
}));
|
|
111
|
+
|
|
112
|
+
const { Select } = await import('./Select.native');
|
|
113
|
+
|
|
114
|
+
afterAll(() => {
|
|
115
|
+
browser.close();
|
|
116
|
+
for (const [key, descriptor] of previousGlobals) {
|
|
117
|
+
if (descriptor) Object.defineProperty(globalThis, key, descriptor);
|
|
118
|
+
else Reflect.deleteProperty(globalThis, key);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
/*** Mounts Select beside the shared sheet content owned by the canonical controller. */
|
|
123
|
+
function mount(strict = false) {
|
|
124
|
+
const host = document.createElement('div');
|
|
125
|
+
document.body.append(host);
|
|
126
|
+
const root = createRoot(host);
|
|
127
|
+
|
|
128
|
+
function SheetHost({ children }: { children: React.ReactNode }) {
|
|
129
|
+
const [content, setContent] = React.useState<React.ReactNode>(null);
|
|
130
|
+
renderSheetContent = setContent;
|
|
131
|
+
return (
|
|
132
|
+
<>
|
|
133
|
+
{children}
|
|
134
|
+
{content}
|
|
135
|
+
</>
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return {
|
|
140
|
+
host,
|
|
141
|
+
render: (element: React.ReactNode) =>
|
|
142
|
+
act(() =>
|
|
143
|
+
Promise.resolve().then(() =>
|
|
144
|
+
root.render(
|
|
145
|
+
strict ? (
|
|
146
|
+
<React.StrictMode>
|
|
147
|
+
<SheetHost>{element}</SheetHost>
|
|
148
|
+
</React.StrictMode>
|
|
149
|
+
) : (
|
|
150
|
+
<SheetHost>{element}</SheetHost>
|
|
151
|
+
),
|
|
152
|
+
),
|
|
153
|
+
),
|
|
154
|
+
),
|
|
155
|
+
unmount: () => act(() => Promise.resolve().then(() => root.unmount())),
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
test('keeps an open native sheet reactive to search, options, and current selection callbacks', async () => {
|
|
160
|
+
const mounted = mount();
|
|
161
|
+
const initialSelection: string[] = [];
|
|
162
|
+
const currentSelection: string[] = [];
|
|
163
|
+
dismissCalls.length = 0;
|
|
164
|
+
activeSheet = undefined;
|
|
165
|
+
|
|
166
|
+
try {
|
|
167
|
+
await mounted.render(
|
|
168
|
+
<Select
|
|
169
|
+
onValueChange={(value) => initialSelection.push(value)}
|
|
170
|
+
options={[
|
|
171
|
+
{ label: 'Alpha', value: 'alpha' },
|
|
172
|
+
{ label: 'Beta', value: 'beta' },
|
|
173
|
+
]}
|
|
174
|
+
searchable
|
|
175
|
+
/>,
|
|
176
|
+
);
|
|
177
|
+
await act(() => Promise.resolve().then(() => mounted.host.querySelector('button')?.click()));
|
|
178
|
+
|
|
179
|
+
const search = mounted.host.querySelector<HTMLInputElement>('[aria-label="Search options"]');
|
|
180
|
+
if (!search) throw new Error('Missing native Select search input');
|
|
181
|
+
search.value = 'beta';
|
|
182
|
+
await act(() =>
|
|
183
|
+
Promise.resolve().then(() =>
|
|
184
|
+
search.dispatchEvent(new browser.Event('input', { bubbles: true })),
|
|
185
|
+
),
|
|
186
|
+
);
|
|
187
|
+
expect(mounted.host.textContent).toContain('Beta');
|
|
188
|
+
expect(mounted.host.textContent).not.toContain('Alpha');
|
|
189
|
+
|
|
190
|
+
await mounted.render(
|
|
191
|
+
<Select
|
|
192
|
+
onValueChange={(value) => currentSelection.push(value)}
|
|
193
|
+
options={[
|
|
194
|
+
{ label: 'Beta', value: 'beta' },
|
|
195
|
+
{ label: 'Gamma', value: 'gamma' },
|
|
196
|
+
]}
|
|
197
|
+
searchable
|
|
198
|
+
searchValue="gamma"
|
|
199
|
+
/>,
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
const controlledSearch = mounted.host.querySelector<HTMLInputElement>(
|
|
203
|
+
'[aria-label="Search options"]',
|
|
204
|
+
);
|
|
205
|
+
const gamma = [...mounted.host.querySelectorAll('button')].find(
|
|
206
|
+
(button) => button.textContent === 'Gamma',
|
|
207
|
+
);
|
|
208
|
+
expect(controlledSearch?.value).toBe('gamma');
|
|
209
|
+
expect(mounted.host.textContent).toContain('Gamma');
|
|
210
|
+
expect(mounted.host.textContent).not.toContain('Beta');
|
|
211
|
+
await act(() => Promise.resolve().then(() => gamma?.click()));
|
|
212
|
+
|
|
213
|
+
expect(initialSelection).toEqual([]);
|
|
214
|
+
expect(currentSelection).toEqual(['gamma']);
|
|
215
|
+
expect(dismissCalls).toHaveLength(1);
|
|
216
|
+
expect(mounted.host.querySelector('[data-testid="select-sheet"]')).toBeNull();
|
|
217
|
+
} finally {
|
|
218
|
+
await mounted.unmount();
|
|
219
|
+
mounted.host.remove();
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test('releases native sheet ownership when its Select unmounts or another sheet replaces it', async () => {
|
|
224
|
+
const mounted = mount();
|
|
225
|
+
dismissCalls.length = 0;
|
|
226
|
+
activeSheet = undefined;
|
|
227
|
+
|
|
228
|
+
try {
|
|
229
|
+
await mounted.render(<Select options={[{ label: 'Alpha', value: 'alpha' }]} searchable />);
|
|
230
|
+
await act(() => Promise.resolve().then(() => mounted.host.querySelector('button')?.click()));
|
|
231
|
+
expect(mounted.host.querySelector('[data-testid="select-sheet"]')).not.toBeNull();
|
|
232
|
+
|
|
233
|
+
await mounted.unmount();
|
|
234
|
+
expect(dismissCalls).toHaveLength(1);
|
|
235
|
+
expect(activeSheet).toBeUndefined();
|
|
236
|
+
} finally {
|
|
237
|
+
mounted.host.remove();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const replacement = mount();
|
|
241
|
+
dismissCalls.length = 0;
|
|
242
|
+
activeSheet = undefined;
|
|
243
|
+
|
|
244
|
+
try {
|
|
245
|
+
await replacement.render(<Select options={[{ label: 'Alpha', value: 'alpha' }]} searchable />);
|
|
246
|
+
await act(() =>
|
|
247
|
+
Promise.resolve().then(() => replacement.host.querySelector('button')?.click()),
|
|
248
|
+
);
|
|
249
|
+
await act(() => Promise.resolve().then(() => replaceSheet(<div data-testid="other-sheet" />)));
|
|
250
|
+
|
|
251
|
+
await replacement.render(<Select options={[{ label: 'Beta', value: 'beta' }]} searchable />);
|
|
252
|
+
expect(replacement.host.querySelector('[data-testid="other-sheet"]')).not.toBeNull();
|
|
253
|
+
expect(replacement.host.querySelector('[data-testid="select-sheet"]')).toBeNull();
|
|
254
|
+
|
|
255
|
+
await replacement.unmount();
|
|
256
|
+
expect(dismissCalls).toHaveLength(0);
|
|
257
|
+
expect(replacement.host.querySelector('[data-testid="other-sheet"]')).toBeNull();
|
|
258
|
+
} finally {
|
|
259
|
+
replacement.host.remove();
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test('does not reclaim a native sheet after external dismissal', async () => {
|
|
264
|
+
const mounted = mount();
|
|
265
|
+
dismissCalls.length = 0;
|
|
266
|
+
activeSheet = undefined;
|
|
267
|
+
|
|
268
|
+
try {
|
|
269
|
+
await mounted.render(<Select options={[{ label: 'Alpha', value: 'alpha' }]} searchable />);
|
|
270
|
+
await act(() => Promise.resolve().then(() => mounted.host.querySelector('button')?.click()));
|
|
271
|
+
await act(() => Promise.resolve().then(() => dismissSheet()));
|
|
272
|
+
|
|
273
|
+
await mounted.render(<Select options={[{ label: 'Beta', value: 'beta' }]} searchable />);
|
|
274
|
+
expect(mounted.host.querySelector('[data-testid="select-sheet"]')).toBeNull();
|
|
275
|
+
expect(dismissCalls).toHaveLength(0);
|
|
276
|
+
} finally {
|
|
277
|
+
await mounted.unmount();
|
|
278
|
+
mounted.host.remove();
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
test('keeps its active native sheet through StrictMode effect cleanup and setup', async () => {
|
|
283
|
+
const mounted = mount(true);
|
|
284
|
+
dismissCalls.length = 0;
|
|
285
|
+
activeSheet = undefined;
|
|
286
|
+
|
|
287
|
+
try {
|
|
288
|
+
await mounted.render(<Select options={[{ label: 'Alpha', value: 'alpha' }]} searchable />);
|
|
289
|
+
await act(() => Promise.resolve().then(() => mounted.host.querySelector('button')?.click()));
|
|
290
|
+
|
|
291
|
+
expect(mounted.host.querySelector('[data-testid="select-sheet"]')).not.toBeNull();
|
|
292
|
+
expect(dismissCalls).toHaveLength(0);
|
|
293
|
+
} finally {
|
|
294
|
+
await mounted.unmount();
|
|
295
|
+
mounted.host.remove();
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
test('does not reclaim a replacement sheet when its previous Select receives updated props', async () => {
|
|
300
|
+
const mounted = mount();
|
|
301
|
+
dismissCalls.length = 0;
|
|
302
|
+
activeSheet = undefined;
|
|
303
|
+
|
|
304
|
+
try {
|
|
305
|
+
await mounted.render(<Select options={[{ label: 'Alpha', value: 'alpha' }]} searchable />);
|
|
306
|
+
await act(() => Promise.resolve().then(() => mounted.host.querySelector('button')?.click()));
|
|
307
|
+
|
|
308
|
+
await act(() =>
|
|
309
|
+
Promise.resolve().then(() => {
|
|
310
|
+
replaceSheet(<div data-testid="other-sheet" />);
|
|
311
|
+
return mounted.render(<Select options={[{ label: 'Beta', value: 'beta' }]} searchable />);
|
|
312
|
+
}),
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
expect(mounted.host.querySelector('[data-testid="other-sheet"]')).not.toBeNull();
|
|
316
|
+
expect(mounted.host.querySelector('[data-testid="select-sheet"]')).toBeNull();
|
|
317
|
+
expect(dismissCalls).toHaveLength(0);
|
|
318
|
+
} finally {
|
|
319
|
+
await mounted.unmount();
|
|
320
|
+
mounted.host.remove();
|
|
321
|
+
}
|
|
322
|
+
});
|
|
@@ -6,6 +6,7 @@ import { BottomSheetFlatList, useBottomSheet } from '../../../../bottom-sheet/pu
|
|
|
6
6
|
import { withZoraThemeScope } from '../../../../theme/adapters/inbound/withZoraThemeScope';
|
|
7
7
|
import { SelectField } from '../../composition/SelectField';
|
|
8
8
|
import { SelectOptionRow } from '../../composition/SelectOptionRow';
|
|
9
|
+
import { SelectSearchInput } from '../../composition/SelectSearchInput';
|
|
9
10
|
import { SelectTrigger } from '../../composition/SelectTrigger';
|
|
10
11
|
import { useSelectController } from '../../composition/useSelectController';
|
|
11
12
|
|
|
@@ -21,17 +22,47 @@ function SelectInner<TValue extends string = string>({
|
|
|
21
22
|
...props
|
|
22
23
|
}: SelectProps<TValue>) {
|
|
23
24
|
const { dismiss, present } = useBottomSheet();
|
|
24
|
-
const
|
|
25
|
+
const [open, setOpen] = React.useState(false);
|
|
26
|
+
const ownsSheet = React.useRef(false);
|
|
27
|
+
const sheetRequestId = React.useRef(0);
|
|
28
|
+
const sheetId = React.useId();
|
|
29
|
+
const { search, searchValue, select, selectedOption, value, visibleOptions } =
|
|
30
|
+
useSelectController(props);
|
|
25
31
|
const passive = props.interactionPolicy === 'passive';
|
|
26
|
-
const { disabled, interactionPolicy,
|
|
27
|
-
const listData = React.useMemo(() => [...
|
|
32
|
+
const { disabled, interactionPolicy, readOnly, testID } = props;
|
|
33
|
+
const listData = React.useMemo(() => [...visibleOptions], [visibleOptions]);
|
|
34
|
+
|
|
35
|
+
const releaseSheet = React.useCallback(() => {
|
|
36
|
+
ownsSheet.current = false;
|
|
37
|
+
setOpen(false);
|
|
38
|
+
}, []);
|
|
39
|
+
|
|
40
|
+
const isCurrentSheetRequest = React.useCallback(
|
|
41
|
+
(requestId: number) => sheetRequestId.current === requestId,
|
|
42
|
+
[],
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const claimSheet = React.useCallback(
|
|
46
|
+
(requestId: number) => {
|
|
47
|
+
if (isCurrentSheetRequest(requestId)) ownsSheet.current = true;
|
|
48
|
+
},
|
|
49
|
+
[isCurrentSheetRequest],
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const releaseSheetOwnership = React.useCallback(
|
|
53
|
+
(requestId: number) => {
|
|
54
|
+
if (isCurrentSheetRequest(requestId)) ownsSheet.current = false;
|
|
55
|
+
},
|
|
56
|
+
[isCurrentSheetRequest],
|
|
57
|
+
);
|
|
28
58
|
|
|
29
59
|
const handleSelect = React.useCallback(
|
|
30
60
|
(nextValue: TValue) => {
|
|
31
61
|
select(nextValue);
|
|
62
|
+
releaseSheet();
|
|
32
63
|
dismiss();
|
|
33
64
|
},
|
|
34
|
-
[dismiss, select],
|
|
65
|
+
[dismiss, releaseSheet, select],
|
|
35
66
|
);
|
|
36
67
|
|
|
37
68
|
const renderOption = React.useCallback(
|
|
@@ -47,22 +78,59 @@ function SelectInner<TValue extends string = string>({
|
|
|
47
78
|
[handleSelect, interactionPolicy, testID, value],
|
|
48
79
|
);
|
|
49
80
|
|
|
50
|
-
|
|
51
|
-
if (
|
|
52
|
-
|
|
81
|
+
React.useEffect(() => {
|
|
82
|
+
if (!open || !ownsSheet.current) return;
|
|
83
|
+
ownsSheet.current = true;
|
|
84
|
+
const requestId = sheetRequestId.current + 1;
|
|
85
|
+
sheetRequestId.current = requestId;
|
|
53
86
|
present({
|
|
54
87
|
contentMode: 'direct',
|
|
55
88
|
maxDynamicContentSize: SELECT_SHEET_MAX_HEIGHT,
|
|
56
89
|
content: (
|
|
57
|
-
<
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
90
|
+
<SelectSheetContent
|
|
91
|
+
key={sheetId}
|
|
92
|
+
onClaim={claimSheet}
|
|
93
|
+
listData={listData}
|
|
94
|
+
isCurrentRequest={isCurrentSheetRequest}
|
|
95
|
+
onRelease={releaseSheet}
|
|
96
|
+
onReleaseOwnership={releaseSheetOwnership}
|
|
97
|
+
props={props}
|
|
98
|
+
renderOption={renderOption}
|
|
99
|
+
requestId={requestId}
|
|
100
|
+
search={search}
|
|
101
|
+
searchValue={searchValue}
|
|
62
102
|
/>
|
|
63
103
|
),
|
|
104
|
+
onDismiss: releaseSheet,
|
|
64
105
|
});
|
|
65
|
-
}, [
|
|
106
|
+
}, [
|
|
107
|
+
claimSheet,
|
|
108
|
+
isCurrentSheetRequest,
|
|
109
|
+
listData,
|
|
110
|
+
open,
|
|
111
|
+
present,
|
|
112
|
+
props,
|
|
113
|
+
releaseSheet,
|
|
114
|
+
releaseSheetOwnership,
|
|
115
|
+
renderOption,
|
|
116
|
+
search,
|
|
117
|
+
searchValue,
|
|
118
|
+
sheetId,
|
|
119
|
+
]);
|
|
120
|
+
|
|
121
|
+
React.useEffect(
|
|
122
|
+
() => () => {
|
|
123
|
+
if (ownsSheet.current) dismiss();
|
|
124
|
+
},
|
|
125
|
+
[dismiss],
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const openSelect = React.useCallback(() => {
|
|
129
|
+
if (disabled || readOnly || passive) return;
|
|
130
|
+
|
|
131
|
+
ownsSheet.current = true;
|
|
132
|
+
setOpen(true);
|
|
133
|
+
}, [disabled, passive, readOnly]);
|
|
66
134
|
|
|
67
135
|
return (
|
|
68
136
|
<SelectField props={props}>
|
|
@@ -76,6 +144,60 @@ function SelectInner<TValue extends string = string>({
|
|
|
76
144
|
);
|
|
77
145
|
}
|
|
78
146
|
|
|
147
|
+
/*** Renders and releases the Select-owned sheet content when another request replaces it. */
|
|
148
|
+
function SelectSheetContent<TValue extends string>({
|
|
149
|
+
isCurrentRequest,
|
|
150
|
+
listData,
|
|
151
|
+
onClaim,
|
|
152
|
+
onRelease,
|
|
153
|
+
onReleaseOwnership,
|
|
154
|
+
props,
|
|
155
|
+
renderOption,
|
|
156
|
+
requestId,
|
|
157
|
+
search,
|
|
158
|
+
searchValue,
|
|
159
|
+
}: SelectSheetContentProps<TValue>) {
|
|
160
|
+
const mounted = React.useRef(false);
|
|
161
|
+
|
|
162
|
+
React.useEffect(() => {
|
|
163
|
+
mounted.current = true;
|
|
164
|
+
onClaim(requestId);
|
|
165
|
+
|
|
166
|
+
return () => {
|
|
167
|
+
mounted.current = false;
|
|
168
|
+
onReleaseOwnership(requestId);
|
|
169
|
+
void Promise.resolve().then(() => {
|
|
170
|
+
if (!mounted.current && isCurrentRequest(requestId)) onRelease();
|
|
171
|
+
});
|
|
172
|
+
};
|
|
173
|
+
}, [isCurrentRequest, onClaim, onRelease, onReleaseOwnership, requestId]);
|
|
174
|
+
|
|
175
|
+
return (
|
|
176
|
+
<BottomSheetFlatList
|
|
177
|
+
contentContainerStyle={styles.listContent}
|
|
178
|
+
data={listData}
|
|
179
|
+
keyExtractor={(item: SelectOption<TValue>) => item.value}
|
|
180
|
+
ListHeaderComponent={
|
|
181
|
+
<SelectSearchInput onSearchChange={search} props={props} searchValue={searchValue} />
|
|
182
|
+
}
|
|
183
|
+
renderItem={renderOption}
|
|
184
|
+
/>
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
interface SelectSheetContentProps<TValue extends string> {
|
|
189
|
+
readonly isCurrentRequest: (requestId: number) => boolean;
|
|
190
|
+
readonly listData: readonly SelectOption<TValue>[];
|
|
191
|
+
readonly onClaim: (requestId: number) => void;
|
|
192
|
+
readonly onRelease: () => void;
|
|
193
|
+
readonly onReleaseOwnership: (requestId: number) => void;
|
|
194
|
+
readonly props: SelectProps<TValue>;
|
|
195
|
+
readonly renderOption: (info: ListRenderItemInfo<SelectOption<TValue>>) => React.ReactElement;
|
|
196
|
+
readonly requestId: number;
|
|
197
|
+
readonly search: (value: string) => void;
|
|
198
|
+
readonly searchValue: string;
|
|
199
|
+
}
|
|
200
|
+
|
|
79
201
|
const styles = StyleSheet.create({
|
|
80
202
|
listContent: {
|
|
81
203
|
padding: 16,
|
|
@@ -7,6 +7,7 @@ import { Surface } from '../../../../surface/public';
|
|
|
7
7
|
import { withZoraThemeScope } from '../../../../theme/adapters/inbound/withZoraThemeScope';
|
|
8
8
|
import { SelectField } from '../../composition/SelectField';
|
|
9
9
|
import { SelectOptionRow } from '../../composition/SelectOptionRow';
|
|
10
|
+
import { SelectSearchInput } from '../../composition/SelectSearchInput';
|
|
10
11
|
import { SelectTrigger } from '../../composition/SelectTrigger';
|
|
11
12
|
import { useSelectController } from '../../composition/useSelectController';
|
|
12
13
|
|
|
@@ -20,8 +21,9 @@ function SelectInner<TValue extends string = string>({
|
|
|
20
21
|
...props
|
|
21
22
|
}: SelectProps<TValue>) {
|
|
22
23
|
const [open, setOpen] = React.useState(false);
|
|
23
|
-
const { select, selectedOption, value } =
|
|
24
|
-
|
|
24
|
+
const { search, searchValue, select, selectedOption, value, visibleOptions } =
|
|
25
|
+
useSelectController(props);
|
|
26
|
+
const { interactionPolicy, testID } = props;
|
|
25
27
|
|
|
26
28
|
const selectAndClose = React.useCallback(
|
|
27
29
|
(nextValue: TValue) => {
|
|
@@ -50,7 +52,8 @@ function SelectInner<TValue extends string = string>({
|
|
|
50
52
|
>
|
|
51
53
|
<Surface variant="raised">
|
|
52
54
|
<View gap="xs" p="xs">
|
|
53
|
-
{
|
|
55
|
+
<SelectSearchInput onSearchChange={search} props={props} searchValue={searchValue} />
|
|
56
|
+
{visibleOptions.map((option) => (
|
|
54
57
|
<SelectOptionRow
|
|
55
58
|
interactionPolicy={interactionPolicy}
|
|
56
59
|
key={option.value}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { SelectProps } from '../../../../types/select';
|
|
2
|
+
import { View } from '../../../layout/public';
|
|
3
|
+
import { SearchInput } from '../../search-input/public';
|
|
4
|
+
|
|
5
|
+
/*** Renders the optional controlled search input shared by Select presentations. */
|
|
6
|
+
export function SelectSearchInput<TValue extends string>({
|
|
7
|
+
onSearchChange,
|
|
8
|
+
props,
|
|
9
|
+
searchValue,
|
|
10
|
+
}: SelectSearchInputProps<TValue>) {
|
|
11
|
+
if (!props.searchable) return null;
|
|
12
|
+
|
|
13
|
+
return (
|
|
14
|
+
<View pb="xs">
|
|
15
|
+
<SearchInput
|
|
16
|
+
disabled={props.disabled}
|
|
17
|
+
interactionPolicy={props.interactionPolicy}
|
|
18
|
+
onValueChange={onSearchChange}
|
|
19
|
+
placeholder={props.searchPlaceholder ?? 'Search options'}
|
|
20
|
+
readOnly={props.readOnly}
|
|
21
|
+
size="m"
|
|
22
|
+
testID={props.testID ? `${props.testID}-search` : undefined}
|
|
23
|
+
value={searchValue}
|
|
24
|
+
/>
|
|
25
|
+
</View>
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface SelectSearchInputProps<TValue extends string> {
|
|
30
|
+
readonly onSearchChange: (value: string) => void;
|
|
31
|
+
readonly props: SelectProps<TValue>;
|
|
32
|
+
readonly searchValue: string;
|
|
33
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
import { getVisibleSelectOptions } from './getVisibleSelectOptions';
|
|
4
|
+
|
|
5
|
+
const options = [
|
|
6
|
+
{ value: 'atlas', label: 'Atlas' },
|
|
7
|
+
{ value: 'zora', label: 'ZORA' },
|
|
8
|
+
] as const;
|
|
9
|
+
|
|
10
|
+
describe('getVisibleSelectOptions', () => {
|
|
11
|
+
test('preserves the original options and identity when search is disabled or empty', () => {
|
|
12
|
+
expect(getVisibleSelectOptions(options, false, 'atlas')).toBe(options);
|
|
13
|
+
expect(getVisibleSelectOptions(options, true, ' ')).toBe(options);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test('filters searchable options case-insensitively without changing option identity', () => {
|
|
17
|
+
const visible = getVisibleSelectOptions(options, true, 'Zo');
|
|
18
|
+
|
|
19
|
+
expect(visible).toEqual([options[1]]);
|
|
20
|
+
expect(visible[0]).toBe(options[1]);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SelectOption } from '../../../../types/select';
|
|
2
|
+
|
|
3
|
+
/*** Preserves option identity while limiting a searchable Select to labels matching its query. */
|
|
4
|
+
export function getVisibleSelectOptions<TValue extends string>(
|
|
5
|
+
options: readonly SelectOption<TValue>[],
|
|
6
|
+
searchable: boolean | undefined,
|
|
7
|
+
searchValue: string,
|
|
8
|
+
): readonly SelectOption<TValue>[] {
|
|
9
|
+
const query = searchValue.trim().toLocaleLowerCase();
|
|
10
|
+
if (!searchable || query.length === 0) return options;
|
|
11
|
+
|
|
12
|
+
return options.filter((option) => option.label.toLocaleLowerCase().includes(query));
|
|
13
|
+
}
|
|
@@ -1,13 +1,28 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
|
|
3
3
|
import type { SelectProps } from '../../../../types/select';
|
|
4
|
+
import { getVisibleSelectOptions } from './getVisibleSelectOptions';
|
|
4
5
|
|
|
5
6
|
/*** Resolves controlled or uncontrolled Select value state and the selected option. */
|
|
6
7
|
export function useSelectController<TValue extends string>(props: SelectProps<TValue>) {
|
|
7
|
-
const {
|
|
8
|
+
const {
|
|
9
|
+
defaultSearchValue,
|
|
10
|
+
defaultValue,
|
|
11
|
+
onSearchChange,
|
|
12
|
+
onValueChange,
|
|
13
|
+
options,
|
|
14
|
+
searchValue: controlledSearchValue,
|
|
15
|
+
value: controlledValue,
|
|
16
|
+
} = props;
|
|
8
17
|
const [localValue, setLocalValue] = React.useState<TValue | undefined>(defaultValue);
|
|
18
|
+
const [localSearchValue, setLocalSearchValue] = React.useState(defaultSearchValue ?? '');
|
|
9
19
|
const value = controlledValue ?? localValue;
|
|
20
|
+
const searchValue = controlledSearchValue ?? localSearchValue;
|
|
10
21
|
const selectedOption = options.find((option) => option.value === value);
|
|
22
|
+
const visibleOptions = React.useMemo(
|
|
23
|
+
() => getVisibleSelectOptions(options, props.searchable, searchValue),
|
|
24
|
+
[options, props.searchable, searchValue],
|
|
25
|
+
);
|
|
11
26
|
|
|
12
27
|
const select = React.useCallback(
|
|
13
28
|
(nextValue: TValue) => {
|
|
@@ -17,5 +32,13 @@ export function useSelectController<TValue extends string>(props: SelectProps<TV
|
|
|
17
32
|
[controlledValue, onValueChange],
|
|
18
33
|
);
|
|
19
34
|
|
|
20
|
-
|
|
35
|
+
const search = React.useCallback(
|
|
36
|
+
(nextValue: string) => {
|
|
37
|
+
if (controlledSearchValue === undefined) setLocalSearchValue(nextValue);
|
|
38
|
+
onSearchChange?.(nextValue);
|
|
39
|
+
},
|
|
40
|
+
[controlledSearchValue, onSearchChange],
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
return { search, searchValue, select, selectedOption, value, visibleOptions } as const;
|
|
21
44
|
}
|