Skip to content

Commit

Permalink
Combobox: Fixed aria-describedby and some types (#3065)
Browse files Browse the repository at this point in the history
  • Loading branch information
HalvorHaugan authored Aug 2, 2024
1 parent 6e63069 commit 72c8f31
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-ants-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@navikt/ds-react": patch
---

Combobox: Description is now connected to the input field via aria-describedby
5 changes: 4 additions & 1 deletion @navikt/core/react/src/form/combobox/Combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { ComboboxProps } from "./types";

export const Combobox = forwardRef<
HTMLInputElement,
Omit<ComboboxProps, "onChange" | "options" | "size" | "onClear" | "value">
Omit<
ComboboxProps,
"onChange" | "options" | "size" | "onClear" | "value" | "disabled"
>
>((props, ref) => {
const { className, hideLabel = false, description, label, ...rest } = props;

Expand Down
3 changes: 3 additions & 0 deletions @navikt/core/react/src/form/combobox/ComboboxProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const ComboboxProvider = forwardRef<HTMLInputElement, ComboboxProps>(
allowNewValues = false,
children,
defaultValue,
disabled,
error,
errorId,
filteredOptions: externalFilteredOptions,
Expand All @@ -60,6 +61,8 @@ const ComboboxProvider = forwardRef<HTMLInputElement, ComboboxProps>(
<InputContextProvider
value={{
defaultValue,
description: rest.description,
disabled,
error,
errorId,
id,
Expand Down
34 changes: 22 additions & 12 deletions @navikt/core/react/src/form/combobox/Input/Input.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ComboboxProps } from "../types";

interface InputContextValue extends FormFieldType {
clearInput: NonNullable<ComboboxProps["onClear"]>;
error?: string;
error?: ComboboxProps["error"];
focusInput: () => void;
inputRef: React.RefObject<HTMLInputElement>;
value: string;
Expand All @@ -24,7 +24,24 @@ const [InputContextProvider, useInputContext] =
errorMessage: "useInputContext must be used within an InputContextProvider",
});

const InputProvider = ({ children, value: props }) => {
interface Props {
children: React.ReactNode;
value: {
defaultValue: ComboboxProps["defaultValue"];
description: ComboboxProps["description"];
disabled: ComboboxProps["disabled"];
error: ComboboxProps["error"];
errorId: ComboboxProps["errorId"];
id: ComboboxProps["id"];
value: ComboboxProps["value"];
onChange: ComboboxProps["onChange"];
onClear: ComboboxProps["onClear"];
shouldAutocomplete: ComboboxProps["shouldAutocomplete"];
size: ComboboxProps["size"];
};
}

const InputProvider = ({ children, value: props }: Props) => {
const {
defaultValue = "",
description,
Expand Down Expand Up @@ -69,21 +86,14 @@ const InputProvider = ({ children, value: props }) => {
[externalValue, externalOnChange],
);

const setValue = useCallback(
(text: string) => {
setInternalValue(text);
},
[setInternalValue],
);

const clearInput = useCallback(
(event: React.PointerEvent | React.KeyboardEvent | React.MouseEvent) => {
onClear?.(event);
externalOnChange?.("");
setValue("");
setInternalValue("");
setSearchTerm("");
},
[externalOnChange, onClear, setValue],
[externalOnChange, onClear, setInternalValue],
);

const focusInput = useCallback(() => {
Expand All @@ -103,7 +113,7 @@ const InputProvider = ({ children, value: props }) => {
focusInput,
inputRef,
value,
setValue,
setValue: setInternalValue,
onChange,
searchTerm,
setSearchTerm,
Expand Down
2 changes: 1 addition & 1 deletion @navikt/core/react/src/form/combobox/Input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useSelectedOptionsContext } from "../SelectedOptions/selectedOptionsCon
import { useInputContext } from "./Input.context";

interface InputProps
extends Omit<InputHTMLAttributes<HTMLInputElement>, "value"> {
extends Omit<InputHTMLAttributes<HTMLInputElement>, "value" | "disabled"> {
ref: React.Ref<HTMLInputElement>;
inputClassName?: string;
value?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const InputController = forwardRef<
| "size"
| "onClear"
| "value"
| "disabled"
>
>((props, ref) => {
const {
Expand Down
18 changes: 18 additions & 0 deletions @navikt/core/react/src/form/combobox/combobox.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ Default.args = {
onChange: console.log,
};
Default.argTypes = {
description: {
control: { type: "text" },
},
disabled: {
control: { type: "boolean" },
},
isListOpen: {
control: { type: "boolean" },
},
Expand Down Expand Up @@ -403,6 +409,16 @@ export const WithError: StoryFn = () => {
);
};

export const Disabled: StoryFn = () => {
return (
<UNSAFE_Combobox
options={options}
label="Hva er dine favorittfrukter?"
disabled
/>
);
};

export const Chromatic: StoryFn = () => {
const H2 = (props: { children: string; style?: React.CSSProperties }) => (
<h2 style={{ marginBottom: "-0.25rem", ...props.style }}>
Expand Down Expand Up @@ -433,6 +449,8 @@ export const Chromatic: StoryFn = () => {
<MaxSelectedOptions open />
<H2 style={{ marginTop: "20rem" }}>WithError</H2>
<WithError />
<H2>Disabled</H2>
<Disabled />
</VStack>
);
};
Expand Down
9 changes: 8 additions & 1 deletion @navikt/core/react/src/form/combobox/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export type MaxSelected = {

export interface ComboboxProps
extends FormFieldProps,
Omit<InputHTMLAttributes<HTMLInputElement>, "size" | "onChange" | "value"> {
Omit<
InputHTMLAttributes<HTMLInputElement>,
"size" | "onChange" | "value" | "defaultValue"
> {
/**
* Combobox label.
*/
Expand Down Expand Up @@ -153,4 +156,8 @@ export interface ComboboxProps
* This converts the input to a controlled input, so you have to use onChange to update the value.
*/
value?: string;
/**
* Initial value of the input field. Only works when the input is uncontrolled.
*/
defaultValue?: string;
}

0 comments on commit 72c8f31

Please sign in to comment.