@faststore/ui 1.3.1 → 1.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/dist/molecules/RadioGroup/RadioGroup.d.ts +5 -0
- package/dist/molecules/RadioGroup/RadioOption.d.ts +19 -0
- package/dist/molecules/RadioGroup/index.d.ts +4 -0
- package/dist/molecules/RadioGroup/stories/RadioGroup.stories.d.ts +15 -0
- package/dist/molecules/RadioGroup/useRadioGroup.d.ts +17 -0
- package/package.json +4 -3
- package/src/molecules/RadioGroup/RadioGroup.test.tsx +105 -0
- package/src/molecules/RadioGroup/RadioGroup.tsx +25 -0
- package/src/molecules/RadioGroup/RadioOption.tsx +49 -0
- package/src/molecules/RadioGroup/index.tsx +11 -0
- package/src/molecules/RadioGroup/stories/RadioGroup.mdx +39 -0
- package/src/molecules/RadioGroup/stories/RadioGroup.stories.tsx +107 -0
- package/src/molecules/RadioGroup/useRadioGroup.ts +33 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,44 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## 1.3.5 (2021-11-25)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* **ui:** RadioGroup Component ([#1033](https://github.com/vtex/faststore/issues/1033)) ([60f306d](https://github.com/vtex/faststore/commit/60f306d97257a8cc65ec67bf1f1cc16376ad1654))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
## 1.3.4 (2021-11-23)
|
|
18
|
+
|
|
19
|
+
**Note:** Version bump only for package @faststore/ui
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
## 1.3.3 (2021-11-23)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
### Features
|
|
29
|
+
|
|
30
|
+
* **gatsby-plugin-nginx:** support to augment the generateRewrites ([#1017](https://github.com/vtex/faststore/issues/1017)) ([46587b0](https://github.com/vtex/faststore/commit/46587b0280d7f8dd733a0dca29909f041c944897))
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
## 1.3.2 (2021-11-22)
|
|
37
|
+
|
|
38
|
+
**Note:** Version bump only for package @faststore/ui
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
6
44
|
## 1.3.1 (2021-11-18)
|
|
7
45
|
|
|
8
46
|
**Note:** Version bump only for package @faststore/ui
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { PropsWithChildren } from 'react';
|
|
2
|
+
import { RadioGroupContext } from './useRadioGroup';
|
|
3
|
+
export declare type RadioGroupProps = PropsWithChildren<RadioGroupContext>;
|
|
4
|
+
declare const RadioGroup: ({ name, onChange, children, selectedValue, }: RadioGroupProps) => JSX.Element;
|
|
5
|
+
export default RadioGroup;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { InputHTMLAttributes } from 'react';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
export interface RadioOptionProps extends Omit<InputHTMLAttributes<HTMLInputElement>, 'type'> {
|
|
4
|
+
/**
|
|
5
|
+
* ID to find this component in testing tools (e.g.: cypress,
|
|
6
|
+
* testing-library, and jest).
|
|
7
|
+
*/
|
|
8
|
+
testId?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Attribute to set aria-label option as required.
|
|
11
|
+
*/
|
|
12
|
+
label: string;
|
|
13
|
+
/**
|
|
14
|
+
* Value to be emitted when radio is clicked.
|
|
15
|
+
*/
|
|
16
|
+
value: string | number;
|
|
17
|
+
}
|
|
18
|
+
declare const RadioOption: React.ForwardRefExoticComponent<RadioOptionProps & React.RefAttributes<HTMLInputElement>>;
|
|
19
|
+
export default RadioOption;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import type { Story } from '@storybook/react';
|
|
3
|
+
import type { RadioGroupProps, RadioOptionProps } from '..';
|
|
4
|
+
export declare const RadioGroupSimple: Story<RadioGroupProps & RadioOptionProps>;
|
|
5
|
+
export declare const RadioOptionWithChildren: Story<RadioGroupProps & RadioOptionProps>;
|
|
6
|
+
export declare const RadioGroupControlled: Story<RadioGroupProps & RadioOptionProps>;
|
|
7
|
+
declare const _default: {
|
|
8
|
+
title: string;
|
|
9
|
+
parameters: {
|
|
10
|
+
docs: {
|
|
11
|
+
page: (props: any) => JSX.Element;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
export default _default;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ChangeEventHandler } from 'react';
|
|
2
|
+
export interface RadioGroupContext {
|
|
3
|
+
/**
|
|
4
|
+
* Name to link children by context.
|
|
5
|
+
*/
|
|
6
|
+
name: string;
|
|
7
|
+
/**
|
|
8
|
+
* Value of checked child.
|
|
9
|
+
*/
|
|
10
|
+
selectedValue?: string | number;
|
|
11
|
+
/**
|
|
12
|
+
* Function that is triggered when any children is checked.
|
|
13
|
+
*/
|
|
14
|
+
onChange?: ChangeEventHandler<HTMLInputElement>;
|
|
15
|
+
}
|
|
16
|
+
export declare const RadioGroupContext: import("react").Context<RadioGroupContext | undefined>;
|
|
17
|
+
export declare function useRadioGroup(): RadioGroupContext;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faststore/ui",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.5",
|
|
4
4
|
"description": "A lightweight, framework agnostic component library for React",
|
|
5
5
|
"author": "emersonlaurentino",
|
|
6
6
|
"license": "MIT",
|
|
@@ -70,11 +70,12 @@
|
|
|
70
70
|
"@storybook/react": "^6.3.7",
|
|
71
71
|
"@testing-library/jest-dom": "^5.12.0",
|
|
72
72
|
"@testing-library/react": "^11.2.6",
|
|
73
|
+
"@testing-library/react-hooks": "^7.0.2",
|
|
73
74
|
"@testing-library/user-event": "^13.1.8",
|
|
74
75
|
"@types/jest-axe": "^3.5.3",
|
|
75
76
|
"@types/tabbable": "^3.1.1",
|
|
76
77
|
"@types/testing-library__jest-dom": "^5.9.5",
|
|
77
|
-
"@vtex/theme-b2c-tailwind": "^1.3.
|
|
78
|
+
"@vtex/theme-b2c-tailwind": "^1.3.5",
|
|
78
79
|
"@vtex/tsconfig": "^0.5.0",
|
|
79
80
|
"jest-axe": "^5.0.1",
|
|
80
81
|
"react": "^17.0.2",
|
|
@@ -85,5 +86,5 @@
|
|
|
85
86
|
"tsdx": "^0.14.1",
|
|
86
87
|
"typescript": "^4.2.4"
|
|
87
88
|
},
|
|
88
|
-
"gitHead": "
|
|
89
|
+
"gitHead": "44138a0db6f3d757a9428b6bcb3b0b5aab236616"
|
|
89
90
|
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { render } from '@testing-library/react'
|
|
2
|
+
import { renderHook } from '@testing-library/react-hooks'
|
|
3
|
+
import userEvent from '@testing-library/user-event'
|
|
4
|
+
import type { ReactNode } from 'react'
|
|
5
|
+
import React from 'react'
|
|
6
|
+
|
|
7
|
+
import { RadioGroup, RadioOption, useRadioGroup } from '.'
|
|
8
|
+
|
|
9
|
+
describe('RadioGroup', () => {
|
|
10
|
+
it('Should render radio group with radio option', () => {
|
|
11
|
+
const onChange = jest.fn()
|
|
12
|
+
|
|
13
|
+
const { getByTestId, getByRole } = render(
|
|
14
|
+
<RadioGroup name="radio-group" selectedValue="radio" onChange={onChange}>
|
|
15
|
+
<RadioOption value="radio" label="Radio 1" />
|
|
16
|
+
</RadioGroup>
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
const radio = getByRole('radio', { name: 'Radio 1' })
|
|
20
|
+
|
|
21
|
+
expect(radio).toBeInTheDocument()
|
|
22
|
+
expect(getByTestId('store-radio-option')).toBeInTheDocument()
|
|
23
|
+
})
|
|
24
|
+
it('Should render RadioOption Children', () => {
|
|
25
|
+
const onChange = jest.fn()
|
|
26
|
+
const { getByRole } = render(
|
|
27
|
+
<RadioGroup
|
|
28
|
+
name="radio-group"
|
|
29
|
+
selectedValue="radio-1"
|
|
30
|
+
onChange={onChange}
|
|
31
|
+
>
|
|
32
|
+
<RadioOption value="radio-1" label="Radio 1">
|
|
33
|
+
<h1>Radio Group 1</h1>
|
|
34
|
+
</RadioOption>
|
|
35
|
+
</RadioGroup>
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
expect(getByRole('heading')).toHaveTextContent('Radio Group 1')
|
|
39
|
+
})
|
|
40
|
+
it('Should pass name from RadioGroup to RadioOptions', () => {
|
|
41
|
+
const onChange = jest.fn()
|
|
42
|
+
|
|
43
|
+
const { container } = render(
|
|
44
|
+
<RadioGroup
|
|
45
|
+
name="radio-group"
|
|
46
|
+
selectedValue="radio-1"
|
|
47
|
+
onChange={onChange}
|
|
48
|
+
>
|
|
49
|
+
<RadioOption value="radio-1" label="Radio 1" />
|
|
50
|
+
<RadioOption value="radio-2" label="Radio 2" />
|
|
51
|
+
</RadioGroup>
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
const options = container.querySelectorAll('[name="radio-group"]')
|
|
55
|
+
|
|
56
|
+
expect(options).toHaveLength(2)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('Should emit onChange with right value', () => {
|
|
60
|
+
const onChange = jest.fn()
|
|
61
|
+
|
|
62
|
+
const { getByTestId } = render(
|
|
63
|
+
<RadioGroup
|
|
64
|
+
name="radio-group"
|
|
65
|
+
selectedValue="radio-1"
|
|
66
|
+
onChange={onChange}
|
|
67
|
+
>
|
|
68
|
+
<RadioOption value="radio-1" label="Radio 1">
|
|
69
|
+
Radio 1
|
|
70
|
+
</RadioOption>
|
|
71
|
+
<RadioOption
|
|
72
|
+
value="radio-2"
|
|
73
|
+
label="Radio 2"
|
|
74
|
+
testId="store-radio2-option"
|
|
75
|
+
>
|
|
76
|
+
Radio 2
|
|
77
|
+
</RadioOption>
|
|
78
|
+
</RadioGroup>
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
const radio2 = getByTestId('store-radio2-option')
|
|
82
|
+
|
|
83
|
+
userEvent.click(radio2)
|
|
84
|
+
|
|
85
|
+
const [[event]] = onChange.mock.calls
|
|
86
|
+
|
|
87
|
+
expect(onChange).toHaveBeenCalledTimes(1)
|
|
88
|
+
expect(event.target.value).toEqual('radio-2')
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('Should render useRadioGroup hook', () => {
|
|
92
|
+
const WrapperRadioGroup = ({ children }: { children: ReactNode }) => (
|
|
93
|
+
<RadioGroup name="radio-group" selectedValue="radio-1">
|
|
94
|
+
{children}
|
|
95
|
+
</RadioGroup>
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
const { result } = renderHook(() => useRadioGroup(), {
|
|
99
|
+
wrapper: WrapperRadioGroup,
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
expect(result.current.name).toEqual('radio-group')
|
|
103
|
+
expect(result.current.selectedValue).toEqual('radio-1')
|
|
104
|
+
})
|
|
105
|
+
})
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { PropsWithChildren } from 'react'
|
|
2
|
+
import React from 'react'
|
|
3
|
+
|
|
4
|
+
import { RadioGroupContext } from './useRadioGroup'
|
|
5
|
+
|
|
6
|
+
export type RadioGroupProps = PropsWithChildren<RadioGroupContext>
|
|
7
|
+
|
|
8
|
+
const RadioGroup = ({
|
|
9
|
+
name,
|
|
10
|
+
onChange,
|
|
11
|
+
children,
|
|
12
|
+
selectedValue,
|
|
13
|
+
}: RadioGroupProps) => {
|
|
14
|
+
const contextValues = React.useMemo(() => {
|
|
15
|
+
return { name, selectedValue, onChange }
|
|
16
|
+
}, [name, selectedValue, onChange])
|
|
17
|
+
|
|
18
|
+
return (
|
|
19
|
+
<RadioGroupContext.Provider value={contextValues}>
|
|
20
|
+
{children}
|
|
21
|
+
</RadioGroupContext.Provider>
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default RadioGroup
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { InputHTMLAttributes } from 'react'
|
|
2
|
+
import React, { forwardRef } from 'react'
|
|
3
|
+
|
|
4
|
+
import { Radio } from '../..'
|
|
5
|
+
import { useRadioGroup } from './useRadioGroup'
|
|
6
|
+
|
|
7
|
+
export interface RadioOptionProps
|
|
8
|
+
extends Omit<InputHTMLAttributes<HTMLInputElement>, 'type'> {
|
|
9
|
+
/**
|
|
10
|
+
* ID to find this component in testing tools (e.g.: cypress,
|
|
11
|
+
* testing-library, and jest).
|
|
12
|
+
*/
|
|
13
|
+
testId?: string
|
|
14
|
+
/**
|
|
15
|
+
* Attribute to set aria-label option as required.
|
|
16
|
+
*/
|
|
17
|
+
label: string
|
|
18
|
+
/**
|
|
19
|
+
* Value to be emitted when radio is clicked.
|
|
20
|
+
*/
|
|
21
|
+
value: string | number
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const RadioOption = forwardRef<HTMLInputElement, RadioOptionProps>(
|
|
25
|
+
function RadioOption(
|
|
26
|
+
{ label, value, children, testId = 'store-radio-option', ...otherProps },
|
|
27
|
+
ref
|
|
28
|
+
) {
|
|
29
|
+
const { name, selectedValue, onChange } = useRadioGroup()
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<label aria-label={label} data-store-radio-option>
|
|
33
|
+
<Radio
|
|
34
|
+
data-store-radio-option-item
|
|
35
|
+
ref={ref}
|
|
36
|
+
name={name}
|
|
37
|
+
checked={value === selectedValue}
|
|
38
|
+
onChange={onChange}
|
|
39
|
+
value={value}
|
|
40
|
+
testId={testId}
|
|
41
|
+
{...otherProps}
|
|
42
|
+
/>
|
|
43
|
+
{children}
|
|
44
|
+
</label>
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
export default RadioOption
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import RadioGroup, { RadioGroupProps } from './RadioGroup'
|
|
2
|
+
import RadioOption, { RadioOptionProps } from './RadioOption'
|
|
3
|
+
import { useRadioGroup } from './useRadioGroup'
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
RadioGroup,
|
|
7
|
+
RadioOption,
|
|
8
|
+
useRadioGroup,
|
|
9
|
+
RadioGroupProps,
|
|
10
|
+
RadioOptionProps,
|
|
11
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Canvas, Props, Story, ArgsTable } from '@storybook/addon-docs'
|
|
2
|
+
|
|
3
|
+
import RadioGroup from '../RadioGroup.tsx'
|
|
4
|
+
import RadioOption from '../RadioOption.tsx'
|
|
5
|
+
|
|
6
|
+
# Simple RadioGroup
|
|
7
|
+
|
|
8
|
+
<Canvas>
|
|
9
|
+
<Story id="molecules-radiogroup--radio-group-simple" />
|
|
10
|
+
</Canvas>
|
|
11
|
+
|
|
12
|
+
# RadioOption With Children
|
|
13
|
+
|
|
14
|
+
<Canvas>
|
|
15
|
+
<Story id="molecules-radiogroup--radio-option-with-children" />
|
|
16
|
+
</Canvas>
|
|
17
|
+
|
|
18
|
+
# Controlled RadioGroup
|
|
19
|
+
|
|
20
|
+
<Canvas>
|
|
21
|
+
<Story id="molecules-radiogroup--radio-group-controlled" />
|
|
22
|
+
</Canvas>
|
|
23
|
+
|
|
24
|
+
## Props
|
|
25
|
+
|
|
26
|
+
#### RadioGroup Props
|
|
27
|
+
|
|
28
|
+
<ArgsTable of={RadioGroup} />
|
|
29
|
+
|
|
30
|
+
#### RadioOption Props
|
|
31
|
+
|
|
32
|
+
<ArgsTable of={RadioOption} />
|
|
33
|
+
|
|
34
|
+
## CSS Selectors
|
|
35
|
+
|
|
36
|
+
```css
|
|
37
|
+
[data-store-radio-option] {}
|
|
38
|
+
[data-store-radio-option-item] {}
|
|
39
|
+
```
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import React, { useState } from 'react'
|
|
2
|
+
import type { Story } from '@storybook/react'
|
|
3
|
+
|
|
4
|
+
import type { RadioGroupProps, RadioOptionProps } from '..'
|
|
5
|
+
import { RadioGroup, RadioOption } from '..'
|
|
6
|
+
import mdx from './RadioGroup.mdx'
|
|
7
|
+
|
|
8
|
+
export const RadioGroupSimple: Story<RadioGroupProps & RadioOptionProps> = (
|
|
9
|
+
args
|
|
10
|
+
) => {
|
|
11
|
+
const [option, setOption] = useState<string | number>('')
|
|
12
|
+
|
|
13
|
+
return (
|
|
14
|
+
<>
|
|
15
|
+
<RadioGroup
|
|
16
|
+
{...args}
|
|
17
|
+
name="radio-group"
|
|
18
|
+
onChange={(v) => {
|
|
19
|
+
setOption(v.currentTarget.value)
|
|
20
|
+
}}
|
|
21
|
+
selectedValue={option}
|
|
22
|
+
>
|
|
23
|
+
<RadioOption value="radio-1" label="Radio 1">
|
|
24
|
+
<span>Radio 1</span>
|
|
25
|
+
</RadioOption>
|
|
26
|
+
<RadioOption value="radio-2" label="Radio 2">
|
|
27
|
+
<span>Radio 2</span>
|
|
28
|
+
</RadioOption>
|
|
29
|
+
</RadioGroup>
|
|
30
|
+
</>
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const RadioOptionWithChildren: Story<
|
|
35
|
+
RadioGroupProps & RadioOptionProps
|
|
36
|
+
> = (args) => {
|
|
37
|
+
const [option, setOption] = useState<string | number>('')
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<>
|
|
41
|
+
<RadioGroup
|
|
42
|
+
{...args}
|
|
43
|
+
name="radio-group-with-children"
|
|
44
|
+
onChange={(v) => {
|
|
45
|
+
setOption(v.currentTarget.value)
|
|
46
|
+
}}
|
|
47
|
+
selectedValue={option}
|
|
48
|
+
>
|
|
49
|
+
<RadioOption value="radio-1" label="Radio 1">
|
|
50
|
+
<div>Radio 1</div>
|
|
51
|
+
</RadioOption>
|
|
52
|
+
<RadioOption value="radio-2" label="Radio 2">
|
|
53
|
+
<div>Radio 2</div>
|
|
54
|
+
</RadioOption>
|
|
55
|
+
<RadioOption value="radio-3" label="Radio 3">
|
|
56
|
+
<div>Radio 3</div>
|
|
57
|
+
</RadioOption>
|
|
58
|
+
</RadioGroup>
|
|
59
|
+
</>
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export const RadioGroupControlled: Story<RadioGroupProps & RadioOptionProps> = (
|
|
64
|
+
args
|
|
65
|
+
) => {
|
|
66
|
+
const [option, setOption] = useState<string | number>('')
|
|
67
|
+
|
|
68
|
+
return (
|
|
69
|
+
<>
|
|
70
|
+
<RadioGroup
|
|
71
|
+
{...args}
|
|
72
|
+
name="radio-group-controlled"
|
|
73
|
+
onChange={(v) => {
|
|
74
|
+
setOption(v.currentTarget.value)
|
|
75
|
+
}}
|
|
76
|
+
selectedValue={option}
|
|
77
|
+
>
|
|
78
|
+
<RadioOption value="radio-1" label="Radio 1">
|
|
79
|
+
<span>Radio 1</span>
|
|
80
|
+
</RadioOption>
|
|
81
|
+
<RadioOption value="radio-2" label="Radio 2">
|
|
82
|
+
<span>Radio 2</span>
|
|
83
|
+
</RadioOption>
|
|
84
|
+
<RadioOption value="radio-3" label="Radio 3">
|
|
85
|
+
<span>Radio 3</span>
|
|
86
|
+
</RadioOption>
|
|
87
|
+
</RadioGroup>
|
|
88
|
+
<br />
|
|
89
|
+
<button
|
|
90
|
+
onClick={() => {
|
|
91
|
+
setOption('radio-3')
|
|
92
|
+
}}
|
|
93
|
+
>
|
|
94
|
+
Check Radio 3
|
|
95
|
+
</button>
|
|
96
|
+
</>
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export default {
|
|
101
|
+
title: 'Molecules/RadioGroup',
|
|
102
|
+
parameters: {
|
|
103
|
+
docs: {
|
|
104
|
+
page: mdx,
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ChangeEventHandler } from 'react'
|
|
2
|
+
import { createContext, useContext } from 'react'
|
|
3
|
+
|
|
4
|
+
export interface RadioGroupContext {
|
|
5
|
+
/**
|
|
6
|
+
* Name to link children by context.
|
|
7
|
+
*/
|
|
8
|
+
name: string
|
|
9
|
+
/**
|
|
10
|
+
* Value of checked child.
|
|
11
|
+
*/
|
|
12
|
+
selectedValue?: string | number
|
|
13
|
+
/**
|
|
14
|
+
* Function that is triggered when any children is checked.
|
|
15
|
+
*/
|
|
16
|
+
onChange?: ChangeEventHandler<HTMLInputElement>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const RadioGroupContext = createContext<RadioGroupContext | undefined>(
|
|
20
|
+
undefined
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
export function useRadioGroup() {
|
|
24
|
+
const context = useContext(RadioGroupContext)
|
|
25
|
+
|
|
26
|
+
if (!context) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
`useRadioOption hook cannot be used outside the RadioGroup context`
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return context
|
|
33
|
+
}
|