@torpor/ui 1.3.5 → 1.4.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.
Files changed (129) hide show
  1. package/dist/Accordion/AccordionItem.torp +1 -1
  2. package/dist/Alert/Alert.torp +41 -0
  3. package/dist/Alert/index.d.ts +37 -0
  4. package/dist/Alert/index.js +1 -0
  5. package/dist/Charts/ChartScatter.torp +167 -0
  6. package/dist/Charts/ScatterChart.torp +49 -178
  7. package/dist/Charts/index.d.ts +38 -78
  8. package/dist/Charts/index.js +2 -4
  9. package/dist/ColorPicker/ColorPalette.torp +107 -29
  10. package/dist/ColorPicker/ColorPicker.torp +121 -10
  11. package/dist/ColorPicker/ColorPickerAlpha.torp +58 -0
  12. package/dist/ColorPicker/ColorPickerArea.torp +175 -0
  13. package/dist/ColorPicker/ColorPickerEyedropper.torp +77 -0
  14. package/dist/ColorPicker/ColorPickerHue.torp +59 -0
  15. package/dist/ColorPicker/ColorPickerInput.torp +7 -18
  16. package/dist/ColorPicker/ColorPickerTypes.ts +11 -1
  17. package/dist/ColorPicker/index.d.ts +139 -14
  18. package/dist/ColorPicker/index.js +5 -1
  19. package/dist/ComboBox/ComboBox.torp +40 -5
  20. package/dist/ComboBox/index.d.ts +5 -2
  21. package/dist/ComboBox/index.js +1 -2
  22. package/dist/Contextual/Contextual.torp +25 -77
  23. package/dist/Contextual/ContextualContent.torp +10 -51
  24. package/dist/Contextual/ContextualHover.torp +13 -85
  25. package/dist/Contextual/ContextualTrigger.torp +9 -16
  26. package/dist/Contextual/ContextualTypes.ts +6 -12
  27. package/dist/Contextual/index.d.ts +10 -13
  28. package/dist/DataGrid/DataGrid.torp +21 -3
  29. package/dist/DataGrid/index.d.ts +1 -1
  30. package/dist/DatePicker/DatePicker.torp +6 -4
  31. package/dist/DateRangePicker/DateRangePicker.torp +14 -13
  32. package/dist/DateTimePicker/DateTimePicker.torp +264 -0
  33. package/dist/DateTimePicker/index.d.ts +46 -0
  34. package/dist/DateTimePicker/index.js +1 -0
  35. package/dist/Dialog/Dialog.torp +0 -1
  36. package/dist/Dialog/index.d.ts +0 -1
  37. package/dist/Drawer/Drawer.torp +5 -23
  38. package/dist/Drawer/DrawerContent.torp +1 -0
  39. package/dist/Drawer/DrawerOverlay.torp +9 -16
  40. package/dist/Drawer/DrawerTrigger.torp +9 -24
  41. package/dist/Drawer/DrawerTypes.ts +3 -1
  42. package/dist/Form/CheckBox.torp +10 -18
  43. package/dist/Form/File.torp +10 -18
  44. package/dist/Form/FileDrop.torp +16 -22
  45. package/dist/Form/Input.torp +14 -20
  46. package/dist/Form/Radio.torp +71 -0
  47. package/dist/Form/RadioGroup.torp +221 -0
  48. package/dist/Form/RadioGroupTypes.ts +40 -0
  49. package/dist/Form/Select.torp +10 -18
  50. package/dist/Form/{Toggle.torp → Switch.torp} +17 -21
  51. package/dist/Form/TextArea.torp +10 -18
  52. package/dist/Form/ToggleButton.torp +94 -0
  53. package/dist/Form/ToggleGroup.torp +150 -0
  54. package/dist/Form/ToggleGroupTypes.ts +23 -0
  55. package/dist/Form/index.d.ts +173 -15
  56. package/dist/Form/index.js +6 -2
  57. package/dist/ListBox/ListBox.torp +27 -5
  58. package/dist/ListBox/ListBoxItem.torp +1 -1
  59. package/dist/ListBox/index.d.ts +6 -2
  60. package/dist/MaskedInput/MaskedInput.torp +1 -1
  61. package/dist/Menu/MenuPopoutTrigger.torp +1 -1
  62. package/dist/MenuBar/MenuBar.torp +5 -3
  63. package/dist/MenuBar/MenuBarItemTrigger.torp +2 -2
  64. package/dist/Meter/Meter.torp +126 -0
  65. package/dist/Meter/MeterIndicator.torp +41 -0
  66. package/dist/Meter/MeterTypes.ts +8 -0
  67. package/dist/Meter/index.d.ts +79 -0
  68. package/dist/Meter/index.js +3 -0
  69. package/dist/Modal/Modal.torp +4 -22
  70. package/dist/Modal/ModalOverlay.torp +9 -16
  71. package/dist/NavMenu/NavMenu.torp +5 -3
  72. package/dist/NumberInput/NumberInput.torp +1 -1
  73. package/dist/PinInput/PinInput.torp +220 -0
  74. package/dist/PinInput/index.d.ts +48 -0
  75. package/dist/PinInput/index.js +1 -0
  76. package/dist/Popover/Popover.torp +18 -24
  77. package/dist/Popover/PopoverContent.torp +8 -0
  78. package/dist/Popover/PopoverHover.torp +30 -3
  79. package/dist/Popover/PopoverTypes.ts +2 -0
  80. package/dist/Popover/index.d.ts +8 -1
  81. package/dist/Rating/Rating.torp +1 -1
  82. package/dist/SegmentedControl/SegmentedControl.torp +3 -3
  83. package/dist/SegmentedControl/SegmentedItem.torp +1 -1
  84. package/dist/SelectBox/SelectBox.torp +40 -5
  85. package/dist/SelectBox/index.d.ts +5 -2
  86. package/dist/SelectBox/index.js +1 -2
  87. package/dist/Slider/Slider.torp +1 -1
  88. package/dist/SortableList/SortableList.torp +132 -0
  89. package/dist/SortableList/SortableListItem.torp +117 -0
  90. package/dist/SortableList/SortableListTypes.ts +16 -0
  91. package/dist/SortableList/index.d.ts +81 -0
  92. package/dist/SortableList/index.js +3 -0
  93. package/dist/TabGroup/TabContent.torp +1 -1
  94. package/dist/TabGroup/TabGroup.torp +2 -2
  95. package/dist/TabGroup/TabHeader.torp +1 -1
  96. package/dist/TagInput/TagInput.torp +1 -1
  97. package/dist/TimePicker/TimePicker.torp +1 -1
  98. package/dist/ToolBar/ToolBar.torp +8 -4
  99. package/dist/ToolBar/ToolBarPopoutTrigger.torp +1 -1
  100. package/dist/Tooltip/Tooltip.torp +104 -0
  101. package/dist/Tooltip/TooltipContent.torp +81 -0
  102. package/dist/Tooltip/TooltipTrigger.torp +79 -0
  103. package/dist/Tooltip/TooltipTypes.ts +28 -0
  104. package/dist/Tooltip/index.d.ts +104 -0
  105. package/dist/Tooltip/index.js +4 -0
  106. package/dist/Tree/Tree.torp +1 -1
  107. package/dist/Tree/TreeItem.torp +1 -1
  108. package/dist/VirtualList/VirtualList.torp +149 -0
  109. package/dist/VirtualList/index.d.ts +46 -0
  110. package/dist/VirtualList/index.js +1 -0
  111. package/dist/dnd/index.d.ts +2 -0
  112. package/dist/dnd/index.js +212 -0
  113. package/dist/dnd/index.js.map +1 -0
  114. package/dist/loader/index.d.ts +2 -0
  115. package/dist/{loader-DEeG5vFY.js → loader/index.js} +10 -3
  116. package/dist/loader/index.js.map +1 -0
  117. package/dist/utils/PopoutClickTrigger.torp +1 -0
  118. package/dist/utils/PopoutOverlay.torp +39 -0
  119. package/dist/utils/PopoutTypes.ts +4 -0
  120. package/dist/utils/SubmenuPopoutTrigger.torp +5 -5
  121. package/dist/utils/colorUtils.ts +260 -75
  122. package/dist/utils/formField.ts +4 -3
  123. package/dist/utils/handlePopoutButton.ts +43 -0
  124. package/dist/utils/loader.ts +20 -3
  125. package/package.json +41 -5
  126. package/dist/Charts/GridLines.torp +0 -65
  127. package/dist/Charts/XAxis.torp +0 -69
  128. package/dist/Charts/YAxis.torp +0 -49
  129. package/dist/loader-DEeG5vFY.js.map +0 -1
