@sybilion/uilib 1.4.21 → 1.4.22

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.
Files changed (65) hide show
  1. package/assets/globals.css +3 -0
  2. package/dist/agent-glossary/workspace.generated.ts +2 -0
  3. package/dist/agent-glossary/workspace.index.md +2 -0
  4. package/dist/agent-glossary/workspace.md +2 -0
  5. package/dist/esm/components/ui/Checkbox/Checkbox.js +2 -2
  6. package/dist/esm/components/ui/Checkbox/Checkbox.styl.js +2 -2
  7. package/dist/esm/components/ui/DatePicker/Calendar.js +72 -0
  8. package/dist/esm/components/ui/DatePicker/DatePicker.js +81 -0
  9. package/dist/esm/components/ui/DatePicker/DatePicker.styl.js +7 -0
  10. package/dist/esm/components/ui/DatePicker/DatePickerInput.js +70 -0
  11. package/dist/esm/components/ui/DatePicker/DateRangePickerInput.js +130 -0
  12. package/dist/esm/components/ui/DatePicker/dateUtils.js +137 -0
  13. package/dist/esm/components/ui/RadioGroup/RadioGroup.helpers.js +6 -0
  14. package/dist/esm/components/ui/RadioGroup/RadioGroup.js +18 -0
  15. package/dist/esm/components/ui/RadioGroup/RadioGroup.styl.js +7 -0
  16. package/dist/esm/index.js +5 -0
  17. package/dist/esm/types/src/components/ui/Checkbox/Checkbox.d.ts +1 -1
  18. package/dist/esm/types/src/components/ui/Checkbox/Checkbox.types.d.ts +2 -0
  19. package/dist/esm/types/src/components/ui/Checkbox/index.d.ts +1 -1
  20. package/dist/esm/types/src/components/ui/DatePicker/Calendar.d.ts +2 -0
  21. package/dist/esm/types/src/components/ui/DatePicker/DatePicker.d.ts +2 -0
  22. package/dist/esm/types/src/components/ui/DatePicker/DatePicker.types.d.ts +60 -0
  23. package/dist/esm/types/src/components/ui/DatePicker/DatePickerInput.d.ts +2 -0
  24. package/dist/esm/types/src/components/ui/DatePicker/DateRangePickerInput.d.ts +2 -0
  25. package/dist/esm/types/src/components/ui/DatePicker/dateUtils.d.ts +30 -0
  26. package/dist/esm/types/src/components/ui/DatePicker/index.d.ts +6 -0
  27. package/dist/esm/types/src/components/ui/RadioGroup/RadioGroup.d.ts +4 -0
  28. package/dist/esm/types/src/components/ui/RadioGroup/RadioGroup.example.d.ts +1 -0
  29. package/dist/esm/types/src/components/ui/RadioGroup/RadioGroup.helpers.d.ts +3 -0
  30. package/dist/esm/types/src/components/ui/RadioGroup/RadioGroup.types.d.ts +11 -0
  31. package/dist/esm/types/src/components/ui/RadioGroup/index.d.ts +2 -0
  32. package/dist/esm/types/src/docs/pages/DatePickerPage.d.ts +1 -0
  33. package/dist/esm/types/src/docs/pages/RadioGroupPage.d.ts +1 -0
  34. package/dist/esm/types/src/index.d.ts +2 -0
  35. package/package.json +2 -1
  36. package/src/components/ui/Checkbox/AGENT.md +1 -0
  37. package/src/components/ui/Checkbox/Checkbox.styl +26 -2
  38. package/src/components/ui/Checkbox/Checkbox.styl.d.ts +4 -0
  39. package/src/components/ui/Checkbox/Checkbox.tsx +3 -3
  40. package/src/components/ui/Checkbox/Checkbox.types.ts +5 -1
  41. package/src/components/ui/Checkbox/index.ts +1 -1
  42. package/src/components/ui/DatePicker/AGENT.md +45 -0
  43. package/src/components/ui/DatePicker/Calendar.tsx +160 -0
  44. package/src/components/ui/DatePicker/DatePicker.styl +219 -0
  45. package/src/components/ui/DatePicker/DatePicker.styl.d.ts +39 -0
  46. package/src/components/ui/DatePicker/DatePicker.tsx +160 -0
  47. package/src/components/ui/DatePicker/DatePicker.types.ts +91 -0
  48. package/src/components/ui/DatePicker/DatePickerInput.tsx +169 -0
  49. package/src/components/ui/DatePicker/DateRangePickerInput.tsx +276 -0
  50. package/src/components/ui/DatePicker/dateUtils.ts +199 -0
  51. package/src/components/ui/DatePicker/index.ts +13 -0
  52. package/src/components/ui/RadioGroup/AGENT.md +24 -0
  53. package/src/components/ui/RadioGroup/RadioGroup.example.tsx +136 -0
  54. package/src/components/ui/RadioGroup/RadioGroup.helpers.ts +11 -0
  55. package/src/components/ui/RadioGroup/RadioGroup.styl +70 -0
  56. package/src/components/ui/RadioGroup/RadioGroup.styl.d.ts +13 -0
  57. package/src/components/ui/RadioGroup/RadioGroup.tsx +58 -0
  58. package/src/components/ui/RadioGroup/RadioGroup.types.ts +19 -0
  59. package/src/components/ui/RadioGroup/index.ts +6 -0
  60. package/src/docs/pages/CheckboxPage.tsx +129 -18
  61. package/src/docs/pages/DatePickerPage.tsx +183 -0
  62. package/src/docs/pages/FormsOverviewPage.tsx +131 -0
  63. package/src/docs/pages/RadioGroupPage.tsx +21 -0
  64. package/src/docs/registry.ts +12 -0
  65. package/src/index.ts +2 -0
