Skip to content

Commit 5a034fd

Browse files
HedwigJDoetsgjulivan
authored andcommitted
feat: add getValidationErrorId utility function
1 parent 7800abc commit 5a034fd

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

packages/pluggableWidgets/combobox-web/src/components/MultiSelection/MultiSelection.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import classNames from "classnames";
22
import { createElement, Fragment, KeyboardEvent, ReactElement, useMemo, useRef } from "react";
33
import { ClearButton } from "../../assets/icons";
44
import { MultiSelector, SelectionBaseProps } from "../../helpers/types";
5-
import { getInputLabel, getSelectedCaptionsPlaceholder } from "../../helpers/utils";
5+
import { getInputLabel, getSelectedCaptionsPlaceholder, getValidationErrorId } from "../../helpers/utils";
66
import { useDownshiftMultiSelectProps } from "../../hooks/useDownshiftMultiSelectProps";
77
import { useLazyLoading } from "../../hooks/useLazyLoading";
88
import { ComboboxWrapper } from "../ComboboxWrapper";
@@ -38,6 +38,7 @@ export function MultiSelection({
3838
const isSelectedItemsBoxStyle = selector.selectedItemsStyle === "boxes";
3939
const isOptionsSelected = selector.isOptionsSelected();
4040
const inputLabel = getInputLabel(options.inputId);
41+
const errorId = getValidationErrorId(options.inputId);
4142
const hasLabel = useMemo(() => Boolean(inputLabel), [inputLabel]);
4243
const inputProps = getInputProps({
4344
...getDropdownProps(
@@ -86,7 +87,6 @@ export function MultiSelection({
8687
readOnly: selector.readOnly
8788
});
8889

89-
const errorId = options.inputId ? options.inputId + "-validation-message" : undefined;
9090
return (
9191
<Fragment>
9292
<ComboboxWrapper

packages/pluggableWidgets/combobox-web/src/components/SingleSelection/SingleSelection.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import classNames from "classnames";
22
import { createElement, Fragment, ReactElement, useMemo, useRef } from "react";
33
import { ClearButton } from "../../assets/icons";
44
import { SelectionBaseProps, SingleSelector } from "../../helpers/types";
5-
import { getInputLabel } from "../../helpers/utils";
5+
import { getInputLabel, getValidationErrorId } from "../../helpers/utils";
66
import { useDownshiftSingleSelectProps } from "../../hooks/useDownshiftSingleSelectProps";
77
import { useLazyLoading } from "../../hooks/useLazyLoading";
88
import { ComboboxWrapper } from "../ComboboxWrapper";
@@ -29,7 +29,6 @@ export function SingleSelection({
2929
highlightedIndex
3030
} = useDownshiftSingleSelectProps(selector, options, a11yConfig.a11yStatusMessage);
3131
const inputRef = useRef<HTMLInputElement>(null);
32-
const errorId = options.inputId ? options.inputId + "-validation-message" : undefined;
3332
const lazyLoading = selector.lazyLoading ?? false;
3433
const { onScroll } = useLazyLoading({
3534
hasMoreItems: selector.options.hasMore ?? false,
@@ -58,6 +57,7 @@ export function SingleSelection({
5857
);
5958

6059
const inputLabel = getInputLabel(options.inputId);
60+
const errorId = getValidationErrorId(options.inputId);
6161
const hasLabel = useMemo(() => Boolean(inputLabel), [inputLabel]);
6262

6363
const inputProps = getInputProps(

packages/pluggableWidgets/combobox-web/src/helpers/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,7 @@ function sortSelections(
153153
export function getInputLabel(inputId: string): Element | null {
154154
return document.querySelector(`label[for="${inputId}"]`);
155155
}
156+
157+
export function getValidationErrorId(inputId?: string): string | undefined {
158+
return inputId ? inputId + "-validation-message" : undefined;
159+
}

0 commit comments

Comments
 (0)