@@ -1,4 +1,5 @@
1
1
  import type { ClassValue, StyleValue } from "@torpor/view";
2
+ import createFormField from "../utils/formField";
2
3
  import getId from "../utils/getId";
3
4
  import {
4
5
  type FieldContext,
@@ -36,18 +37,19 @@ interface FileDropProps {
36
37
  */
37
38
  export default function FileDrop($props: FileDropProps | undefined) {
38
39
  let defaultId = getId();
39
- let inputId = `${$props?.id ?? defaultId}-input`;
40
+ function getInputId(): string {
41
+ return `${$props?.id ?? defaultId}-input`;
42
+ }
40
43
  let input: HTMLInputElement;
41
44
 
42
- let blurred = false;
43
-
44
45
  function getDisabled(): boolean {
45
46
  return $props?.disabled === true;
46
47
  }
47
48
 
48
49
  // Get stuff out of the FormContext and FieldContext
49
- const formContext = $context[FormContextName] as FormContext;
50
- const fieldContext = $context[FieldContextName] as FieldContext;
50
+ const formContext = $context[FormContextName] as FormContext | undefined;
51
+ const fieldContext = $context[FieldContextName] as FieldContext | undefined;
52
+ const form = createFormField(() => $props?.name, formContext, fieldContext);
51
53
 
52
54
  // This will change the form's enctype to `multipart/form-data`
53
55
  if (formContext) {
@@ -58,7 +60,7 @@ export default function FileDrop($props: FileDropProps | undefined) {
58
60
  let $state =
59
61
  fieldContext?.state ??
60
62
  $watch({
61
- inputId,
63
+ inputId: getInputId(),
62
64
  value: null,
63
65
  errors: {} as Record<string, string>,
64
66
  valid: false,
@@ -66,7 +68,7 @@ export default function FileDrop($props: FileDropProps | undefined) {
66
68
  });
67
69
 
68
70
  // Set the input ID now that we know it and it will get linked with the associated label
69
- $state.inputId = inputId;
71
+ $state.inputId = getInputId();
70
72
 
71
73
  function handleFiles(files: FileList | File[] | undefined | null) {
72
74
  if (getDisabled() || !files) return;
@@ -74,12 +76,7 @@ export default function FileDrop($props: FileDropProps | undefined) {
74
76
  if (all.length === 0) return;
75
77
  const selected = $props?.multiple ? all : all.slice(0, 1);
76
78
 
77
- if (formContext) {
78
- formContext.validate();
79
- }
80
- if (fieldContext && blurred) {
81
- fieldContext.validate();
82
- }
79
+ form.handleInput();
83
80
 
84
81
  $props?.onfiles?.(selected);
85
82
  }
@@ -123,10 +120,7 @@ export default function FileDrop($props: FileDropProps | undefined) {
123
120
  }
124
121
 
125
122
  function handleBlur() {
126
- if (fieldContext) {
127
- fieldContext.validate();
128
- }
129
- blurred = true;
123
+ form.handleBlur();
130
124
  }
131
125
 
132
126
  @render {
@@ -142,9 +136,9 @@ export default function FileDrop($props: FileDropProps | undefined) {
142
136
  tabindex={getDisabled() ? -1 : 0}
143
137
  aria-label={$props?.ariaLabel}
144
138
  aria-disabled={getDisabled() ? "true" : undefined}
145
- data-valid={$state.valid ? "valid" : "invalid"}
146
- aria-describedby={$state.messageId}
147
- aria-invalid={fieldContext && !$state.valid ? "true" : undefined}
139
+ data-valid={form.valid ? "valid" : "invalid"}
140
+ aria-describedby={form.messageId}
141
+ aria-invalid={form.inField && !form.valid ? "true" : undefined}
148
142
  onclick={handleClick}
149
143
  onkeydown={handleKey}
150
144
  onblur={handleBlur}
@@ -158,10 +152,10 @@ export default function FileDrop($props: FileDropProps | undefined) {
158
152
  </div>
159
153
 
160
154
  <input
161
- id={inputId}
155
+ id={getInputId()}
162
156
  class="torp-file-drop-input"
163
157
  type="file"
164
- name={$props?.name ?? fieldContext?.name}
158
+ name={form.name}
165
159
  aria-hidden="true"
166
160
  tabindex={-1}
167
161
  multiple={$props?.multiple}
@@ -1,4 +1,5 @@
1
1
  import type { Bindable, ClassValue, StyleValue } from "@torpor/view";
2
+ import createFormField from "../utils/formField";
2
3
  import getId from "../utils/getId";
3
4
  import {
4
5
  type FieldContext,
@@ -42,13 +43,14 @@ interface InputProps {
42
43
  export default function Input($props: InputProps | undefined) {
43
44
  let defaultId = getId();
44
45
 
45
- let type = $props?.type ?? "text";
46
-
47
- let blurred = false;
46
+ function getType(): string {
47
+ return $props?.type ?? "text";
48
+ }
48
49
 
49
50
  // Get stuff out of the FormContext and FieldContext
50
- const formContext = $context[FormContextName] as FormContext;
51
- const fieldContext = $context[FieldContextName] as FieldContext;
51
+ const formContext = $context[FormContextName] as FormContext | undefined;
52
+ const fieldContext = $context[FieldContextName] as FieldContext | undefined;
53
+ const form = createFormField(() => $props?.name, formContext, fieldContext);
52
54
 
53
55
  // Use the FieldContext's state, or create a new one
54
56
  let $state =
@@ -72,22 +74,14 @@ export default function Input($props: InputProps | undefined) {
72
74
  $state.value = $props?.type?.match(/^(number|range)$/)
73
75
  ? +e.currentTarget.value
74
76
  : e.currentTarget.value;
75
- if (formContext) {
76
- formContext.validate();
77
- }
78
- if (fieldContext && blurred) {
79
- fieldContext.validate();
80
- }
77
+ form.handleInput();
81
78
  if ($props?.oninput) {
82
79
  $props.oninput(e);
83
80
  }
84
81
  }
85
82
 
86
83
  function handleBlur() {
87
- if (fieldContext) {
88
- fieldContext.validate();
89
- }
90
- blurred = true;
84
+ form.handleBlur();
91
85
  }
92
86
 
93
87
  @render {
@@ -95,17 +89,17 @@ export default function Input($props: InputProps | undefined) {
95
89
  id={$props?.id ?? defaultId}
96
90
  class={["torp-input", $props?.class]}
97
91
  {$props?.style}
98
- {type}
99
- name={$props?.name ?? fieldContext?.name}
92
+ {getType()}
93
+ name={form.name}
100
94
  &value={$state.value}
101
95
  {$props?.placeholder}
102
96
  {$props?.readonly}
103
97
  {$props?.required}
104
98
  {$props?.maxlength}
105
99
  aria-label={$props?.ariaLabel}
106
- data-valid={$state.valid ? "valid" : "invalid"}
107
- aria-describedby={$state.messageId}
108
- aria-invalid={fieldContext && !$state.valid ? "true" : undefined}
100
+ data-valid={form.valid ? "valid" : "invalid"}
101
+ aria-describedby={form.messageId}
102
+ aria-invalid={form.inField && !form.valid ? "true" : undefined}
109
103
  {$props?.onchange}
110
104
  oninput={handleInput}
111
105
  onblur={handleBlur}
@@ -0,0 +1,71 @@
1
+ import type { ClassValue, StyleValue } from "@torpor/view";
2
+ import getId from "../utils/getId";
3
+ import { type RadioGroupContext, RadioGroupContextName } from "./RadioGroupTypes";
4
+
5
+ interface RadioProps {
6
+ /** An ID for the radio element */
7
+ id?: string;
8
+ /** Classes for the radio element */
9
+ class?: ClassValue;
10
+ /** Styles for the radio element */
11
+ style?: StyleValue;
12
+ /** The value submitted when this radio is selected */
13
+ value?: any;
14
+ /** If set to true, the user cannot interact with this radio */
15
+ disabled?: boolean;
16
+ }
17
+
18
+ /**
19
+ * One option in a RadioGroup. Its content is the radio's label.
20
+ */
21
+ export default function Radio($props: RadioProps | undefined) {
22
+ let defaultId = getId();
23
+ let button: HTMLButtonElement;
24
+
25
+ // Get stuff out of the RadioGroupContext
26
+ const context = $context[RadioGroupContextName] as RadioGroupContext;
27
+ if (!context) {
28
+ throw new Error("Radio must be contained within a RadioGroup");
29
+ }
30
+
31
+ const id = $props?.id ?? defaultId;
32
+ const $state = context.registerItem(undefined, $props?.value, $props?.disabled, id);
33
+ $state.setFocused = () => button.focus();
34
+
35
+ function getDisabled(): boolean {
36
+ return context.getDisabled() || $state.disabled === true;
37
+ }
38
+
39
+ function select() {
40
+ if (getDisabled()) return;
41
+ context.select($state);
42
+ }
43
+
44
+ function handleKey(e: KeyboardEvent) {
45
+ if (getDisabled()) return;
46
+ context.handleKey($state.index!, e);
47
+ }
48
+
49
+ @render {
50
+ <button
51
+ type="button"
52
+ id={id}
53
+ class={["torp-radio", $props?.class]}
54
+ {$props?.style}
55
+ role="radio"
56
+ tabindex={context.isTabStop($state.index!) ? 0 : -1}
57
+ aria-checked={context.isSelected($state) ? "true" : "false"}
58
+ aria-disabled={getDisabled() ? "true" : undefined}
59
+ data-valid={context.getValid() ? "valid" : "invalid"}
60
+ aria-describedby={context.getMessageId()}
61
+ aria-invalid={context.inField() && !context.getValid() ? "true" : undefined}
62
+ disabled={getDisabled()}
63
+ onclick={select}
64
+ onfocus={() => context.handleItemFocus($state.index!)}
65
+ onkeydown={handleKey}
66
+ &ref={button}
67
+ >
68
+ <slot />
69
+ </button>
70
+ }
71
+ }
@@ -0,0 +1,221 @@
1
+ import type { Bindable, ClassValue, StyleValue } from "@torpor/view";
2
+ import { createFocusGroup } from "../utils/focusGroup";
3
+ import getId from "../utils/getId";
4
+ import createFormField from "../utils/formField";
5
+ import FormValues from "./FormValues.torp";
6
+ import {
7
+ type FieldContext,
8
+ FieldContextName,
9
+ type FormContext,
10
+ FormContextName,
11
+ } from "./FormTypes";
12
+ import {
13
+ type RadioGroupContext,
14
+ RadioGroupContextName,
15
+ type RadioItemState,
16
+ } from "./RadioGroupTypes";
17
+
18
+ interface RadioGroupProps {
19
+ /** An ID for the root element */
20
+ id?: string;
21
+ /** Classes for the root element */
22
+ class?: ClassValue;
23
+ /** Styles for the root element */
24
+ style?: StyleValue;
25
+ /** The name of the group's form data field */
26
+ name?: string;
27
+ /** The value of the selected radio */
28
+ value: Bindable<any>;
29
+ /** If set to true, the user cannot interact with this group */
30
+ disabled?: boolean;
31
+ /** An accessible name for the group */
32
+ ariaLabel?: string;
33
+ /** Raised when the selected value has changed */
34
+ onchange?: (value: any) => void;
35
+ }
36
+
37
+ /**
38
+ * A group of radios, of which exactly one can be selected.
39
+ *
40
+ * See [the WAI ARIA guidelines for Radio Groups](https://www.w3.org/WAI/ARIA/apg/patterns/radiogroup/).
41
+ *
42
+ * The structure of a RadioGroup is:
43
+ *
44
+ * ```
45
+ * <RadioGroup name="size" &value={$state.size}>
46
+ * <Radio value="small">Small</Radio>
47
+ * <Radio value="large">Large</Radio>
48
+ * </RadioGroup>
49
+ * ```
50
+ */
51
+ export default function RadioGroup($props: RadioGroupProps | undefined) {
52
+ let defaultId = getId();
53
+ let nextItemIndex = 0;
54
+
55
+ // Wire up the enclosing Form and Field, if any
56
+ const formContext = $context[FormContextName] as FormContext | undefined;
57
+ const fieldContext = $context[FieldContextName] as FieldContext | undefined;
58
+ const form = createFormField(() => $props?.name, formContext, fieldContext);
59
+
60
+ let $state = $watch({
61
+ value: $props?.value,
62
+ });
63
+
64
+ $bind($state, $props, "value");
65
+
66
+ const group = createFocusGroup<RadioItemState>();
67
+
68
+ function getDisabled(): boolean {
69
+ return $props?.disabled === true;
70
+ }
71
+
72
+ function registerItem(
73
+ index: number | undefined,
74
+ value: any,
75
+ disabled: boolean | undefined,
76
+ id: string,
77
+ ): RadioItemState {
78
+ if (index === undefined) {
79
+ index = nextItemIndex++;
80
+ }
81
+
82
+ const existing = group.itemStates.find((item) => item.index === index);
83
+ if (existing) {
84
+ return existing;
85
+ }
86
+
87
+ if (value === undefined) {
88
+ value = index.toString();
89
+ }
90
+
91
+ const itemState: RadioItemState = $watch({
92
+ index,
93
+ value,
94
+ id,
95
+ disabled: disabled === true,
96
+ setFocused: () => {},
97
+ });
98
+ group.registerItem(itemState);
99
+ updateInputId();
100
+ return itemState;
101
+ }
102
+
103
+ function isSelected(item: RadioItemState): boolean {
104
+ return $state.value === item.value;
105
+ }
106
+
107
+ function select(item: RadioItemState) {
108
+ if (getDisabled() || item.disabled) return;
109
+ group.handleItemFocus(item.index!);
110
+ if ($state.value === item.value) return;
111
+ $state.value = item.value;
112
+ updateInputId();
113
+ form.handleInput();
114
+ $props?.onchange?.(item.value);
115
+ }
116
+
117
+ function move(index: number, target: "start" | "previous" | "next" | "end") {
118
+ const before = group.getFocusIndex();
119
+ group.moveFocus(index, target);
120
+ const after = group.getFocusIndex();
121
+ if (after !== -1 && after !== before) {
122
+ select(group.itemStates[after]);
123
+ }
124
+ }
125
+
126
+ function handleKey(index: number, e: KeyboardEvent) {
127
+ if (getDisabled()) return;
128
+ switch (e.key) {
129
+ case "ArrowRight":
130
+ case "ArrowDown": {
131
+ e.preventDefault();
132
+ move(index, "next");
133
+ break;
134
+ }
135
+ case "ArrowLeft":
136
+ case "ArrowUp": {
137
+ e.preventDefault();
138
+ move(index, "previous");
139
+ break;
140
+ }
141
+ case "Home": {
142
+ e.preventDefault();
143
+ move(index, "start");
144
+ break;
145
+ }
146
+ case "End": {
147
+ e.preventDefault();
148
+ move(index, "end");
149
+ break;
150
+ }
151
+ }
152
+ }
153
+
154
+ function getSelectedId(): string | undefined {
155
+ return group.itemStates.find(isSelected)?.id;
156
+ }
157
+
158
+ // Keep the Field's Label pointing at the selected radio, or the first one
159
+ function updateInputId() {
160
+ const id = getSelectedId() ?? group.itemStates[0]?.id;
161
+ if (id && fieldContext) {
162
+ fieldContext.state.inputId = id;
163
+ }
164
+ }
165
+
166
+ $run(() => {
167
+ // Read the value so this re-runs when the selection changes
168
+ $state.value;
169
+ updateInputId();
170
+ });
171
+
172
+ let context: RadioGroupContext = {
173
+ registerItem,
174
+ removeItem: (index) => group.removeItem(index),
175
+ isSelected,
176
+ select,
177
+ handleKey,
178
+ handleItemFocus: (index) => group.handleItemFocus(index),
179
+ isTabStop: (index) => {
180
+ let tabStop = group.getFocusIndex();
181
+ if (tabStop === -1) {
182
+ const selected = group.itemStates.findIndex(isSelected);
183
+ tabStop = selected === -1 ? 0 : selected;
184
+ }
185
+ return tabStop === index;
186
+ },
187
+ getSelectedId,
188
+ getDisabled,
189
+ getValid: () => form.valid,
190
+ getMessageId: () => form.messageId,
191
+ inField: () => form.inField,
192
+ };
193
+ $context[RadioGroupContextName] = context;
194
+
195
+ // Only blur when focus leaves the whole group
196
+ function handleFocusOut(e: FocusEvent) {
197
+ if (!(e.currentTarget as HTMLElement).contains(e.relatedTarget as Node | null)) {
198
+ form.handleBlur();
199
+ }
200
+ }
201
+
202
+ @render {
203
+ <div
204
+ id={$props?.id ?? defaultId}
205
+ class={["torp-radio-group", $props?.class]}
206
+ {$props?.style}
207
+ role="radiogroup"
208
+ aria-label={$props?.ariaLabel}
209
+ aria-disabled={getDisabled() ? "true" : undefined}
210
+ data-valid={form.valid ? "valid" : "invalid"}
211
+ aria-describedby={form.messageId}
212
+ aria-invalid={form.inField && !form.valid ? "true" : undefined}
213
+ onfocusout={handleFocusOut}
214
+ >
215
+ <slot />
216
+ </div>
217
+
218
+ <FormValues name={form.name} value={$state.value} />
219
+ }
220
+
221
+ }
@@ -0,0 +1,40 @@
1
+ import type { FocusItemState } from "../utils/focusGroup";
2
+
3
+ export const RadioGroupContextName: unique symbol = Symbol("RadioGroup");
4
+
5
+ export interface RadioItemState extends FocusItemState {
6
+ /** The value submitted when this radio is selected */
7
+ value: any;
8
+ /** The element ID of this radio */
9
+ id: string;
10
+ }
11
+
12
+ export interface RadioGroupContext {
13
+ registerItem: (
14
+ index: number | undefined,
15
+ value: any,
16
+ disabled: boolean | undefined,
17
+ id: string,
18
+ ) => RadioItemState;
19
+ removeItem: (index: number) => void;
20
+ /** Whether a radio is the selected one */
21
+ isSelected: (item: RadioItemState) => boolean;
22
+ /** Selects a radio, reporting the change */
23
+ select: (item: RadioItemState) => void;
24
+ /** Handles the roving-tabindex arrow keys for a radio */
25
+ handleKey: (index: number, e: KeyboardEvent) => void;
26
+ /** Records that a radio received focus, for the roving tab stop */
27
+ handleItemFocus: (index: number) => void;
28
+ /** Whether the radio should be the group's single tab stop */
29
+ isTabStop: (index: number) => boolean;
30
+ /** The ID of the selected radio, for the Field's Label */
31
+ getSelectedId: () => string | undefined;
32
+ /** Whether the whole group is disabled */
33
+ getDisabled: () => boolean;
34
+ /** The Field's validity, for aria/data attributes */
35
+ getValid: () => boolean;
36
+ /** The Field's message ID, for aria-describedby */
37
+ getMessageId: () => string | undefined;
38
+ /** Whether the group is inside a Form Field */
39
+ inField: () => boolean;
40
+ }
@@ -1,4 +1,5 @@
1
1
  import type { Bindable, ClassValue, StyleValue } from "@torpor/view";
2
+ import createFormField from "../utils/formField";
2
3
  import getId from "../utils/getId";
3
4
  import {
4
5
  type FieldContext,
@@ -36,11 +37,10 @@ interface SelectProps {
36
37
  export default function Select($props: SelectProps | undefined) {
37
38
  let defaultId = getId();
38
39
 
39
- let blurred = false;
40
-
41
40
  // Get stuff out of the FormContext and FieldContext
42
- const formContext = $context[FormContextName] as FormContext;
43
- const fieldContext = $context[FieldContextName] as FieldContext;
41
+ const formContext = $context[FormContextName] as FormContext | undefined;
42
+ const fieldContext = $context[FieldContextName] as FieldContext | undefined;
43
+ const form = createFormField(() => $props?.name, formContext, fieldContext);
44
44
 
45
45
  // Use the FieldContext's state, or create a new one
46
46
  let $state =
@@ -59,22 +59,14 @@ export default function Select($props: SelectProps | undefined) {
59
59
  $bind($state, $props, "value");
60
60
 
61
61
  function handleInput(e: Event & { currentTarget: EventTarget & HTMLInputElement }) {
62
- if (formContext) {
63
- formContext.validate();
64
- }
65
- if (fieldContext && blurred) {
66
- fieldContext.validate();
67
- }
62
+ form.handleInput();
68
63
  if ($props?.oninput) {
69
64
  $props.oninput(e);
70
65
  }
71
66
  }
72
67
 
73
68
  function handleBlur() {
74
- if (fieldContext) {
75
- fieldContext.validate();
76
- }
77
- blurred = true;
69
+ form.handleBlur();
78
70
  }
79
71
 
80
72
  @render {
@@ -82,14 +74,14 @@ export default function Select($props: SelectProps | undefined) {
82
74
  id={$props?.id ?? defaultId}
83
75
  class={["torp-select", $props?.class]}
84
76
  {$props?.style}
85
- name={$props?.name ?? fieldContext?.name}
77
+ name={form.name}
86
78
  &value={$state.value}
87
79
  {$props?.placeholder}
88
80
  {$props?.readonly}
89
81
  aria-label={$props?.ariaLabel}
90
- data-valid={$state.valid ? "valid" : "invalid"}
91
- aria-describedby={$state.messageId}
92
- aria-invalid={fieldContext && !$state.valid ? "true" : undefined}
82
+ data-valid={form.valid ? "valid" : "invalid"}
83
+ aria-describedby={form.messageId}
84
+ aria-invalid={form.inField && !form.valid ? "true" : undefined}
93
85
  {$props?.onchange}
94
86
  oninput={handleInput}
95
87
  onblur={handleBlur}
@@ -1,4 +1,5 @@
1
1
  import type { ClassValue, StyleValue } from "@torpor/view";
2
+ import createFormField from "../utils/formField";
2
3
  import getId from "../utils/getId";
3
4
  import {
4
5
  type FieldContext,
@@ -7,7 +8,7 @@ import {
7
8
  FormContextName,
8
9
  } from "./FormTypes";
9
10
 
10
- interface ToggleProps {
11
+ interface SwitchProps {
11
12
  /** An ID for the root element */
12
13
  id?: string;
13
14
  /** Classes for the root element */
@@ -31,16 +32,19 @@ interface ToggleProps {
31
32
  /**
32
33
  * A switch input in a form, which can be turned on or off.
33
34
  *
35
+ * Named after the switch role: unlike a ToggleButton, a switch has an
36
+ * on/off state, not a pressed/not-pressed action. `Toggle` is kept as a
37
+ * deprecated alias for the original name.
38
+ *
34
39
  * See [the WAI ARIA guidelines for Switches](https://www.w3.org/WAI/ARIA/apg/patterns/switch/).
35
40
  */
36
- export default function Toggle($props: ToggleProps | undefined) {
41
+ export default function Switch($props: SwitchProps | undefined) {
37
42
  let defaultId = getId();
38
43
 
39
- let blurred = false;
40
-
41
44
  // Get stuff out of the FormContext and FieldContext
42
- const formContext = $context[FormContextName] as FormContext;
43
- const fieldContext = $context[FieldContextName] as FieldContext;
45
+ const formContext = $context[FormContextName] as FormContext | undefined;
46
+ const fieldContext = $context[FieldContextName] as FieldContext | undefined;
47
+ const form = createFormField(() => $props?.name, formContext, fieldContext);
44
48
 
45
49
  // Use the FieldContext's state, or create a new one
46
50
  let $state =
@@ -64,38 +68,30 @@ export default function Toggle($props: ToggleProps | undefined) {
64
68
  $state.inputId = $props?.id ?? defaultId;
65
69
 
66
70
  function handleInput(e: Event) {
67
- if (formContext) {
68
- formContext.validate();
69
- }
70
- if (fieldContext && blurred) {
71
- fieldContext.validate();
72
- }
71
+ form.handleInput();
73
72
  if ($props?.oninput) {
74
73
  $props.oninput(e);
75
74
  }
76
75
  }
77
76
 
78
77
  function handleBlur() {
79
- if (fieldContext) {
80
- fieldContext.validate();
81
- }
82
- blurred = true;
78
+ form.handleBlur();
83
79
  }
84
80
 
85
81
  @render {
86
82
  <input
87
83
  id={$props?.id ?? defaultId}
88
- class={["torp-toggle", $props?.class]}
84
+ class={["torp-switch", "torp-toggle", $props?.class]}
89
85
  type="checkbox"
90
86
  role="switch"
91
- name={$props?.name ?? fieldContext?.name}
87
+ name={form.name}
92
88
  &checked={$state.value}
93
89
  aria-checked={$state.value === true ? "true" : "false"}
94
90
  disabled={$props?.disabled}
95
91
  aria-label={$props?.ariaLabel}
96
- data-valid={$state.valid ? "valid" : "invalid"}
97
- aria-describedby={$state.messageId}
98
- aria-invalid={fieldContext && !$state.valid ? "true" : undefined}
92
+ data-valid={form.valid ? "valid" : "invalid"}
93
+ aria-describedby={form.messageId}
94
+ aria-invalid={form.inField && !form.valid ? "true" : undefined}
99
95
  {$props?.onchange}
100
96
  oninput={handleInput}
101
97
  onblur={handleBlur}