Skip to content

Commit 6771932

Browse files
committed
add radio conditional story, fix radiogroup
1 parent 4e512d3 commit 6771932

File tree

2 files changed

+62
-20
lines changed

2 files changed

+62
-20
lines changed

packages/react/src/components/Radio/Radio.stories.tsx

+39
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { FloppydiskIcon, PencilIcon } from '@navikt/aksel-icons';
12
import type { Meta, StoryFn, StoryObj } from '@storybook/react';
3+
import { useState } from 'react';
24
import {
35
Button,
46
Divider,
@@ -133,3 +135,40 @@ export const Inline: StoryFn<typeof Fieldset> = () => (
133135
</div>
134136
</Fieldset>
135137
);
138+
139+
export const Conditional: StoryFn<UseRadioGroupProps> = (args) => {
140+
const { getRadioProps, validationMessageProps, value } = useRadioGroup({
141+
name: 'kommunikasjonskanal',
142+
...args,
143+
});
144+
const [open, setOpen] = useState(false);
145+
146+
return (
147+
<>
148+
Din kommunikasjonskanal: {value}
149+
{open ? (
150+
<>
151+
<Button onClick={() => setOpen(false)}>
152+
<FloppydiskIcon /> Lagre
153+
</Button>
154+
<Fieldset>
155+
<Fieldset.Legend>
156+
Hvordan vil du helst at vi skal kontakte deg?
157+
</Fieldset.Legend>
158+
<Fieldset.Description>
159+
Velg alle alternativene som er relevante for deg.
160+
</Fieldset.Description>
161+
<Radio label='E-post' {...getRadioProps('epost')} />
162+
<Radio label='Telefon' {...getRadioProps('telefon')} />
163+
<Radio label='SMS' {...getRadioProps('sms')} />
164+
<ValidationMessage {...validationMessageProps} />
165+
</Fieldset>
166+
</>
167+
) : (
168+
<Button onClick={() => setOpen(true)} variant='secondary'>
169+
<PencilIcon /> Rediger
170+
</Button>
171+
)}
172+
</>
173+
);
174+
};

packages/react/src/utilities/hooks/useRadioGroup/useRadioGroup.ts

+23-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { useMergeRefs } from '@floating-ui/react';
2-
import { useEffect, useId, useRef, useState } from 'react';
1+
import { useId, useState } from 'react';
32
import type { ReactNode } from 'react';
43
import type { RadioProps } from '../../../components';
54

@@ -105,45 +104,49 @@ export function useRadioGroup({
105104
typeof propsOrValue === 'string'
106105
? { value: propsOrValue }
107106
: propsOrValue;
108-
const { ref = undefined, value = '', ...rest } = props;
109-
const localRef = useRef<HTMLInputElement>(null);
110-
const mergedRefs = useMergeRefs([ref, localRef]);
107+
const { ref: forwardedRef = undefined, value = '', ...rest } = props;
111108

112-
const handleChange = () => {
113-
const input = localRef.current;
114-
const isInput = input instanceof HTMLInputElement;
109+
const handleRef = (element: HTMLInputElement | null) => {
110+
if (element) {
111+
// Set initial checked state
112+
element.checked = value === groupValue;
113+
}
115114

116-
if (isInput && input.name === radioGroupName) {
115+
// Handle forwarded ref
116+
if (forwardedRef) {
117+
if (typeof forwardedRef === 'function') {
118+
forwardedRef(element);
119+
} else {
120+
forwardedRef.current = element;
121+
}
122+
}
123+
};
124+
125+
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
126+
if (e.target.name === radioGroupName) {
117127
setGroupValue((prevValue) => {
118-
onChange?.(input.value, prevValue);
119-
return input.value;
128+
onChange?.(e.target.value, prevValue);
129+
return e.target.value;
120130
});
121131
}
122132
};
123133

124-
useEffect(() => {
125-
if (!localRef.current) return;
126-
localRef.current.checked = value === groupValue;
127-
}, [groupValue, value]);
128-
129134
return {
130-
/* Spread anything the user has set first */
131135
...rest,
132-
/* Concat ours with the user prop */
133136
name: radioGroupName,
134137
'aria-describedby':
135138
`${error ? errorId : ''} ${rest['aria-describedby'] || ''}`.trim() ||
136139
undefined,
137140
'aria-invalid': !!error || rest['aria-invalid'],
138141
value,
139-
ref: mergedRefs,
142+
ref: handleRef,
140143
required: required || rest.required,
141144
readOnly: readOnly || rest.readOnly,
142145
disabled: disabled || rest.disabled,
143146
onChange: (e: React.ChangeEvent<HTMLInputElement>) => {
144147
rest.onChange?.(e);
145148
if (e.defaultPrevented) return;
146-
handleChange();
149+
handleChange(e);
147150
},
148151
};
149152
},

0 commit comments

Comments
 (0)