@entur/form 8.3.1 → 8.3.2-beta.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.
@@ -0,0 +1,1121 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const utils = require("@entur/utils");
4
+ const jsxRuntime = require("react/jsx-runtime");
5
+ const React = require("react");
6
+ const icons = require("@entur/icons");
7
+ const tooltip = require("@entur/tooltip");
8
+ const button = require("@entur/button");
9
+ const typography = require("@entur/typography");
10
+ const tokens = require("@entur/tokens");
11
+ function _interopNamespaceDefault(e) {
12
+ const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
13
+ if (e) {
14
+ for (const k in e) {
15
+ if (k !== "default") {
16
+ const d = Object.getOwnPropertyDescriptor(e, k);
17
+ Object.defineProperty(n, k, d.get ? d : {
18
+ enumerable: true,
19
+ get: () => e[k]
20
+ });
21
+ }
22
+ }
23
+ }
24
+ n.default = e;
25
+ return Object.freeze(n);
26
+ }
27
+ const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
28
+ function getDefaultExportFromCjs(x) {
29
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
30
+ }
31
+ var classnames = { exports: {} };
32
+ /*!
33
+ Copyright (c) 2018 Jed Watson.
34
+ Licensed under the MIT License (MIT), see
35
+ http://jedwatson.github.io/classnames
36
+ */
37
+ var hasRequiredClassnames;
38
+ function requireClassnames() {
39
+ if (hasRequiredClassnames) return classnames.exports;
40
+ hasRequiredClassnames = 1;
41
+ (function(module2) {
42
+ (function() {
43
+ var hasOwn = {}.hasOwnProperty;
44
+ function classNames2() {
45
+ var classes = "";
46
+ for (var i = 0; i < arguments.length; i++) {
47
+ var arg = arguments[i];
48
+ if (arg) {
49
+ classes = appendClass(classes, parseValue(arg));
50
+ }
51
+ }
52
+ return classes;
53
+ }
54
+ function parseValue(arg) {
55
+ if (typeof arg === "string" || typeof arg === "number") {
56
+ return arg;
57
+ }
58
+ if (typeof arg !== "object") {
59
+ return "";
60
+ }
61
+ if (Array.isArray(arg)) {
62
+ return classNames2.apply(null, arg);
63
+ }
64
+ if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes("[native code]")) {
65
+ return arg.toString();
66
+ }
67
+ var classes = "";
68
+ for (var key in arg) {
69
+ if (hasOwn.call(arg, key) && arg[key]) {
70
+ classes = appendClass(classes, key);
71
+ }
72
+ }
73
+ return classes;
74
+ }
75
+ function appendClass(value, newClass) {
76
+ if (!newClass) {
77
+ return value;
78
+ }
79
+ if (value) {
80
+ return value + " " + newClass;
81
+ }
82
+ return value + newClass;
83
+ }
84
+ if (module2.exports) {
85
+ classNames2.default = classNames2;
86
+ module2.exports = classNames2;
87
+ } else {
88
+ window.classNames = classNames2;
89
+ }
90
+ })();
91
+ })(classnames);
92
+ return classnames.exports;
93
+ }
94
+ var classnamesExports = requireClassnames();
95
+ const classNames = /* @__PURE__ */ getDefaultExportFromCjs(classnamesExports);
96
+ const info = "info";
97
+ const error$1 = "error";
98
+ const AlertIcon = ({ variant }) => {
99
+ const iconClass = `eds-feedback-text__icon eds-feedback-text__icon--${variant}`;
100
+ switch (variant) {
101
+ case "success":
102
+ return /* @__PURE__ */ jsxRuntime.jsx(
103
+ icons.ValidationSuccessFilledIcon,
104
+ {
105
+ "aria-label": "Suksessmelding",
106
+ className: iconClass
107
+ }
108
+ );
109
+ case "negative":
110
+ return /* @__PURE__ */ jsxRuntime.jsx(
111
+ icons.ValidationErrorFilledIcon,
112
+ {
113
+ "aria-label": "Feilmelding",
114
+ className: iconClass
115
+ }
116
+ );
117
+ case "information":
118
+ return null;
119
+ case "warning":
120
+ return /* @__PURE__ */ jsxRuntime.jsx(
121
+ icons.ValidationExclamationFilledIcon,
122
+ {
123
+ "aria-label": "Varselmelding",
124
+ className: iconClass
125
+ }
126
+ );
127
+ case error$1:
128
+ return /* @__PURE__ */ jsxRuntime.jsx(
129
+ icons.ValidationErrorFilledIcon,
130
+ {
131
+ "aria-label": "Feilmelding",
132
+ className: iconClass
133
+ }
134
+ );
135
+ case info:
136
+ return null;
137
+ default:
138
+ return null;
139
+ }
140
+ };
141
+ const FeedbackText = ({
142
+ children,
143
+ hideIcon = false,
144
+ variant,
145
+ className,
146
+ ...rest
147
+ }) => {
148
+ return /* @__PURE__ */ jsxRuntime.jsxs(
149
+ typography.SubLabel,
150
+ {
151
+ className: classNames(
152
+ "eds-feedback-text",
153
+ {
154
+ "eds-feedback-text--information": variant === info || variant === "information"
155
+ },
156
+ className
157
+ ),
158
+ ...rest,
159
+ children: [
160
+ !hideIcon && /* @__PURE__ */ jsxRuntime.jsx(AlertIcon, { variant }),
161
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "eds-feedback-text__text", children })
162
+ ]
163
+ }
164
+ );
165
+ };
166
+ const InputGroupContext = React__namespace.createContext({
167
+ isFilled: false,
168
+ setFilled: () => null
169
+ });
170
+ const InputGroupContextProvider = ({ children }) => {
171
+ const [filled, setFilled] = React__namespace.useState(false);
172
+ return /* @__PURE__ */ jsxRuntime.jsx(
173
+ InputGroupContext.Provider,
174
+ {
175
+ value: { isFilled: filled, setFilled },
176
+ children
177
+ }
178
+ );
179
+ };
180
+ const useInputGroupContext = () => React__namespace.useContext(InputGroupContext);
181
+ const InputGroupLabel = ({
182
+ label,
183
+ required,
184
+ labelId,
185
+ staticAnimation = false,
186
+ ...rest
187
+ }) => {
188
+ const { isFilled: isFilled2 } = useInputGroupContext();
189
+ const filler = staticAnimation || isFilled2;
190
+ return /* @__PURE__ */ jsxRuntime.jsx(
191
+ "label",
192
+ {
193
+ className: classNames(rest.className, {
194
+ "eds-input-group-label-wrapper--filled": filler
195
+ }),
196
+ id: labelId,
197
+ ...rest,
198
+ children: /* @__PURE__ */ jsxRuntime.jsxs(
199
+ "span",
200
+ {
201
+ className: classNames("eds-input-group__label", {
202
+ "eds-input-group__label--filled": filler
203
+ }),
204
+ children: [
205
+ label,
206
+ " ",
207
+ required && /* @__PURE__ */ jsxRuntime.jsx("span", { children: "*" })
208
+ ]
209
+ }
210
+ )
211
+ }
212
+ );
213
+ };
214
+ const VariantContext = React.createContext(null);
215
+ const VariantProvider = ({
216
+ children,
217
+ variant = null
218
+ }) => {
219
+ return /* @__PURE__ */ jsxRuntime.jsx(VariantContext.Provider, { value: variant, children });
220
+ };
221
+ const useVariant = () => {
222
+ const context = React.useContext(VariantContext);
223
+ return context;
224
+ };
225
+ const error = "error";
226
+ const BaseFormControl = React.forwardRef(
227
+ ({
228
+ after,
229
+ before,
230
+ children,
231
+ className,
232
+ disabled = false,
233
+ readOnly = false,
234
+ variant,
235
+ prepend,
236
+ append,
237
+ size = "medium",
238
+ isFilled: isFilled2,
239
+ label,
240
+ required,
241
+ labelTooltip,
242
+ labelTooltipButtonAriaLabel = `Klikk for tilleggsinfo om ${label}-feltet`,
243
+ labelTooltipPlacement = "top",
244
+ feedback,
245
+ labelId,
246
+ labelProps,
247
+ style,
248
+ disableLabelAnimation = false,
249
+ ariaAlertOnFeedback = false,
250
+ ariaLabelOnReadOnly = "Dette skjemafeltet kan bare leses",
251
+ ...rest
252
+ }, ref) => {
253
+ const contextVariant = useVariant();
254
+ const currentVariant = variant || contextVariant;
255
+ return /* @__PURE__ */ jsxRuntime.jsx(InputGroupContextProvider, { children: /* @__PURE__ */ jsxRuntime.jsxs(
256
+ "div",
257
+ {
258
+ className: classNames(
259
+ "eds-form-control__field-and-feedback-text",
260
+ className,
261
+ {
262
+ "eds-form-control__field-and-feedback-text--has-tooltip": labelTooltip !== void 0
263
+ }
264
+ ),
265
+ style,
266
+ children: [
267
+ before,
268
+ /* @__PURE__ */ jsxRuntime.jsxs(
269
+ "div",
270
+ {
271
+ className: classNames(
272
+ "eds-form-control-wrapper",
273
+ `eds-form-control-wrapper--size-${size}`,
274
+ {
275
+ "eds-form-control-wrapper--success": currentVariant === "success",
276
+ "eds-form-control-wrapper--negative": currentVariant === "negative" || currentVariant === error,
277
+ "eds-form-control-wrapper--disabled": disabled,
278
+ "eds-form-control-wrapper--readonly": readOnly,
279
+ "eds-form-control-wrapper--is-filled": isFilled2
280
+ }
281
+ ),
282
+ ref,
283
+ tabIndex: readOnly ? -1 : void 0,
284
+ ...rest,
285
+ children: [
286
+ prepend && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "eds-form-control__prepend", children: prepend }),
287
+ /* @__PURE__ */ jsxRuntime.jsx(
288
+ InputGroupLabel,
289
+ {
290
+ label,
291
+ required,
292
+ labelId,
293
+ staticAnimation: disableLabelAnimation,
294
+ "aria-label": readOnly ? `${label}, ${ariaLabelOnReadOnly}` : void 0,
295
+ ...labelProps
296
+ }
297
+ ),
298
+ labelTooltip && /* @__PURE__ */ jsxRuntime.jsx(
299
+ tooltip.Tooltip,
300
+ {
301
+ content: labelTooltip,
302
+ placement: labelTooltipPlacement,
303
+ showCloseButton: false,
304
+ disableFocusListener: true,
305
+ disableHoverListener: true,
306
+ disableClickListner: false,
307
+ disableKeyboardListener: false,
308
+ children: /* @__PURE__ */ jsxRuntime.jsx(
309
+ button.IconButton,
310
+ {
311
+ as: "span",
312
+ tabIndex: 0,
313
+ role: "button",
314
+ className: "eds-form-control__append eds-form-control__append--tooltip",
315
+ "aria-label": labelTooltipButtonAriaLabel,
316
+ children: /* @__PURE__ */ jsxRuntime.jsx(
317
+ icons.QuestionIcon,
318
+ {
319
+ className: "eds-input-group__label-tooltip-icon",
320
+ "aria-hidden": "true"
321
+ }
322
+ )
323
+ }
324
+ )
325
+ }
326
+ ),
327
+ children,
328
+ append && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "eds-form-control__append", children: append })
329
+ ]
330
+ }
331
+ ),
332
+ feedback && currentVariant && /* @__PURE__ */ jsxRuntime.jsx(
333
+ FeedbackText,
334
+ {
335
+ variant: currentVariant,
336
+ role: ariaAlertOnFeedback ? "alert" : void 0,
337
+ children: feedback
338
+ }
339
+ ),
340
+ after
341
+ ]
342
+ }
343
+ ) });
344
+ }
345
+ );
346
+ const Checkbox = React.forwardRef(
347
+ ({
348
+ checked,
349
+ className,
350
+ children,
351
+ style,
352
+ disabled = false,
353
+ readOnly = false,
354
+ reduceClickArea,
355
+ disableAnimation = false,
356
+ ...rest
357
+ }, ref) => {
358
+ const inputRef = React.useRef(null);
359
+ const isIndeterminate = checked === "indeterminate";
360
+ const isControlled = checked !== void 0;
361
+ React.useEffect(() => {
362
+ if (inputRef && inputRef.current) {
363
+ inputRef.current.indeterminate = isIndeterminate;
364
+ }
365
+ }, [isIndeterminate]);
366
+ return /* @__PURE__ */ jsxRuntime.jsxs(
367
+ "label",
368
+ {
369
+ className: classNames("eds-checkbox__container", className, {
370
+ "eds-checkbox--disabled": disabled,
371
+ "eds-checkbox--readonly": readOnly,
372
+ "eds-checkbox__container--reduced-click-area": reduceClickArea
373
+ }),
374
+ style,
375
+ children: [
376
+ /* @__PURE__ */ jsxRuntime.jsx(
377
+ "input",
378
+ {
379
+ type: "checkbox",
380
+ ref: utils.mergeRefs(ref, inputRef),
381
+ checked: isControlled ? checked === true : void 0,
382
+ disabled,
383
+ onKeyDown: (e) => {
384
+ if (readOnly && (e.key === " " || e.key === "Enter")) {
385
+ e.preventDefault();
386
+ e.stopPropagation();
387
+ }
388
+ },
389
+ "aria-label": readOnly ? ` ${children?.toString()}. Kan ikke endres` : void 0,
390
+ ...rest
391
+ }
392
+ ),
393
+ /* @__PURE__ */ jsxRuntime.jsx(
394
+ "span",
395
+ {
396
+ className: classNames("eds-checkbox__icon", {
397
+ "eds-checkbox__icon--disabled": disabled,
398
+ "eds-checkbox__icon--readonly": readOnly,
399
+ "eds-checkbox__icon--reduced-click-area": reduceClickArea,
400
+ "eds-checkbox__icon--no-animation": disableAnimation || disabled || readOnly
401
+ }),
402
+ children: /* @__PURE__ */ jsxRuntime.jsx(CheckboxIcon, { indeterminate: isIndeterminate })
403
+ }
404
+ ),
405
+ children && /* @__PURE__ */ jsxRuntime.jsx(typography.Paragraph, { className: "eds-checkbox__label", margin: "none", as: "span", children })
406
+ ]
407
+ }
408
+ );
409
+ }
410
+ );
411
+ const CheckboxIcon = ({
412
+ indeterminate = false
413
+ }) => {
414
+ return /* @__PURE__ */ jsxRuntime.jsx(
415
+ "svg",
416
+ {
417
+ className: "eds-checkbox-icon",
418
+ width: "11px",
419
+ height: "9px",
420
+ viewBox: "6 11 37 33",
421
+ "aria-hidden": true,
422
+ children: indeterminate ? /* @__PURE__ */ jsxRuntime.jsx("rect", { x: "10", y: "25", width: "28", height: "5", fill: "white" }) : /* @__PURE__ */ jsxRuntime.jsx("path", { d: "M14.1 27.2l7.1 7.2 14.6-14.8", fill: "none" })
423
+ }
424
+ );
425
+ };
426
+ const Fieldset = ({
427
+ children,
428
+ className,
429
+ label,
430
+ ...rest
431
+ }) => /* @__PURE__ */ jsxRuntime.jsxs("fieldset", { className: classNames("eds-fieldset", className), ...rest, children: [
432
+ label && /* @__PURE__ */ jsxRuntime.jsx(typography.Heading5, { as: "legend", children: label }),
433
+ children
434
+ ] });
435
+ const RadioGroupContext = React.createContext(
436
+ null
437
+ );
438
+ const RadioGroupContextProvider = RadioGroupContext.Provider;
439
+ const useRadioGroupContext = () => {
440
+ const context = React.useContext(RadioGroupContext);
441
+ if (!context) {
442
+ throw new Error(
443
+ "You need to wrap your RadioButtons in a RadioGroup component"
444
+ );
445
+ }
446
+ return context;
447
+ };
448
+ const Radio = React.forwardRef(
449
+ ({ className, children, value, disabled, readOnlyLabelDescription, ...rest }, ref) => {
450
+ const {
451
+ name,
452
+ value: selectedValue,
453
+ onChange,
454
+ readOnly
455
+ } = useRadioGroupContext();
456
+ const classList = classNames(className, "eds-form-component--radio__radio", {
457
+ "eds-form-component--radio__radio--disabled": disabled,
458
+ "eds-form-component--radio__radio--readonly": readOnly
459
+ });
460
+ return /* @__PURE__ */ jsxRuntime.jsxs("label", { className: "eds-form-component--radio__container", children: [
461
+ /* @__PURE__ */ jsxRuntime.jsx(
462
+ "input",
463
+ {
464
+ type: "radio",
465
+ name: rest.name ?? name,
466
+ ref,
467
+ value,
468
+ checked: rest.checked ?? selectedValue === value,
469
+ onChange: (e) => {
470
+ if (readOnly) {
471
+ e.preventDefault();
472
+ return;
473
+ }
474
+ (rest.onChange ?? onChange)?.(e);
475
+ },
476
+ onClick: (e) => {
477
+ if (readOnly) {
478
+ e.preventDefault();
479
+ }
480
+ },
481
+ disabled,
482
+ "aria-label": readOnly ? `${children?.toString()}. ${readOnlyLabelDescription ?? "Kan ikke endres"}` : void 0,
483
+ ...rest
484
+ }
485
+ ),
486
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: classList, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "eds-form-component--radio__circle" }) }),
487
+ children && /* @__PURE__ */ jsxRuntime.jsx(
488
+ typography.Paragraph,
489
+ {
490
+ margin: "none",
491
+ as: "span",
492
+ className: "eds-form-component--radio__label",
493
+ children
494
+ }
495
+ )
496
+ ] });
497
+ }
498
+ );
499
+ const InputPanelBase = React.forwardRef(
500
+ ({
501
+ className,
502
+ children,
503
+ value,
504
+ title,
505
+ secondaryLabel,
506
+ size = "medium",
507
+ hideSelectionIndicator = false,
508
+ style,
509
+ id,
510
+ disabled = false,
511
+ type = "radio",
512
+ onChange,
513
+ checked,
514
+ name,
515
+ ...rest
516
+ }, ref) => {
517
+ const classList = classNames(
518
+ className,
519
+ "eds-input-panel__container",
520
+ `eds-input-panel--${size}`
521
+ );
522
+ const inputRef = React.useRef(null);
523
+ const defaultId = utils.useRandomId("eds-inputpanel");
524
+ const inputPanelId = id || defaultId;
525
+ const forceUpdate = utils.useForceUpdate();
526
+ const handleOnChange = (e) => {
527
+ if (onChange === void 0) forceUpdate();
528
+ onChange?.(e);
529
+ };
530
+ return /* @__PURE__ */ jsxRuntime.jsxs("label", { className: "eds-input-panel", htmlFor: inputPanelId, children: [
531
+ /* @__PURE__ */ jsxRuntime.jsx(
532
+ "input",
533
+ {
534
+ type,
535
+ name,
536
+ ref: utils.mergeRefs(ref, inputRef),
537
+ value,
538
+ checked,
539
+ onChange: handleOnChange,
540
+ id: inputPanelId,
541
+ disabled,
542
+ ...rest
543
+ }
544
+ ),
545
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: classList, style, children: [
546
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "eds-input-panel__title-wrapper", children: [
547
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "eds-input-panel__title", children: title }),
548
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "eds-input-panel__secondary-label-and-icon-wrapper", children: [
549
+ secondaryLabel !== void 0 && /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: secondaryLabel }),
550
+ /* @__PURE__ */ jsxRuntime.jsx("span", { style: { pointerEvents: "none" }, children: !(disabled || hideSelectionIndicator) && (type === "radio" ? /* @__PURE__ */ jsxRuntime.jsx(
551
+ Radio,
552
+ {
553
+ name: "",
554
+ value: "",
555
+ checked: checked ?? inputRef.current?.checked ?? false,
556
+ onChange: () => {
557
+ return;
558
+ },
559
+ "aria-hidden": "true",
560
+ tabIndex: -1
561
+ }
562
+ ) : /* @__PURE__ */ jsxRuntime.jsx(
563
+ Checkbox,
564
+ {
565
+ checked: checked ?? inputRef.current?.checked ?? false,
566
+ onChange: () => null,
567
+ "aria-hidden": "true",
568
+ tabIndex: -1
569
+ }
570
+ )) })
571
+ ] })
572
+ ] }),
573
+ children && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "eds-input-panel__additional-content", children })
574
+ ] })
575
+ ] });
576
+ }
577
+ );
578
+ const RadioPanel = React.forwardRef(
579
+ ({
580
+ className,
581
+ children,
582
+ value,
583
+ title,
584
+ secondaryLabel,
585
+ size = "medium",
586
+ hideRadioButton = false,
587
+ style,
588
+ id,
589
+ disabled = false,
590
+ ...rest
591
+ }, ref) => {
592
+ const { name, value: selected, onChange } = useRadioGroupContext();
593
+ return /* @__PURE__ */ jsxRuntime.jsx(
594
+ InputPanelBase,
595
+ {
596
+ type: "radio",
597
+ name,
598
+ title,
599
+ value,
600
+ checked: selected === value,
601
+ onChange,
602
+ className,
603
+ secondaryLabel,
604
+ size,
605
+ hideSelectionIndicator: hideRadioButton,
606
+ style,
607
+ id,
608
+ disabled,
609
+ ...rest,
610
+ ref,
611
+ children
612
+ }
613
+ );
614
+ }
615
+ );
616
+ const CheckboxPanel = React.forwardRef(
617
+ ({
618
+ name,
619
+ checked,
620
+ onChange,
621
+ className,
622
+ children,
623
+ value,
624
+ title,
625
+ secondaryLabel,
626
+ size = "medium",
627
+ hideCheckbox = false,
628
+ style,
629
+ id,
630
+ disabled = false,
631
+ ...rest
632
+ }, ref) => {
633
+ return /* @__PURE__ */ jsxRuntime.jsx(
634
+ InputPanelBase,
635
+ {
636
+ type: "checkbox",
637
+ name,
638
+ title,
639
+ value,
640
+ checked,
641
+ onChange,
642
+ className,
643
+ secondaryLabel,
644
+ size,
645
+ hideSelectionIndicator: hideCheckbox,
646
+ style,
647
+ id,
648
+ disabled,
649
+ ...rest,
650
+ ref,
651
+ children
652
+ }
653
+ );
654
+ }
655
+ );
656
+ const RadioGroup = ({
657
+ name,
658
+ value,
659
+ children,
660
+ onChange,
661
+ label,
662
+ readOnly = false,
663
+ ...rest
664
+ }) => {
665
+ const contextValue = React.useMemo(
666
+ () => ({ name, value, onChange, readOnly }),
667
+ [name, value, onChange, readOnly]
668
+ );
669
+ return /* @__PURE__ */ jsxRuntime.jsx(RadioGroupContextProvider, { value: contextValue, children: label ? /* @__PURE__ */ jsxRuntime.jsx(Fieldset, { label, ...rest, children }) : children });
670
+ };
671
+ const Switch = React.forwardRef(
672
+ ({
673
+ className,
674
+ children,
675
+ labelPlacement = "right",
676
+ icon,
677
+ hideIcon = false,
678
+ color = tokens.colors.validation.mint,
679
+ contrastColor = tokens.colors.validation.mintContrast,
680
+ size = "medium",
681
+ checked,
682
+ ...rest
683
+ }, ref) => {
684
+ const displayedIcon = () => {
685
+ if (icon) return icon;
686
+ if (checked === void 0) return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, {});
687
+ const iconSize = size === "large" ? 23 : void 0;
688
+ return checked ? /* @__PURE__ */ jsxRuntime.jsx(icons.CheckIcon, { size: iconSize }) : /* @__PURE__ */ jsxRuntime.jsx(icons.CloseSmallIcon, { size: iconSize });
689
+ };
690
+ return /* @__PURE__ */ jsxRuntime.jsxs(
691
+ "label",
692
+ {
693
+ className: classNames(
694
+ "eds-switch",
695
+ `eds-switch--${labelPlacement}`,
696
+ className
697
+ ),
698
+ style: { ...rest.style },
699
+ children: [
700
+ /* @__PURE__ */ jsxRuntime.jsx("input", { type: "checkbox", ref, checked, ...rest }),
701
+ /* @__PURE__ */ jsxRuntime.jsx(
702
+ "span",
703
+ {
704
+ className: classNames(
705
+ "eds-switch__switch",
706
+ `eds-switch__switch--${size}`
707
+ ),
708
+ style: {
709
+ "--eds-switch-color": color,
710
+ "--eds-switch-contrast-color": contrastColor
711
+ },
712
+ "aria-hidden": "true",
713
+ children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "eds-switch__circle", children: !hideIcon && displayedIcon() })
714
+ }
715
+ ),
716
+ children && /* @__PURE__ */ jsxRuntime.jsx(
717
+ "span",
718
+ {
719
+ className: classNames(
720
+ "eds-switch__label",
721
+ `eds-switch__label--${size}--${labelPlacement}`
722
+ ),
723
+ children
724
+ }
725
+ )
726
+ ]
727
+ }
728
+ );
729
+ }
730
+ );
731
+ function hasValue(value) {
732
+ return value != null && !(Array.isArray(value) && value.length === 0);
733
+ }
734
+ function isFilled(obj, SSR = false) {
735
+ return obj && (hasValue(obj.value) && obj.value !== "" || SSR && hasValue(obj.defaultValue) && obj.defaultValue !== "");
736
+ }
737
+ const TextArea = React.forwardRef(
738
+ ({
739
+ variant,
740
+ disabled = false,
741
+ readOnly = false,
742
+ className,
743
+ style,
744
+ label,
745
+ feedback,
746
+ labelTooltip,
747
+ labelTooltipButtonAriaLabel,
748
+ labelTooltipPlacement,
749
+ onChange,
750
+ disableLabelAnimation,
751
+ ...rest
752
+ }, ref) => {
753
+ const textAreaId = utils.useRandomId("eds-textarea");
754
+ const textareaRef = React.useRef(null);
755
+ return /* @__PURE__ */ jsxRuntime.jsx(
756
+ BaseFormControl,
757
+ {
758
+ className: classNames(className, "eds-textarea__wrapper"),
759
+ disabled,
760
+ readOnly,
761
+ variant,
762
+ style,
763
+ label,
764
+ labelId: textAreaId,
765
+ feedback,
766
+ labelTooltip,
767
+ labelTooltipButtonAriaLabel,
768
+ labelTooltipPlacement,
769
+ labelProps: { className: "eds-textarea__label" },
770
+ disableLabelAnimation,
771
+ onClick: (e) => {
772
+ if (e.target === e.currentTarget) textareaRef?.current?.focus();
773
+ },
774
+ children: /* @__PURE__ */ jsxRuntime.jsx(
775
+ TextAreaBase,
776
+ {
777
+ readOnly,
778
+ disabled,
779
+ ref: utils.mergeRefs(ref, textareaRef),
780
+ "aria-labelledby": textAreaId,
781
+ onChange,
782
+ variant,
783
+ ...rest
784
+ }
785
+ )
786
+ }
787
+ );
788
+ }
789
+ );
790
+ const TextAreaBase = React.forwardRef(
791
+ ({ readOnly, disabled, onChange, value, variant, ...rest }, ref) => {
792
+ const contextVariant = useVariant();
793
+ const currentVariant = variant || contextVariant;
794
+ const { isFilled: isInputFilled, setFilled: setFiller } = useInputGroupContext();
795
+ utils.useOnMount(() => {
796
+ if (value?.toString() || rest.defaultValue) {
797
+ setFiller && !isInputFilled && setFiller(true);
798
+ }
799
+ });
800
+ React.useEffect(() => {
801
+ if (value?.toString() && setFiller && !isInputFilled) {
802
+ setFiller(true);
803
+ }
804
+ }, [value, setFiller, isInputFilled]);
805
+ const handleChange = (event) => {
806
+ if (isFilled(event.target)) {
807
+ setFiller && !isInputFilled && setFiller(true);
808
+ } else {
809
+ setFiller && isInputFilled && setFiller(false);
810
+ }
811
+ if (onChange) {
812
+ onChange(event);
813
+ }
814
+ };
815
+ return /* @__PURE__ */ jsxRuntime.jsx(
816
+ "textarea",
817
+ {
818
+ className: "eds-form-control eds-textarea",
819
+ ref,
820
+ readOnly,
821
+ disabled,
822
+ onChange: handleChange,
823
+ value,
824
+ "aria-invalid": currentVariant === "error",
825
+ ...rest
826
+ }
827
+ );
828
+ }
829
+ );
830
+ const TextField = React.forwardRef(
831
+ ({
832
+ prepend,
833
+ append,
834
+ variant,
835
+ disabled = false,
836
+ readOnly = false,
837
+ className,
838
+ style,
839
+ size = "medium",
840
+ label,
841
+ required,
842
+ labelTooltip,
843
+ labelTooltipButtonAriaLabel,
844
+ labelTooltipPlacement,
845
+ feedback,
846
+ onChange,
847
+ disableLabelAnimation,
848
+ labelProps,
849
+ clearable = false,
850
+ onClear,
851
+ value,
852
+ ariaAlertOnFeedback = false,
853
+ ...rest
854
+ }, ref) => {
855
+ const randomId = utils.useRandomId("eds-textfield");
856
+ const textFieldId = labelProps && labelProps.id ? labelProps.id : randomId;
857
+ const textFieldRef = React.useRef(null);
858
+ return /* @__PURE__ */ jsxRuntime.jsx(
859
+ BaseFormControl,
860
+ {
861
+ disabled,
862
+ readOnly,
863
+ variant,
864
+ prepend,
865
+ append: clearable && onClear ? /* @__PURE__ */ jsxRuntime.jsx(ClearButton, { onClear }) : append,
866
+ className: classNames(className, "eds-textfield__wrapper"),
867
+ style,
868
+ size,
869
+ label,
870
+ required,
871
+ labelTooltip,
872
+ labelTooltipButtonAriaLabel,
873
+ labelTooltipPlacement,
874
+ labelId: textFieldId,
875
+ feedback,
876
+ disableLabelAnimation,
877
+ labelProps,
878
+ ariaAlertOnFeedback,
879
+ onClick: (e) => {
880
+ if (e.target === e.currentTarget) textFieldRef?.current?.focus();
881
+ },
882
+ children: /* @__PURE__ */ jsxRuntime.jsx(
883
+ TextFieldBase,
884
+ {
885
+ disabled,
886
+ readOnly,
887
+ ref: utils.mergeRefs(ref, textFieldRef),
888
+ "aria-labelledby": textFieldId,
889
+ onChange,
890
+ value,
891
+ variant,
892
+ ...rest
893
+ }
894
+ )
895
+ }
896
+ );
897
+ }
898
+ );
899
+ const TextFieldBase = React.forwardRef(
900
+ ({ disabled, readOnly, placeholder, onChange, value, variant, ...rest }, forwardRef) => {
901
+ const contextVariant = useVariant();
902
+ const currentVariant = variant || contextVariant;
903
+ const { isFilled: isInputFilled, setFilled: setFiller } = useInputGroupContext();
904
+ utils.useOnMount(() => {
905
+ if (value?.toString() || rest.defaultValue) {
906
+ setFiller && !isInputFilled && setFiller(true);
907
+ }
908
+ });
909
+ React.useEffect(() => {
910
+ if (value?.toString() && setFiller && !isInputFilled) {
911
+ setFiller(true);
912
+ }
913
+ }, [value, setFiller, isInputFilled]);
914
+ const handleChange = (event) => {
915
+ if (isFilled(event.target)) {
916
+ setFiller && !isInputFilled && setFiller(true);
917
+ } else {
918
+ setFiller && isInputFilled && setFiller(false);
919
+ }
920
+ if (onChange) {
921
+ onChange(event);
922
+ }
923
+ };
924
+ return /* @__PURE__ */ jsxRuntime.jsx(
925
+ "input",
926
+ {
927
+ "aria-invalid": currentVariant === "error",
928
+ className: "eds-form-control",
929
+ disabled,
930
+ readOnly,
931
+ ref: forwardRef,
932
+ placeholder,
933
+ onChange: handleChange,
934
+ value,
935
+ ...rest
936
+ }
937
+ );
938
+ }
939
+ );
940
+ const ClearButton = ({ onClear, ...props }) => {
941
+ const { isFilled: hasValue2, setFilled } = useInputGroupContext();
942
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "eds-textfield__clear-button-wrapper", children: [
943
+ hasValue2 && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "eds-textfield__divider" }),
944
+ hasValue2 && /* @__PURE__ */ jsxRuntime.jsx(
945
+ "button",
946
+ {
947
+ className: "eds-textfield__clear-button",
948
+ type: "button",
949
+ tabIndex: -1,
950
+ onClick: () => {
951
+ setFilled(false);
952
+ onClear();
953
+ },
954
+ ...props,
955
+ children: /* @__PURE__ */ jsxRuntime.jsx(icons.CloseSmallIcon, {})
956
+ }
957
+ )
958
+ ] });
959
+ };
960
+ const SegmentedContext = React.createContext(
961
+ null
962
+ );
963
+ const SegmentedProvider = ({
964
+ name,
965
+ onChange = () => void 0,
966
+ selectedValue,
967
+ multiple,
968
+ size,
969
+ ...rest
970
+ }) => {
971
+ const generatedName = utils.useRandomId("eds-segmented-control");
972
+ const contextValue = React.useMemo(
973
+ () => ({
974
+ name: name || generatedName,
975
+ onChange,
976
+ multiple,
977
+ selectedValue,
978
+ size
979
+ }),
980
+ [generatedName, multiple, name, onChange, selectedValue, size]
981
+ );
982
+ return /* @__PURE__ */ jsxRuntime.jsx(SegmentedContext.Provider, { value: contextValue, ...rest });
983
+ };
984
+ const useSegmentedContext = () => {
985
+ const context = React.useContext(SegmentedContext);
986
+ if (!context) {
987
+ throw new Error(
988
+ "You need to wrap your SegmentedChoice in either SegmentedControl or MultipleSegmentedControl"
989
+ );
990
+ }
991
+ return context;
992
+ };
993
+ const SegmentedChoice = React.forwardRef(
994
+ ({
995
+ children,
996
+ className,
997
+ style,
998
+ value,
999
+ name,
1000
+ onChange = () => void 0,
1001
+ ...rest
1002
+ }, ref) => {
1003
+ const {
1004
+ name: commonName,
1005
+ selectedValue,
1006
+ onChange: commonOnChange,
1007
+ multiple,
1008
+ size
1009
+ } = useSegmentedContext();
1010
+ const isChecked = multiple ? selectedValue[value] : selectedValue === value;
1011
+ const handleChange = (e) => {
1012
+ onChange(e);
1013
+ if (multiple) {
1014
+ commonOnChange({
1015
+ ...selectedValue,
1016
+ [value]: e.target.checked
1017
+ });
1018
+ } else if (e.target.checked) {
1019
+ commonOnChange(value);
1020
+ }
1021
+ };
1022
+ return /* @__PURE__ */ jsxRuntime.jsxs(
1023
+ "label",
1024
+ {
1025
+ className: classNames("eds-segmented-choice", className),
1026
+ style,
1027
+ children: [
1028
+ /* @__PURE__ */ jsxRuntime.jsx(
1029
+ "input",
1030
+ {
1031
+ type: multiple ? "checkbox" : "radio",
1032
+ name: name || commonName,
1033
+ checked: isChecked,
1034
+ value,
1035
+ onChange: handleChange,
1036
+ ref,
1037
+ ...rest
1038
+ }
1039
+ ),
1040
+ /* @__PURE__ */ jsxRuntime.jsx(
1041
+ "div",
1042
+ {
1043
+ className: classNames("eds-base-segmented", {
1044
+ "eds-base-segmented--large": size === "large"
1045
+ }),
1046
+ children
1047
+ }
1048
+ )
1049
+ ]
1050
+ }
1051
+ );
1052
+ }
1053
+ );
1054
+ const SegmentedControl = ({
1055
+ children,
1056
+ label,
1057
+ name,
1058
+ onChange,
1059
+ selectedValue,
1060
+ size = "medium",
1061
+ className,
1062
+ ...rest
1063
+ }) => {
1064
+ return /* @__PURE__ */ jsxRuntime.jsx(
1065
+ SegmentedProvider,
1066
+ {
1067
+ name,
1068
+ selectedValue,
1069
+ onChange,
1070
+ multiple: false,
1071
+ size,
1072
+ children: /* @__PURE__ */ jsxRuntime.jsx(Fieldset, { label, children: /* @__PURE__ */ jsxRuntime.jsx(
1073
+ "div",
1074
+ {
1075
+ className: classNames("eds-segmented-control", className),
1076
+ ...rest,
1077
+ children
1078
+ }
1079
+ ) })
1080
+ }
1081
+ );
1082
+ };
1083
+ const MultipleSegmentedControl = ({ children, label, name, onChange, selectedValue, ...rest }) => {
1084
+ return /* @__PURE__ */ jsxRuntime.jsxs(
1085
+ SegmentedProvider,
1086
+ {
1087
+ name,
1088
+ selectedValue,
1089
+ onChange,
1090
+ multiple: true,
1091
+ size: "medium",
1092
+ children: [
1093
+ /* @__PURE__ */ jsxRuntime.jsx(typography.Label, { as: "div", children: label }),
1094
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "eds-segmented-control", ...rest, children })
1095
+ ]
1096
+ }
1097
+ );
1098
+ };
1099
+ utils.warnAboutMissingStyles("form", "icons", "typography");
1100
+ exports.BaseFormControl = BaseFormControl;
1101
+ exports.Checkbox = Checkbox;
1102
+ exports.CheckboxPanel = CheckboxPanel;
1103
+ exports.FeedbackText = FeedbackText;
1104
+ exports.Fieldset = Fieldset;
1105
+ exports.InputGroupContextProvider = InputGroupContextProvider;
1106
+ exports.InputGroupLabel = InputGroupLabel;
1107
+ exports.MultipleSegmentedControl = MultipleSegmentedControl;
1108
+ exports.Radio = Radio;
1109
+ exports.RadioGroup = RadioGroup;
1110
+ exports.RadioPanel = RadioPanel;
1111
+ exports.SegmentedChoice = SegmentedChoice;
1112
+ exports.SegmentedControl = SegmentedControl;
1113
+ exports.Switch = Switch;
1114
+ exports.TextArea = TextArea;
1115
+ exports.TextField = TextField;
1116
+ exports.VariantProvider = VariantProvider;
1117
+ exports.hasValue = hasValue;
1118
+ exports.isFilled = isFilled;
1119
+ exports.useInputGroupContext = useInputGroupContext;
1120
+ exports.useVariant = useVariant;
1121
+ //# sourceMappingURL=form.cjs.js.map