@entur/form 10.3.0 → 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.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { mergeRefs, useForceUpdate, useOnMount, warnAboutMissingStyles } from "@entur/utils";
1
+ import { mergeRefs, useForceUpdate, useOnMount, forwardRefPolymorphic, warnAboutMissingStyles } from "@entur/utils";
2
2
  import { jsxs, jsx, Fragment } from "react/jsx-runtime";
3
3
  import * as React from "react";
4
4
  import React__default, { useRef, useId } from "react";
@@ -119,9 +119,9 @@ const InputGroupLabel = ({
119
119
  const VariantContext = React__default.createContext(null);
120
120
  const VariantProvider = ({
121
121
  children,
122
- variant = null
122
+ variant
123
123
  }) => {
124
- return /* @__PURE__ */ jsx(VariantContext.Provider, { value: variant, children });
124
+ return /* @__PURE__ */ jsx(VariantContext.Provider, { value: variant ?? null, children });
125
125
  };
126
126
  const useVariant = () => {
127
127
  const context = React__default.useContext(VariantContext);
@@ -1115,120 +1115,122 @@ const getLastWithDataValue = (parent) => {
1115
1115
  return null;
1116
1116
  };
1117
1117
  const defaultElement = "button";
1118
- const SegmentedChoice = React__default.forwardRef(function SegmentedChoice2({
1119
- children,
1120
- className,
1121
- style,
1122
- value,
1123
- as,
1124
- onChange,
1125
- "data-first-child": isFirstChild,
1126
- ...rest
1127
- }, ref) {
1128
- const Element = as || defaultElement;
1129
- const {
1130
- value: selectedValue,
1131
- onChange: contextOnChange,
1132
- size,
1133
- focusedValue,
1134
- setFocusedValue
1135
- } = useSegmentedContext();
1136
- const isChecked = selectedValue === value;
1137
- const tabIndex = React__default.useMemo(() => {
1138
- if (selectedValue !== null) return isChecked ? 0 : -1;
1139
- return isFirstChild ? 0 : -1;
1140
- }, [isChecked, selectedValue, isFirstChild]);
1141
- const handleSelection = () => {
1142
- contextOnChange(value);
1143
- onChange?.(value);
1144
- };
1145
- const handleKeyDown = (e) => {
1146
- const current = e.currentTarget;
1147
- const focusChoice = (el) => {
1148
- if (!el) return;
1149
- el.focus();
1150
- setFocusedValue(el.getAttribute("data-value") || null);
1118
+ const SegmentedChoice = forwardRefPolymorphic(
1119
+ function SegmentedChoice2({
1120
+ children,
1121
+ className,
1122
+ style,
1123
+ value,
1124
+ as,
1125
+ onChange,
1126
+ "data-first-child": isFirstChild,
1127
+ ...rest
1128
+ }, ref) {
1129
+ const Element = as || defaultElement;
1130
+ const {
1131
+ value: selectedValue,
1132
+ onChange: contextOnChange,
1133
+ size,
1134
+ focusedValue,
1135
+ setFocusedValue
1136
+ } = useSegmentedContext();
1137
+ const isChecked = selectedValue === value;
1138
+ const tabIndex = React__default.useMemo(() => {
1139
+ if (selectedValue !== null) return isChecked ? 0 : -1;
1140
+ return isFirstChild ? 0 : -1;
1141
+ }, [isChecked, selectedValue, isFirstChild]);
1142
+ const handleSelection = () => {
1143
+ contextOnChange(value);
1144
+ onChange?.(value);
1151
1145
  };
1152
- switch (e.key) {
1153
- case "ArrowLeft":
1154
- case "ArrowUp": {
1155
- e.preventDefault();
1156
- focusChoice(getPrevWithDataValue(current));
1157
- break;
1158
- }
1159
- case "ArrowRight":
1160
- case "ArrowDown": {
1161
- e.preventDefault();
1162
- focusChoice(getNextWithDataValue(current));
1163
- break;
1164
- }
1165
- case "Home": {
1166
- e.preventDefault();
1167
- const parent = current.parentElement;
1168
- focusChoice(getFirstWithDataValue(parent));
1169
- break;
1170
- }
1171
- case "End": {
1172
- e.preventDefault();
1173
- const parent = current.parentElement;
1174
- focusChoice(getLastWithDataValue(parent));
1175
- break;
1176
- }
1177
- case " ":
1178
- e.preventDefault();
1179
- if (as === "a") {
1180
- handleSelection();
1181
- const linkElement = e.currentTarget;
1182
- if (linkElement.href) {
1183
- queueMicrotask(() => {
1184
- linkElement.click();
1185
- });
1146
+ const handleKeyDown = (e) => {
1147
+ const current = e.currentTarget;
1148
+ const focusChoice = (el) => {
1149
+ if (!el) return;
1150
+ el.focus();
1151
+ setFocusedValue(el.getAttribute("data-value") || null);
1152
+ };
1153
+ switch (e.key) {
1154
+ case "ArrowLeft":
1155
+ case "ArrowUp": {
1156
+ e.preventDefault();
1157
+ focusChoice(getPrevWithDataValue(current));
1158
+ break;
1159
+ }
1160
+ case "ArrowRight":
1161
+ case "ArrowDown": {
1162
+ e.preventDefault();
1163
+ focusChoice(getNextWithDataValue(current));
1164
+ break;
1165
+ }
1166
+ case "Home": {
1167
+ e.preventDefault();
1168
+ const parent = current.parentElement;
1169
+ focusChoice(getFirstWithDataValue(parent));
1170
+ break;
1171
+ }
1172
+ case "End": {
1173
+ e.preventDefault();
1174
+ const parent = current.parentElement;
1175
+ focusChoice(getLastWithDataValue(parent));
1176
+ break;
1177
+ }
1178
+ case " ":
1179
+ e.preventDefault();
1180
+ if (as === "a") {
1181
+ handleSelection();
1182
+ const linkElement = e.currentTarget;
1183
+ if (linkElement.href) {
1184
+ queueMicrotask(() => {
1185
+ linkElement.click();
1186
+ });
1187
+ }
1188
+ } else {
1189
+ handleSelection();
1186
1190
  }
1187
- } else {
1191
+ break;
1192
+ case "Enter":
1188
1193
  handleSelection();
1189
- }
1190
- break;
1191
- case "Enter":
1192
- handleSelection();
1193
- break;
1194
- case "Escape":
1195
- e.preventDefault();
1194
+ break;
1195
+ case "Escape":
1196
+ e.preventDefault();
1197
+ setFocusedValue(null);
1198
+ break;
1199
+ }
1200
+ };
1201
+ const onFocus = React__default.useCallback(() => {
1202
+ setFocusedValue(value);
1203
+ }, [setFocusedValue, value]);
1204
+ const onBlur = React__default.useCallback(() => {
1205
+ if (focusedValue === value) {
1196
1206
  setFocusedValue(null);
1197
- break;
1198
- }
1199
- };
1200
- const onFocus = React__default.useCallback(() => {
1201
- setFocusedValue(value);
1202
- }, [setFocusedValue, value]);
1203
- const onBlur = React__default.useCallback(() => {
1204
- if (focusedValue === value) {
1205
- setFocusedValue(null);
1206
- }
1207
- }, [focusedValue, value, setFocusedValue]);
1208
- const elementProps = {
1209
- className: classNames(
1210
- "eds-segmented-choice",
1211
- {
1212
- "eds-segmented-choice--large": size === "large"
1213
- },
1214
- className
1215
- ),
1216
- style,
1217
- "aria-checked": isChecked,
1218
- "data-value": value,
1219
- ref,
1220
- tabIndex,
1221
- onClick: handleSelection,
1222
- onKeyDown: handleKeyDown,
1223
- onFocus,
1224
- onBlur,
1225
- role: "radio",
1226
- // For defaultElement button we override type submit
1227
- ...as === void 0 && { type: "button" },
1228
- ...rest
1229
- };
1230
- return /* @__PURE__ */ jsx(Element, { ...elementProps, children });
1231
- });
1207
+ }
1208
+ }, [focusedValue, value, setFocusedValue]);
1209
+ const elementProps = {
1210
+ className: classNames(
1211
+ "eds-segmented-choice",
1212
+ {
1213
+ "eds-segmented-choice--large": size === "large"
1214
+ },
1215
+ className
1216
+ ),
1217
+ style,
1218
+ "aria-checked": isChecked,
1219
+ "data-value": value,
1220
+ ref,
1221
+ tabIndex,
1222
+ onClick: handleSelection,
1223
+ onKeyDown: handleKeyDown,
1224
+ onFocus,
1225
+ onBlur,
1226
+ role: "radio",
1227
+ // For defaultElement button we override type submit
1228
+ ...as === void 0 && { type: "button" },
1229
+ ...rest
1230
+ };
1231
+ return /* @__PURE__ */ jsx(Element, { ...elementProps, children });
1232
+ }
1233
+ );
1232
1234
  const SegmentedControlComponent = Object.assign(
1233
1235
  SegmentedControl,
1234
1236
  {