File tree Expand file tree Collapse file tree 3 files changed +49
-13
lines changed
packages/form-hooks/src/RhfCheckboxGroup Expand file tree Collapse file tree 3 files changed +49
-13
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import { expect } from 'vitest';
55
66import * as stories from './RhfCheckboxGroup.stories' ;
77
8- const { Default } = composeStories ( stories ) ;
8+ const { Default, MedValidering } = composeStories ( stories ) ;
99
1010describe ( 'RhfCheckboxGroup' , ( ) => {
1111 it ( 'skal sette verdi' , async ( ) => {
@@ -23,4 +23,23 @@ describe('RhfCheckboxGroup', () => {
2323 await userEvent . click ( screen . getByText ( 'Verdi 1' ) ) ;
2424 expect ( screen . getByLabelText ( 'Verdi 1' ) ) . toBeChecked ( ) ;
2525 } ) ;
26+
27+ it ( 'skal gi feil ved ingen valgte elementer' , async ( ) => {
28+ await MedValidering . run ( ) ;
29+ expect ( screen . getByText ( 'Dette er en checkboks med validering' ) ) . toBeInTheDocument ( ) ;
30+
31+ const checkboxes = screen . getAllByLabelText ( / V e r d i / ) ;
32+
33+ await userEvent . click ( checkboxes [ 0 ] ) ;
34+ await userEvent . click ( checkboxes [ 2 ] ) ;
35+
36+ await userEvent . click ( screen . getByText ( 'Submit' ) ) ;
37+
38+ expect ( await screen . findByText ( 'Du må velge minst ett element' ) ) . toBeInTheDocument ( ) ;
39+
40+ await userEvent . click ( checkboxes [ 0 ] ) ;
41+ await userEvent . click ( checkboxes [ 1 ] ) ;
42+
43+ expect ( screen . getByText ( 'Verdi 1 og 2 kan ikke velges samtidig' ) ) . toBeInTheDocument ( ) ;
44+ } ) ;
2645} ) ;
Original file line number Diff line number Diff line change @@ -10,15 +10,19 @@ const meta = {
1010 args : {
1111 isReadOnly : false ,
1212 isEdited : false ,
13- control : undefined , // This will be provided by the decorator
13+ control : undefined ,
14+ children : [
15+ < Checkbox key = "verdi1" value = "verdi1" >
16+ Verdi 1
17+ </ Checkbox > ,
18+ < Checkbox key = "verdi2" value = "verdi2" >
19+ Verdi 2
20+ </ Checkbox > ,
21+ < Checkbox key = "verdi3" value = "verdi3" >
22+ Verdi 3
23+ </ Checkbox > ,
24+ ] ,
1425 } ,
15- render : args => (
16- < RhfCheckboxGroup { ...args } >
17- < Checkbox value = "verdi1" > Verdi 1</ Checkbox >
18- < Checkbox value = "verdi2" > Verdi 2</ Checkbox >
19- < Checkbox value = "verdi3" > Verdi 3</ Checkbox >
20- </ RhfCheckboxGroup >
21- ) ,
2226 decorators : rhfDecorator ( { checkboxpanelpre : [ 'verdi1' , 'verdi3' ] } ) ,
2327} satisfies Meta < typeof RhfCheckboxGroup > ;
2428
@@ -49,3 +53,17 @@ export const ReadOnlyMedOverstyrtMarkering: Story = {
4953 name : 'checkboxpanelpre' ,
5054 } ,
5155} ;
56+
57+ export const MedValidering : Story = {
58+ args : {
59+ validate : [
60+ checkedElements => ( checkedElements . length < 1 ? 'Du må velge minst ett element' : undefined ) ,
61+ checkedElements =>
62+ checkedElements . includes ( 'verdi1' ) && checkedElements . includes ( 'verdi2' )
63+ ? 'Verdi 1 og 2 kan ikke velges samtidig'
64+ : undefined ,
65+ ] ,
66+ label : 'Dette er en checkboks med validering' ,
67+ name : 'checkboxpanelpre' ,
68+ } ,
69+ } ;
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ export interface CheckboxProps {
1717type Props < T extends FieldValues > = {
1818 label ?: string | ReactNode ;
1919 description ?: string ;
20- validate ?: ( ( value : string | number ) => ValidationReturnType ) [ ] ;
20+ validate ?: ( ( values : ( string | number ) [ ] ) => ValidationReturnType ) [ ] ;
2121 onChange ?: ( value : any ) => void ;
2222 isReadOnly ?: boolean ;
2323 hideLegend ?: boolean ;
@@ -37,10 +37,9 @@ export const RhfCheckboxGroup = <T extends FieldValues>({
3737 isEdited = false ,
3838 size = 'small' ,
3939 children,
40- ...controllerProps
40+ name,
41+ control,
4142} : Props < T > ) => {
42- const { name, control } = controllerProps ;
43-
4443 const {
4544 formState : { errors } ,
4645 } = useFormContext ( ) ;
You can’t perform that action at this time.
0 commit comments