@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 +114 -112
- package/dist/form.cjs.map +1 -1
- package/dist/form.mjs +115 -113
- package/dist/form.mjs.map +1 -1
- package/dist/styles.css +9 -1
- package/dist/styles.no-base-tokens.css +1 -1
- package/package.json +11 -10
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
|
|
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 =
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
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
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
}
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
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
|
-
|
|
1191
|
+
break;
|
|
1192
|
+
case "Enter":
|
|
1188
1193
|
handleSelection();
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
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
|
-
|
|
1198
|
-
}
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
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
|
{
|