-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRadioProvider.tsx
More file actions
31 lines (27 loc) · 952 Bytes
/
RadioProvider.tsx
File metadata and controls
31 lines (27 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import type { ReactNode } from 'react';
import { useId, useMemo, useState } from 'react';
import { RadioContext } from '../context';
import { RADIO_NAME_PREFIX } from '../constants';
import type { RadioGroupProps, ReturnContext } from '../types';
interface RadioProviderProps {
defaultValue?: string;
children: ReactNode;
rootProps?: RadioGroupProps;
}
export const RadioProvider = ({ ...props }: RadioProviderProps) => {
const { children, defaultValue, rootProps } = props;
const uuid = useId();
const defaultName = `${RADIO_NAME_PREFIX}_${uuid}`;
const [value, setValue] = useState<string>('');
const providerValue = useMemo(
() => ({
setValue,
value,
defaultValue,
name: defaultName,
rootProps: { ...rootProps }
}),
[value, setValue, defaultValue, defaultName, rootProps]
);
return <RadioContext.Provider value={providerValue as ReturnContext}>{children}</RadioContext.Provider>;
};