-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform-bible-react.tsx
More file actions
349 lines (334 loc) · 10.9 KB
/
Copy pathplatform-bible-react.tsx
File metadata and controls
349 lines (334 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* @file Jest mock for platform-bible-react. The real package ships ESM which Jest cannot parse
* without extra transform configuration. This stub provides the subset used by the extension.
*/
import { forwardRef } from 'react';
import type { ReactElement, ReactNode } from 'react';
export interface MenuItemContainingCommand {
label: `%${string}%`;
command: `${string}.${string}`;
group: `${string}.${string}`;
order: number;
localizeNotes: string;
tooltip?: `%${string}%`;
searchTerms?: `%${string}%`;
iconPathBefore?: string;
iconPathAfter?: string;
}
export type SelectMenuItemHandler = (selectedMenuItem: MenuItemContainingCommand) => void;
interface SerializedVerseRef {
book: string;
chapterNum: number;
verseNum: number;
verse?: string;
versificationStr?: string;
}
/** Localization keys required by {@link BookChapterControl}. */
export const BOOK_CHAPTER_CONTROL_STRING_KEYS = [
'%scripture_section_ot_long%',
'%scripture_section_nt_long%',
'%scripture_section_dc_long%',
'%scripture_section_extra_long%',
'%history_recent%',
'%history_recentSearches_ariaLabel%',
] as const;
/** Sentinel menu item passed by the mock toolbar when the select-project menu button is clicked. */
export const MOCK_SELECT_PROJECT_MENU_ITEM: MenuItemContainingCommand = {
label: '%interlinearizer_menu_select_project%',
command: 'interlinearizer.openSelectProjectModal',
group: 'interlinearizer.project.actions',
order: 1,
localizeNotes: '',
};
/** Sentinel menu item passed by the mock toolbar when the new-project button is clicked. */
export const MOCK_NEW_PROJECT_MENU_ITEM: MenuItemContainingCommand = {
label: '%interlinearizer_menu_new_project%',
command: 'interlinearizer.openNewProjectModal',
group: 'interlinearizer.project.actions',
order: 2,
localizeNotes: '',
};
/** Sentinel menu item passed by the mock toolbar when the view-project-info button is clicked. */
export const MOCK_VIEW_PROJECT_INFO_MENU_ITEM: MenuItemContainingCommand = {
label: '%interlinearizer_menu_view_project_info%',
command: 'interlinearizer.openProjectInfoModal',
group: 'interlinearizer.project.actions',
order: 3,
localizeNotes: '',
};
/**
* Stub toolbar that renders project-menu and view-info buttons using sentinel menu items so tests
* can trigger menu commands without a real toolbar implementation.
*
* @param props - Component props.
* @param props.startAreaChildren - Content rendered in the start slot.
* @param props.endAreaChildren - Content rendered in the end slot.
* @param props.onSelectProjectMenuItem - Called with a sentinel item when a project-menu button is
* clicked (select-project, new-project, or view-project-info buttons).
* @param props.onSelectViewInfoMenuItem - Called with a generic sentinel item when the view-info
* button is clicked.
* @returns A div with `data-testid="tab-toolbar"` containing the rendered buttons.
*/
export function TabToolbar({
startAreaChildren,
endAreaChildren,
onSelectProjectMenuItem,
onSelectViewInfoMenuItem,
}: Readonly<{
className?: string;
startAreaChildren?: ReactNode;
centerAreaChildren?: ReactNode;
endAreaChildren?: ReactNode;
onSelectProjectMenuItem: SelectMenuItemHandler;
onSelectViewInfoMenuItem: SelectMenuItemHandler;
projectMenuData?: unknown;
tabViewMenuData?: unknown;
id?: string;
menuButtonIcon?: ReactNode;
}>): ReactElement {
return (
<div data-testid="tab-toolbar">
<div data-testid="tab-toolbar-start">{startAreaChildren}</div>
<div data-testid="tab-toolbar-end">{endAreaChildren}</div>
{onSelectProjectMenuItem && (
<button
type="button"
data-testid="tab-toolbar-project-menu"
onClick={() => onSelectProjectMenuItem(MOCK_SELECT_PROJECT_MENU_ITEM)}
>
Project menu
</button>
)}
{onSelectProjectMenuItem && (
<button
type="button"
data-testid="tab-toolbar-new-project"
onClick={() => onSelectProjectMenuItem(MOCK_NEW_PROJECT_MENU_ITEM)}
>
New project
</button>
)}
{onSelectProjectMenuItem && (
<button
type="button"
data-testid="tab-toolbar-view-project-info"
onClick={() => onSelectProjectMenuItem(MOCK_VIEW_PROJECT_INFO_MENU_ITEM)}
>
View project info
</button>
)}
{onSelectViewInfoMenuItem && (
<button
type="button"
data-testid="tab-toolbar-view-info-menu"
onClick={() =>
onSelectViewInfoMenuItem({
label: '%mock.viewInfo%',
command: 'mock.viewInfo',
group: 'mock.group',
order: 0,
localizeNotes: '',
})
}
>
View info menu
</button>
)}
</div>
);
}
/**
* Stub scroll-group selector rendered as a native `<select>` so tests can change the scroll group
* without the real component's styling or animation.
*
* @param props - Component props.
* @param props.availableScrollGroupIds - IDs to populate as `<option>` elements.
* @param props.scrollGroupId - The currently selected group ID.
* @param props.onChangeScrollGroupId - Called with the newly selected ID when the selection changes.
* @returns A `<select data-testid="scroll-group-selector">` element.
*/
export function ScrollGroupSelector({
availableScrollGroupIds,
scrollGroupId,
onChangeScrollGroupId,
}: Readonly<{
availableScrollGroupIds: (number | undefined)[];
scrollGroupId: number | undefined;
onChangeScrollGroupId: (id: number | undefined) => void;
localizedStrings?: Record<string, string>;
size?: 'default' | 'sm' | 'lg' | 'icon';
className?: string;
id?: string;
}>): ReactElement {
return (
<select
data-testid="scroll-group-selector"
value={scrollGroupId ?? ''}
onChange={(e) => onChangeScrollGroupId(e.target.value === '' ? undefined : Number(e.target.value))}
>
<option value="">—</option>
{availableScrollGroupIds?.map((id) => (
<option key={id ?? 'undefined'} value={id ?? ''}>
{id ?? '—'}
</option>
))}
</select>
);
}
/**
* Stub button that passes through `children`, `onClick`, `type`, `className`, `disabled`,
* `aria-label`, `aria-expanded`, `aria-haspopup`, `data-testid`, and `ref` to a native `<button>`
* element; `variant` and `size` are accepted but ignored.
*
* @param props - Component props.
* @param props.children - Button content.
* @param props.onClick - Click handler.
* @param props.type - HTML button type attribute.
* @param props.className - CSS class names.
* @param props.disabled - Whether the button is disabled.
* @param props.variant - Ignored styling variant.
* @param props.size - Ignored size variant.
* @param props['aria-label'] - Accessible label.
* @param props['aria-expanded'] - Expanded state for popup triggers.
* @param props['aria-haspopup'] - Haspopup attribute.
* @param props['data-testid'] - Test identifier.
* @param ref - Forwarded ref to the underlying button element.
* @returns A native `<button>` element with standard attributes forwarded.
*/
export const Button = forwardRef<
HTMLButtonElement,
Readonly<{
children?: ReactNode;
onClick?: () => void;
type?: 'button' | 'submit' | 'reset';
className?: string;
disabled?: boolean;
variant?: 'default' | 'secondary' | 'destructive' | 'ghost' | 'outline' | 'link';
size?: 'default' | 'sm' | 'lg' | 'icon';
'aria-label'?: string;
'aria-expanded'?: boolean;
'aria-haspopup'?: boolean | 'true' | 'false' | 'menu' | 'listbox' | 'tree' | 'grid' | 'dialog';
'data-testid'?: string;
}>
>(function ButtonImpl(
{
children,
onClick,
type,
className,
disabled,
variant: _variant,
size: _size,
'aria-label': ariaLabel,
'aria-expanded': ariaExpanded,
'aria-haspopup': ariaHaspopup,
'data-testid': testId,
},
ref,
) {
return (
<button
ref={ref}
type={type ?? 'button'}
onClick={onClick}
className={className}
aria-label={ariaLabel}
aria-expanded={ariaExpanded}
aria-haspopup={ariaHaspopup}
data-testid={testId}
disabled={disabled}
>
{children}
</button>
);
});
/**
* Stub book/chapter control that displays the current reference as text and exposes a single
* "Submit reference" button so tests can simulate reference changes without the real picker UI.
*
* @param props - Component props.
* @param props.scrRef - The currently displayed scripture reference.
* @param props.handleSubmit - Called with `scrRef` when the submit button is clicked.
* @param props.onAddRecentSearch - Called with `scrRef` after `handleSubmit` when provided.
* @returns A `<div data-testid="book-chapter-control">` with a submit button.
*/
export function BookChapterControl({
scrRef,
handleSubmit,
onAddRecentSearch,
}: Readonly<{
scrRef: SerializedVerseRef;
handleSubmit: (ref: SerializedVerseRef) => void;
className?: string;
getActiveBookIds?: () => string[];
localizedBookNames?: Map<string, { localizedId: string; localizedName: string }>;
localizedStrings?: Record<string, string>;
recentSearches?: SerializedVerseRef[];
onAddRecentSearch?: (scrRef: SerializedVerseRef) => void;
id?: string;
}>): ReactElement {
return (
<div data-testid="book-chapter-control">
{scrRef.book} {scrRef.chapterNum}:{scrRef.verseNum}
<button type="button" onClick={() => { handleSubmit(scrRef); onAddRecentSearch?.(scrRef); }}>
Submit reference
</button>
</div>
);
}
/**
* Stub toggle switch rendered as a native checkbox so tests can read and change the checked state
* without the real Radix UI implementation.
*
* @param props - Component props.
* @param props.checked - Whether the switch is on.
* @param props.disabled - Whether the switch is disabled.
* @param props.id - HTML `id` attribute forwarded to the input.
* @param props.onCheckedChange - Called with the new boolean state on change.
* @returns A native `<input type="checkbox">` element.
*/
export function Switch({
checked,
disabled,
id,
onCheckedChange,
}: Readonly<{
checked?: boolean;
disabled?: boolean;
id?: string;
onCheckedChange?: (checked: boolean) => void;
}>): ReactElement {
return (
<input
checked={checked ?? false}
disabled={disabled}
id={id}
onChange={(e) => onCheckedChange?.(e.target.checked)}
type="checkbox"
/>
);
}
/**
* Stub label rendered as a native `<label>` element.
*
* @param props - Component props.
* @param props.children - Label content.
* @param props.className - CSS class names.
* @param props.htmlFor - ID of the associated form control.
* @returns A native `<label>` element.
*/
export function Label({
children,
className,
htmlFor,
}: Readonly<{
children?: ReactNode;
className?: string;
htmlFor?: string;
}>): ReactElement {
return (
<label className={className} htmlFor={htmlFor}>
{children}
</label>
);
}