Skip to content

Commit

Permalink
Merge pull request #513 from yue4u/feat/allow-type-radio-group-value
Browse files Browse the repository at this point in the history
feat: allow type radio group value
  • Loading branch information
naporin0624 authored May 1, 2024
2 parents 8581694 + 40821c4 commit 8684ab8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
31 changes: 16 additions & 15 deletions packages/react/src/components/Radio/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ const LayoutDiv = styled.div`
flex-direction: column;
gap: ${({ theme }) => px(theme.spacing[24])};
`
const options = ['1', '2', '3'] as const
type Option = (typeof options)[number]

export const Default: StoryObj<typeof Radio> = {
render: function Render(args) {
const options = ['1', '2', '3']
const [value, setValue] = useState(options[0])
const [value, setValue] = useState<Option>(options[0])

return (
<LayoutDiv>
<RadioGroup
<RadioGroup<Option>
label={'label'}
name={'name'}
{...args}
Expand All @@ -45,11 +46,11 @@ export const Default: StoryObj<typeof Radio> = {

export const Disabled: StoryObj<typeof Radio> = {
render: function Render() {
const options = ['1', '2', '3']
const [value, setValue] = useState(options[0])
const [value, setValue] = useState<Option>(options[0])

return (
<LayoutDiv>
<RadioGroup
<RadioGroup<Option>
label={'label'}
name={'name'}
value={value}
Expand All @@ -69,11 +70,11 @@ export const Disabled: StoryObj<typeof Radio> = {

export const PartialDisabled: StoryObj<typeof Radio> = {
render: function Render() {
const options = ['1', '2', '3']
const [value, setValue] = useState(options[0])
const [value, setValue] = useState<Option>(options[0])

return (
<LayoutDiv>
<RadioGroup
<RadioGroup<Option>
label={'label'}
name={'name'}
value={value}
Expand All @@ -92,11 +93,11 @@ export const PartialDisabled: StoryObj<typeof Radio> = {

export const Readonly: StoryObj<typeof Radio> = {
render: function Render() {
const options = ['1', '2', '3']
const [value, setValue] = useState(options[0])
const [value, setValue] = useState<Option>(options[0])

return (
<LayoutDiv>
<RadioGroup
<RadioGroup<Option>
label={'label'}
name={'name'}
value={value}
Expand All @@ -116,11 +117,11 @@ export const Readonly: StoryObj<typeof Radio> = {

export const Invalid: StoryObj<typeof Radio> = {
render: function Render() {
const options = ['1', '2', '3']
const [value, setValue] = useState(options[0])
const [value, setValue] = useState<Option>(options[0])

return (
<LayoutDiv>
<RadioGroup
<RadioGroup<Option>
label={'label'}
name={'name'}
value={value}
Expand Down
27 changes: 14 additions & 13 deletions packages/react/src/components/Radio/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,17 @@ const RadioLabel = styled.div`
}
`

export type RadioGroupProps = React.PropsWithChildren<{
className?: string
value?: string
label: string
name: string
onChange(next: string): void
disabled?: boolean
readonly?: boolean
invalid?: boolean
}>
export type RadioGroupProps<Value extends string = string> =
React.PropsWithChildren<{
className?: string
value?: Value
label: string
name: string
onChange(next: Value): void
disabled?: boolean
readonly?: boolean
invalid?: boolean
}>

// TODO: use (or polyfill) flex gap
const StyledRadioGroup = styled.div`
Expand Down Expand Up @@ -209,7 +210,7 @@ const RadioGroupContext = React.createContext<RadioGroupContext>({
},
})

export function RadioGroup({
export function RadioGroup<Value extends string = string>({
className,
value,
label,
Expand All @@ -219,10 +220,10 @@ export function RadioGroup({
readonly,
invalid,
children,
}: RadioGroupProps) {
}: RadioGroupProps<Value>) {
const handleChange = useCallback(
(next: string) => {
onChange(next)
onChange(next as Value)
},
[onChange]
)
Expand Down

0 comments on commit 8684ab8

Please sign in to comment.