@@ -0,0 +1,276 @@
1
+ import cn from 'classnames';
2
+ import { useCallback, useId, useState } from 'react';
3
+
4
+ import { CalendarBlank } from '@phosphor-icons/react';
5
+
6
+ import { Button } from '../Button';
7
+ import {
8
+ DropdownMenu,
9
+ DropdownMenuContent,
10
+ DropdownMenuTrigger,
11
+ } from '../DropdownMenu';
12
+ import { Input } from '../Input';
13
+ import { DatePicker } from './DatePicker';
14
+ import S from './DatePicker.styl';
15
+ import type { DateRangePickerInputProps } from './DatePicker.types';
16
+ import {
17
+ type DateRangeValue,
18
+ formatDateString,
19
+ formatDisplayDateRange,
20
+ normalizeDateRange,
21
+ parseDateString,
22
+ } from './dateUtils';
23
+
24
+ function commitRangeField(
25
+ current: DateRangeValue | null | undefined,
26
+ field: 'start' | 'end',
27
+ nextValue: string | undefined,
28
+ ): DateRangeValue | null {
29
+ const next = {
30
+ start: current?.start ?? '',
31
+ end: current?.end ?? '',
32
+ [field]: nextValue ?? '',
33
+ };
34
+
35
+ if (!next.start && !next.end) {
36
+ return null;
37
+ }
38
+ if (next.start && next.end) {
39
+ return normalizeDateRange(next.start, next.end);
40
+ }
41
+ return next;
42
+ }
43
+
44
+ export function DateRangePickerInput({
45
+ className,
46
+ value: controlledValue,
47
+ defaultValue,
48
+ onChange,
49
+ size = 'md',
50
+ startPlaceholder = 'Start date',
51
+ endPlaceholder = 'End date',
52
+ disabled,
53
+ minDate,
54
+ maxDate,
55
+ disabledDate,
56
+ calendarProps,
57
+ open: controlledOpen,
58
+ onOpenChange,
59
+ id: idProp,
60
+ variant = 'default',
61
+ ...inputProps
62
+ }: DateRangePickerInputProps) {
63
+ const generatedId = useId();
64
+ const startId = idProp ?? `${generatedId}-start`;
65
+ const endId = `${startId}-end`;
66
+ const [uncontrolledValue, setUncontrolledValue] = useState(defaultValue);
67
+ const [uncontrolledOpen, setUncontrolledOpen] = useState(false);
68
+ const [draftStart, setDraftStart] = useState<string | undefined>();
69
+ const [draftEnd, setDraftEnd] = useState<string | undefined>();
70
+ const [focusedField, setFocusedField] = useState<'start' | 'end' | null>(
71
+ null,
72
+ );
73
+
74
+ const value = controlledValue ?? uncontrolledValue;
75
+ const open = controlledOpen ?? uncontrolledOpen;
76
+
77
+ // Calendar picks end once start is set; otherwise start (Carbon-style).
78
+ const calendarActiveField: 'start' | 'end' =
79
+ value?.start && !value?.end ? 'end' : 'start';
80
+ const activeField = focusedField ?? (open ? calendarActiveField : null);
81
+
82
+ const setOpen = useCallback(
83
+ (nextOpen: boolean) => {
84
+ if (controlledOpen === undefined) {
85
+ setUncontrolledOpen(nextOpen);
86
+ }
87
+ onOpenChange?.(nextOpen);
88
+ },
89
+ [controlledOpen, onOpenChange],
90
+ );
91
+
92
+ const commitValue = useCallback(
93
+ (nextValue: DateRangeValue | null) => {
94
+ if (controlledValue === undefined) {
95
+ setUncontrolledValue(nextValue);
96
+ }
97
+ onChange?.(nextValue);
98
+ },
99
+ [controlledValue, onChange],
100
+ );
101
+
102
+ const handleStartChange = useCallback(
103
+ (event: React.ChangeEvent<HTMLInputElement>) => {
104
+ const nextValue = event.target.value;
105
+ setDraftStart(nextValue);
106
+ if (!nextValue) {
107
+ commitValue(commitRangeField(value, 'start', undefined));
108
+ return;
109
+ }
110
+ if (parseDateString(nextValue)) {
111
+ commitValue(commitRangeField(value, 'start', nextValue));
112
+ }
113
+ },
114
+ [commitValue, value],
115
+ );
116
+
117
+ const handleEndChange = useCallback(
118
+ (event: React.ChangeEvent<HTMLInputElement>) => {
119
+ const nextValue = event.target.value;
120
+ setDraftEnd(nextValue);
121
+ if (!nextValue) {
122
+ commitValue(commitRangeField(value, 'end', undefined));
123
+ return;
124
+ }
125
+ if (parseDateString(nextValue)) {
126
+ commitValue(commitRangeField(value, 'end', nextValue));
127
+ }
128
+ },
129
+ [commitValue, value],
130
+ );
131
+
132
+ const handleStartBlur = useCallback(() => {
133
+ if (draftStart === undefined) {
134
+ return;
135
+ }
136
+ const parsed = parseDateString(draftStart);
137
+ if (parsed) {
138
+ commitValue(commitRangeField(value, 'start', formatDateString(parsed)));
139
+ } else if (!draftStart) {
140
+ commitValue(commitRangeField(value, 'start', undefined));
141
+ }
142
+ setDraftStart(undefined);
143
+ }, [commitValue, draftStart, value]);
144
+
145
+ const handleEndBlur = useCallback(() => {
146
+ if (draftEnd === undefined) {
147
+ return;
148
+ }
149
+ const parsed = parseDateString(draftEnd);
150
+ if (parsed) {
151
+ commitValue(commitRangeField(value, 'end', formatDateString(parsed)));
152
+ } else if (!draftEnd) {
153
+ commitValue(commitRangeField(value, 'end', undefined));
154
+ }
155
+ setDraftEnd(undefined);
156
+ }, [commitValue, draftEnd, value]);
157
+
158
+ const handleRangeSelect = useCallback(
159
+ (nextValue: DateRangeValue | null) => {
160
+ commitValue(nextValue);
161
+ if (nextValue?.start && nextValue?.end) {
162
+ setDraftStart(undefined);
163
+ setDraftEnd(undefined);
164
+ setOpen(false);
165
+ }
166
+ },
167
+ [commitValue, setOpen],
168
+ );
169
+
170
+ const startDisplay =
171
+ draftStart !== undefined ? draftStart : (value?.start ?? '');
172
+ const endDisplay = draftEnd !== undefined ? draftEnd : (value?.end ?? '');
173
+
174
+ return (
175
+ <div
176
+ className={cn(S.rangeInputRoot, S[`inputSize-${size}`], className)}
177
+ data-slot="date-range-picker-input"
178
+ >
179
+ <Input
180
+ {...inputProps}
181
+ type="text"
182
+ variant={variant}
183
+ id={startId}
184
+ size={size}
185
+ value={startDisplay}
186
+ placeholder={startPlaceholder}
187
+ disabled={disabled}
188
+ onChange={handleStartChange}
189
+ onBlur={() => {
190
+ handleStartBlur();
191
+ setFocusedField(current => (current === 'start' ? null : current));
192
+ }}
193
+ onFocus={() => {
194
+ setFocusedField('start');
195
+ setOpen(true);
196
+ }}
197
+ className={cn(
198
+ S.inputField,
199
+ S.rangeInputField,
200
+ activeField === 'start' && S.rangeInputFieldActive,
201
+ )}
202
+ autoComplete="off"
203
+ inputMode="numeric"
204
+ aria-label="Start date"
205
+ />
206
+ <span className={S.rangeSeparator} aria-hidden="true">
207
+ –
208
+ </span>
209
+ <Input
210
+ {...inputProps}
211
+ type="text"
212
+ variant={variant}
213
+ id={endId}
214
+ size={size}
215
+ value={endDisplay}
216
+ placeholder={endPlaceholder}
217
+ disabled={disabled}
218
+ onChange={handleEndChange}
219
+ onBlur={() => {
220
+ handleEndBlur();
221
+ setFocusedField(current => (current === 'end' ? null : current));
222
+ }}
223
+ onFocus={() => {
224
+ setFocusedField('end');
225
+ setOpen(true);
226
+ }}
227
+ className={cn(
228
+ S.inputField,
229
+ S.rangeInputField,
230
+ activeField === 'end' && S.rangeInputFieldActive,
231
+ )}
232
+ autoComplete="off"
233
+ inputMode="numeric"
234
+ aria-label="End date"
235
+ />
236
+ <DropdownMenu open={open} onOpenChange={setOpen} modal={false}>
237
+ <DropdownMenuTrigger asChild>
238
+ <Button
239
+ type="button"
240
+ variant="ghost"
241
+ size="sm"
242
+ icon
243
+ disabled={disabled}
244
+ className={S.rangeCalendarTrigger}
245
+ aria-label="Open calendar"
246
+ onClick={() => setOpen(true)}
247
+ >
248
+ <CalendarBlank size={16} />
249
+ </Button>
250
+ </DropdownMenuTrigger>
251
+ <DropdownMenuContent
252
+ align="end"
253
+ sideOffset={4}
254
+ className={cn(S.popupContent, size === 'sm' && S['popupContent-sm'])}
255
+ onCloseAutoFocus={event => event.preventDefault()}
256
+ >
257
+ <DatePicker
258
+ mode="range"
259
+ rangeValue={value}
260
+ onRangeChange={handleRangeSelect}
261
+ size={size}
262
+ calendarProps={{
263
+ minDate,
264
+ maxDate,
265
+ disabledDate,
266
+ ...calendarProps,
267
+ }}
268
+ />
269
+ </DropdownMenuContent>
270
+ </DropdownMenu>
271
+ <span className={S.srOnly} aria-live="polite">
272
+ {formatDisplayDateRange(value)}
273
+ </span>
274
+ </div>
275
+ );
276
+ }
@@ -0,0 +1,199 @@
1
+ export type DateParts = {
2
+ year: number;
3
+ month: number;
4
+ day: number;
5
+ };
6
+
7
+ export type MonthView = Pick<DateParts, 'year' | 'month'>;
8
+
9
+ const ISO_DATE_RE = /^(\d{4})-(\d{2})-(\d{2})$/;
10
+
11
+ export function parseDateString(
12
+ value: string | undefined | null,
13
+ ): DateParts | null {
14
+ if (!value) {
15
+ return null;
16
+ }
17
+ const match = ISO_DATE_RE.exec(value.trim());
18
+ if (!match) {
19
+ return null;
20
+ }
21
+ const year = Number(match[1]);
22
+ const month = Number(match[2]);
23
+ const day = Number(match[3]);
24
+ const date = new Date(Date.UTC(year, month - 1, day));
25
+ if (
26
+ date.getUTCFullYear() !== year ||
27
+ date.getUTCMonth() !== month - 1 ||
28
+ date.getUTCDate() !== day
29
+ ) {
30
+ return null;
31
+ }
32
+ return { year, month, day };
33
+ }
34
+
35
+ export function formatDateString(parts: DateParts): string {
36
+ const month = String(parts.month).padStart(2, '0');
37
+ const day = String(parts.day).padStart(2, '0');
38
+ return `${parts.year}-${month}-${day}`;
39
+ }
40
+
41
+ export function toUtcTimestamp(parts: DateParts): number {
42
+ return Date.UTC(parts.year, parts.month - 1, parts.day);
43
+ }
44
+
45
+ export function isSameDate(a: DateParts, b: DateParts): boolean {
46
+ return a.year === b.year && a.month === b.month && a.day === b.day;
47
+ }
48
+
49
+ export function isBeforeDate(a: DateParts, b: DateParts): boolean {
50
+ return toUtcTimestamp(a) < toUtcTimestamp(b);
51
+ }
52
+
53
+ export function isAfterDate(a: DateParts, b: DateParts): boolean {
54
+ return toUtcTimestamp(a) > toUtcTimestamp(b);
55
+ }
56
+
57
+ export function addMonths(parts: MonthView, delta: number): MonthView {
58
+ const date = new Date(Date.UTC(parts.year, parts.month - 1 + delta, 1));
59
+ return {
60
+ year: date.getUTCFullYear(),
61
+ month: date.getUTCMonth() + 1,
62
+ };
63
+ }
64
+
65
+ export function getTodayParts(): DateParts {
66
+ const now = new Date();
67
+ return {
68
+ year: now.getFullYear(),
69
+ month: now.getMonth() + 1,
70
+ day: now.getDate(),
71
+ };
72
+ }
73
+
74
+ export function getMonthMatrix(
75
+ year: number,
76
+ month: number,
77
+ startOfWeek: 0 | 1,
78
+ ): DateParts[] {
79
+ const firstOfMonth = new Date(Date.UTC(year, month - 1, 1));
80
+ const firstWeekday = firstOfMonth.getUTCDay();
81
+ const offset = (firstWeekday - startOfWeek + 7) % 7;
82
+ const start = new Date(Date.UTC(year, month - 1, 1 - offset));
83
+ const days: DateParts[] = [];
84
+
85
+ for (let i = 0; i < 42; i += 1) {
86
+ const date = new Date(start.getTime() + i * 86_400_000);
87
+ days.push({
88
+ year: date.getUTCFullYear(),
89
+ month: date.getUTCMonth() + 1,
90
+ day: date.getUTCDate(),
91
+ });
92
+ }
93
+
94
+ return days;
95
+ }
96
+
97
+ export function formatDisplayDate(
98
+ value: string | undefined,
99
+ locale = 'en-US',
100
+ ): string {
101
+ const parts = parseDateString(value);
102
+ if (!parts) {
103
+ return '';
104
+ }
105
+ return new Intl.DateTimeFormat(locale, {
106
+ year: 'numeric',
107
+ month: 'short',
108
+ day: 'numeric',
109
+ }).format(new Date(parts.year, parts.month - 1, parts.day));
110
+ }
111
+
112
+ export function formatMonthYear(
113
+ year: number,
114
+ month: number,
115
+ locale = 'en-US',
116
+ ): string {
117
+ return new Intl.DateTimeFormat(locale, {
118
+ month: 'long',
119
+ year: 'numeric',
120
+ }).format(new Date(year, month - 1, 1));
121
+ }
122
+
123
+ export function getWeekdayLabels(
124
+ startOfWeek: 0 | 1,
125
+ locale = 'en-US',
126
+ ): string[] {
127
+ const formatter = new Intl.DateTimeFormat(locale, { weekday: 'narrow' });
128
+ const baseSunday = new Date(2024, 0, 7);
129
+ const labels: string[] = [];
130
+ for (let i = 0; i < 7; i += 1) {
131
+ const weekday = (startOfWeek + i) % 7;
132
+ const date = new Date(baseSunday);
133
+ date.setDate(baseSunday.getDate() + weekday);
134
+ labels.push(formatter.format(date));
135
+ }
136
+ return labels;
137
+ }
138
+
139
+ export type DateRangeValue = {
140
+ start: string;
141
+ end: string;
142
+ };
143
+
144
+ export function normalizeDateRange(start: string, end: string): DateRangeValue {
145
+ const startParts = parseDateString(start);
146
+ const endParts = parseDateString(end);
147
+ if (!startParts || !endParts) {
148
+ return { start, end };
149
+ }
150
+ if (isAfterDate(startParts, endParts)) {
151
+ return { start: end, end: start };
152
+ }
153
+ return { start, end };
154
+ }
155
+
156
+ export function isBetweenDates(
157
+ parts: DateParts,
158
+ start: DateParts,
159
+ end: DateParts,
160
+ ): boolean {
161
+ const timestamp = toUtcTimestamp(parts);
162
+ return timestamp >= toUtcTimestamp(start) && timestamp <= toUtcTimestamp(end);
163
+ }
164
+
165
+ export function formatDisplayDateRange(
166
+ value: DateRangeValue | null | undefined,
167
+ locale = 'en-US',
168
+ ): string {
169
+ if (!value?.start || !value?.end) {
170
+ return '';
171
+ }
172
+ const start = formatDisplayDate(value.start, locale);
173
+ const end = formatDisplayDate(value.end, locale);
174
+ return `${start} – ${end}`;
175
+ }
176
+
177
+ export function isDateDisabled(
178
+ parts: DateParts,
179
+ options: {
180
+ minDate?: string;
181
+ maxDate?: string;
182
+ disabledDate?: (date: string) => boolean;
183
+ },
184
+ ): boolean {
185
+ const { minDate, maxDate, disabledDate } = options;
186
+ const min = parseDateString(minDate);
187
+ const max = parseDateString(maxDate);
188
+
189
+ if (min && isBeforeDate(parts, min)) {
190
+ return true;
191
+ }
192
+ if (max && isAfterDate(parts, max)) {
193
+ return true;
194
+ }
195
+ if (disabledDate?.(formatDateString(parts))) {
196
+ return true;
197
+ }
198
+ return false;
199
+ }
@@ -0,0 +1,13 @@
1
+ export { Calendar } from './Calendar';
2
+ export { DatePicker } from './DatePicker';
3
+ export { DatePickerInput } from './DatePickerInput';
4
+ export { DateRangePickerInput } from './DateRangePickerInput';
5
+ export type {
6
+ CalendarProps,
7
+ DatePickerInputProps,
8
+ DatePickerMode,
9
+ DatePickerProps,
10
+ DatePickerSize,
11
+ DateRangePickerInputProps,
12
+ } from './DatePicker.types';
13
+ export type { DateRangeValue } from './dateUtils';
@@ -0,0 +1,24 @@
1
+ RadioGroup is a single-choice control for selecting one option from a short list. Pair each RadioGroupItem with a Label linked via htmlFor so the text is clickable and announced by assistive technology.
2
+
3
+ Runnable examples: [RadioGroup.example.tsx](./RadioGroup.example.tsx).
4
+
5
+ ## When to use
6
+
7
+ - For mutually exclusive options in forms and settings.
8
+ - When every option should stay visible, typically two to five choices.
9
+
10
+ ## When not to use
11
+
12
+ - For independent on or off toggles; use Checkbox or Switch.
13
+ - For segmented toolbar-style toggles; use ToggleGroup.
14
+ - For more than roughly five options; use Select.
15
+
16
+ ## Do
17
+
18
+ - Give the group a visible label or an aria-label when no legend is present.
19
+ - Use vertical layout by default; switch to horizontal only when space is tight and labels are short.
20
+ - Set `size` on `RadioGroup` so all items inherit it; override per item only when needed. Sizes: `sm` (14px), `md` (16px, default), `lg` (20px).
21
+
22
+ ## Don't
23
+
24
+ - Use checkboxes for single-choice fields; only one option can be selected in a RadioGroup.
@@ -0,0 +1,136 @@
1
+ import { useState } from 'react';
2
+
3
+ import { Label } from '#uilib/components/ui/Label';
4
+ import { PageContentSection } from '#uilib/components/ui/Page';
5
+
6
+ import { RadioGroup, RadioGroupItem } from './RadioGroup';
7
+
8
+ const SIZES = ['sm', 'md', 'lg'] as const;
9
+
10
+ const PLAN_OPTIONS = [
11
+ { value: 'starter', label: 'Starter' },
12
+ { value: 'pro', label: 'Pro' },
13
+ { value: 'enterprise', label: 'Enterprise' },
14
+ ] as const;
15
+
16
+ function OptionRow({
17
+ id,
18
+ value,
19
+ label,
20
+ }: {
21
+ id: string;
22
+ value: string;
23
+ label: string;
24
+ }) {
25
+ return (
26
+ <div style={{ display: 'flex', alignItems: 'center', gap: '0.5rem' }}>
27
+ <RadioGroupItem value={value} id={id} />
28
+ <Label htmlFor={id}>{label}</Label>
29
+ </div>
30
+ );
31
+ }
32
+
33
+ export function RadioGroupExample() {
34
+ const [plan, setPlan] = useState('pro');
35
+
36
+ return (
37
+ <PageContentSection
38
+ style={{ display: 'flex', flexDirection: 'column', gap: '2rem' }}
39
+ >
40
+ <section>
41
+ <h3 style={{ marginBottom: '1rem' }}>Sizes</h3>
42
+
43
+ {SIZES.map(s => (
44
+ <td
45
+ key={s}
46
+ style={{ padding: '0.1rem', display: 'flex', alignItems: 'center' }}
47
+ >
48
+ <RadioGroup
49
+ size={s}
50
+ id={`rg-size-${s}`}
51
+ aria-label={`Size ${s} row`}
52
+ defaultValue="starter"
53
+ >
54
+ <RadioGroupItem value="starter" aria-label={`${s}`} />
55
+ </RadioGroup>
56
+ &nbsp;–&nbsp;
57
+ <Label htmlFor={`rg-size-${s}`}>{s}</Label>
58
+ </td>
59
+ ))}
60
+ </section>
61
+
62
+ <section>
63
+ <h3 style={{ marginBottom: '1rem' }}>Vertical (default)</h3>
64
+ <RadioGroup defaultValue="starter" aria-label="Billing plan">
65
+ {PLAN_OPTIONS.map(option => (
66
+ <OptionRow
67
+ key={option.value}
68
+ id={`rg-vertical-${option.value}`}
69
+ value={option.value}
70
+ label={option.label}
71
+ />
72
+ ))}
73
+ </RadioGroup>
74
+ </section>
75
+
76
+ <section>
77
+ <h3 style={{ marginBottom: '1rem' }}>Horizontal</h3>
78
+ <RadioGroup
79
+ defaultValue="pro"
80
+ orientation="horizontal"
81
+ aria-label="Billing plan horizontal"
82
+ >
83
+ {PLAN_OPTIONS.map(option => (
84
+ <OptionRow
85
+ key={option.value}
86
+ id={`rg-horizontal-${option.value}`}
87
+ value={option.value}
88
+ label={option.label}
89
+ />
90
+ ))}
91
+ </RadioGroup>
92
+ </section>
93
+
94
+ <section>
95
+ <h3 style={{ marginBottom: '1rem' }}>Controlled</h3>
96
+ <p
97
+ style={{
98
+ margin: '0 0 1rem',
99
+ color: 'var(--muted-foreground)',
100
+ fontSize: 'var(--text-sm)',
101
+ }}
102
+ >
103
+ Selected plan: {plan}
104
+ </p>
105
+ <RadioGroup
106
+ value={plan}
107
+ onValueChange={setPlan}
108
+ aria-label="Billing plan controlled"
109
+ >
110
+ {PLAN_OPTIONS.map(option => (
111
+ <OptionRow
112
+ key={option.value}
113
+ id={`rg-controlled-${option.value}`}
114
+ value={option.value}
115
+ label={option.label}
116
+ />
117
+ ))}
118
+ </RadioGroup>
119
+ </section>
120
+
121
+ <section>
122
+ <h3 style={{ marginBottom: '1rem' }}>Disabled</h3>
123
+ <RadioGroup defaultValue="starter" disabled aria-label="Disabled plan">
124
+ {PLAN_OPTIONS.map(option => (
125
+ <OptionRow
126
+ key={option.value}
127
+ id={`rg-disabled-${option.value}`}
128
+ value={option.value}
129
+ label={option.label}
130
+ />
131
+ ))}
132
+ </RadioGroup>
133
+ </section>
134
+ </PageContentSection>
135
+ );
136
+ }
@@ -0,0 +1,11 @@
1
+ import { RadioGroupContextValue, RadioGroupSize } from './RadioGroup.types';
2
+
3
+ export const getRadioGroupContextValues = (
4
+ context: RadioGroupContextValue,
5
+ itemSize?: RadioGroupSize,
6
+ ): RadioGroupSize => context.size || itemSize || 'md';
7
+
8
+ export const getDefaultRadioGroupContextValues =
9
+ (): RadioGroupContextValue => ({
10
+ size: 'md',
11
+ });