@entur/form 10.3.1 → 10.3.2

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/dist/form.cjs CHANGED
@@ -137,9 +137,9 @@ const InputGroupLabel = ({
137
137
  const VariantContext = React.createContext(null);
138
138
  const VariantProvider = ({
139
139
  children,
140
- variant = null
140
+ variant
141
141
  }) => {
142
- return /* @__PURE__ */ jsxRuntime.jsx(VariantContext.Provider, { value: variant, children });
142
+ return /* @__PURE__ */ jsxRuntime.jsx(VariantContext.Provider, { value: variant ?? null, children });
143
143
  };
144
144
  const useVariant = () => {
145
145
  const context = React.useContext(VariantContext);
@@ -1133,120 +1133,122 @@ const getLastWithDataValue = (parent) => {
1133
1133
  return null;
1134
1134
  };
1135
1135
  const defaultElement = "button";
1136
- const SegmentedChoice = React.forwardRef(function SegmentedChoice2({
1137
- children,
1138
- className,
1139
- style,
1140
- value,
1141
- as,
1142
- onChange,
1143
- "data-first-child": isFirstChild,
1144
- ...rest
1145
- }, ref) {
1146
- const Element = as || defaultElement;
1147
- const {
1148
- value: selectedValue,
1149
- onChange: contextOnChange,
1150
- size,
1151
- focusedValue,
1152
- setFocusedValue
1153
- } = useSegmentedContext();
1154
- const isChecked = selectedValue === value;
1155
- const tabIndex = React.useMemo(() => {
1156
- if (selectedValue !== null) return isChecked ? 0 : -1;
1157
- return isFirstChild ? 0 : -1;
1158
- }, [isChecked, selectedValue, isFirstChild]);
1159
- const handleSelection = () => {
1160
- contextOnChange(value);
1161
- onChange?.(value);
1162
- };
1163
- const handleKeyDown = (e) => {
1164
- const current = e.currentTarget;
1165
- const focusChoice = (el) => {
1166
- if (!el) return;
1167
- el.focus();
1168
- setFocusedValue(el.getAttribute("data-value") || null);
1136
+ const SegmentedChoice = utils.forwardRefPolymorphic(
1137
+ function SegmentedChoice2({
1138
+ children,
1139
+ className,
1140
+ style,
1141
+ value,
1142
+ as,
1143
+ onChange,
1144
+ "data-first-child": isFirstChild,
1145
+ ...rest
1146
+ }, ref) {
1147
+ const Element = as || defaultElement;
1148
+ const {
1149
+ value: selectedValue,
1150
+ onChange: contextOnChange,
1151
+ size,
1152
+ focusedValue,
1153
+ setFocusedValue
1154
+ } = useSegmentedContext();
1155
+ const isChecked = selectedValue === value;
1156
+ const tabIndex = React.useMemo(() => {
1157
+ if (selectedValue !== null) return isChecked ? 0 : -1;
1158
+ return isFirstChild ? 0 : -1;
1159
+ }, [isChecked, selectedValue, isFirstChild]);
1160
+ const handleSelection = () => {
1161
+ contextOnChange(value);
1162
+ onChange?.(value);
1169
1163
  };
1170
- switch (e.key) {
1171
- case "ArrowLeft":
1172
- case "ArrowUp": {
1173
- e.preventDefault();
1174
- focusChoice(getPrevWithDataValue(current));
1175
- break;
1176
- }
1177
- case "ArrowRight":
1178
- case "ArrowDown": {
1179
- e.preventDefault();
1180
- focusChoice(getNextWithDataValue(current));
1181
- break;
1182
- }
1183
- case "Home": {
1184
- e.preventDefault();
1185
- const parent = current.parentElement;
1186
- focusChoice(getFirstWithDataValue(parent));
1187
- break;
1188
- }
1189
- case "End": {
1190
- e.preventDefault();
1191
- const parent = current.parentElement;
1192
- focusChoice(getLastWithDataValue(parent));
1193
- break;
1194
- }
1195
- case " ":
1196
- e.preventDefault();
1197
- if (as === "a") {
1198
- handleSelection();
1199
- const linkElement = e.currentTarget;
1200
- if (linkElement.href) {
1201
- queueMicrotask(() => {
1202
- linkElement.click();
1203
- });
1164
+ const handleKeyDown = (e) => {
1165
+ const current = e.currentTarget;
1166
+ const focusChoice = (el) => {
1167
+ if (!el) return;
1168
+ el.focus();
1169
+ setFocusedValue(el.getAttribute("data-value") || null);
1170
+ };
1171
+ switch (e.key) {
1172
+ case "ArrowLeft":
1173
+ case "ArrowUp": {
1174
+ e.preventDefault();
1175
+ focusChoice(getPrevWithDataValue(current));
1176
+ break;
1177
+ }
1178
+ case "ArrowRight":
1179
+ case "ArrowDown": {
1180
+ e.preventDefault();
1181
+ focusChoice(getNextWithDataValue(current));
1182
+ break;
1183
+ }
1184
+ case "Home": {
1185
+ e.preventDefault();
1186
+ const parent = current.parentElement;
1187
+ focusChoice(getFirstWithDataValue(parent));
1188
+ break;
1189
+ }
1190
+ case "End": {
1191
+ e.preventDefault();
1192
+ const parent = current.parentElement;
1193
+ focusChoice(getLastWithDataValue(parent));
1194
+ break;
1195
+ }
1196
+ case " ":
1197
+ e.preventDefault();
1198
+ if (as === "a") {
1199
+ handleSelection();
1200
+ const linkElement = e.currentTarget;
1201
+ if (linkElement.href) {
1202
+ queueMicrotask(() => {
1203
+ linkElement.click();
1204
+ });
1205
+ }
1206
+ } else {
1207
+ handleSelection();
1204
1208
  }
1205
- } else {
1209
+ break;
1210
+ case "Enter":
1206
1211
  handleSelection();
1207
- }
1208
- break;
1209
- case "Enter":
1210
- handleSelection();
1211
- break;
1212
- case "Escape":
1213
- e.preventDefault();
1212
+ break;
1213
+ case "Escape":
1214
+ e.preventDefault();
1215
+ setFocusedValue(null);
1216
+ break;
1217
+ }
1218
+ };
1219
+ const onFocus = React.useCallback(() => {
1220
+ setFocusedValue(value);
1221
+ }, [setFocusedValue, value]);
1222
+ const onBlur = React.useCallback(() => {
1223
+ if (focusedValue === value) {
1214
1224
  setFocusedValue(null);
1215
- break;
1216
- }
1217
- };
1218
- const onFocus = React.useCallback(() => {
1219
- setFocusedValue(value);
1220
- }, [setFocusedValue, value]);
1221
- const onBlur = React.useCallback(() => {
1222
- if (focusedValue === value) {
1223
- setFocusedValue(null);
1224
- }
1225
- }, [focusedValue, value, setFocusedValue]);
1226
- const elementProps = {
1227
- className: classNames(
1228
- "eds-segmented-choice",
1229
- {
1230
- "eds-segmented-choice--large": size === "large"
1231
- },
1232
- className
1233
- ),
1234
- style,
1235
- "aria-checked": isChecked,
1236
- "data-value": value,
1237
- ref,
1238
- tabIndex,
1239
- onClick: handleSelection,
1240
- onKeyDown: handleKeyDown,
1241
- onFocus,
1242
- onBlur,
1243
- role: "radio",
1244
- // For defaultElement button we override type submit
1245
- ...as === void 0 && { type: "button" },
1246
- ...rest
1247
- };
1248
- return /* @__PURE__ */ jsxRuntime.jsx(Element, { ...elementProps, children });
1249
- });
1225
+ }
1226
+ }, [focusedValue, value, setFocusedValue]);
1227
+ const elementProps = {
1228
+ className: classNames(
1229
+ "eds-segmented-choice",
1230
+ {
1231
+ "eds-segmented-choice--large": size === "large"
1232
+ },
1233
+ className
1234
+ ),
1235
+ style,
1236
+ "aria-checked": isChecked,
1237
+ "data-value": value,
1238
+ ref,
1239
+ tabIndex,
1240
+ onClick: handleSelection,
1241
+ onKeyDown: handleKeyDown,
1242
+ onFocus,
1243
+ onBlur,
1244
+ role: "radio",
1245
+ // For defaultElement button we override type submit
1246
+ ...as === void 0 && { type: "button" },
1247
+ ...rest
1248
+ };
1249
+ return /* @__PURE__ */ jsxRuntime.jsx(Element, { ...elementProps, children });
1250
+ }
1251
+ );
1250
1252
  const SegmentedControlComponent = Object.assign(
1251
1253
  SegmentedControl,
1252
1254
  {