-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Combobox: Add support for
onBlur
, and omit props that have no effect (
- Loading branch information
1 parent
914b0db
commit c8b7909
Showing
11 changed files
with
127 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@navikt/ds-react": minor | ||
--- | ||
|
||
Combobox: Add support for `onBlur`, and omit props that have no effect. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
aksel.nav.no/website/pages/eksempler/combobox/react-hook-form.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Controller, SubmitHandler, useForm } from "react-hook-form"; | ||
import { Button, UNSAFE_Combobox, VStack } from "@navikt/ds-react"; | ||
import { withDsExample } from "@/web/examples/withDsExample"; | ||
|
||
type Inputs = { transportmiddel: string[] }; | ||
|
||
const Example = () => { | ||
const { | ||
handleSubmit, | ||
control, | ||
formState: { errors }, | ||
} = useForm<Inputs>({ | ||
shouldFocusError: false, | ||
defaultValues: { transportmiddel: [] }, | ||
}); | ||
|
||
const onValidSubmit: SubmitHandler<Inputs> = (data) => { | ||
alert("Du valgte: " + data.transportmiddel.join(", ")); | ||
}; | ||
|
||
return ( | ||
<VStack as="form" gap="8" onSubmit={handleSubmit(onValidSubmit)}> | ||
<Controller | ||
control={control} | ||
rules={{ required: "Du må velge minst ett transportmiddel." }} | ||
name="transportmiddel" | ||
render={({ field }) => ( | ||
<UNSAFE_Combobox | ||
id="transportmiddel" | ||
label="Hva er de kuleste transportmidlene?" | ||
options={options} | ||
isMultiSelect | ||
error={errors.transportmiddel?.message} | ||
ref={field.ref} | ||
name={field.name} | ||
onBlur={field.onBlur} | ||
onToggleSelected={(option, isSelected) => { | ||
if (isSelected) { | ||
field.onChange([...field.value, option]); | ||
} else { | ||
field.onChange(field.value.filter((v) => v !== option)); | ||
} | ||
}} | ||
/> | ||
)} | ||
/> | ||
<div> | ||
<Button type="submit">Send inn</Button> | ||
</div> | ||
</VStack> | ||
); | ||
}; | ||
|
||
const options = [ | ||
"car", | ||
"bus", | ||
"train", | ||
"skateboard", | ||
"bicycle", | ||
"motorcycle", | ||
"boat", | ||
"airplane", | ||
"helicopter", | ||
"truck", | ||
"van", | ||
"scooter", | ||
]; | ||
|
||
// EXAMPLES DO NOT INCLUDE CONTENT BELOW THIS LINE | ||
export default withDsExample(Example, { variant: "static" }); | ||
|
||
/* Storybook story */ | ||
export const Demo = { | ||
render: Example, | ||
}; | ||
|
||
export const args = { | ||
index: 9, | ||
desc: "Eksempel på bruk med react-hook-form.", | ||
sandbox: false, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters