mirror of
https://github.com/zhigang1992/react.git
synced 2026-04-26 23:04:55 +08:00
Merge pull request #271 from unix/radio
feat(radio): support for number type
This commit is contained in:
@@ -61,6 +61,28 @@ describe('Radio Group', () => {
|
||||
changeHandler.mockRestore()
|
||||
})
|
||||
|
||||
it('the radio value should be support number', () => {
|
||||
let value = ''
|
||||
const changeHandler = jest.fn().mockImplementation(val => (value = val))
|
||||
const wrapper = mount(
|
||||
<Radio.Group onChange={changeHandler}>
|
||||
<Radio value={5}>Option 1</Radio>
|
||||
<Radio value={10}>Option 2</Radio>
|
||||
</Radio.Group>,
|
||||
)
|
||||
|
||||
wrapper
|
||||
.find('input')
|
||||
.at(0)
|
||||
.simulate('change', {
|
||||
...nativeEvent,
|
||||
target: { checked: true },
|
||||
})
|
||||
expect(changeHandler).toHaveBeenCalled()
|
||||
expect(value).toEqual(5)
|
||||
changeHandler.mockRestore()
|
||||
})
|
||||
|
||||
it('should ignore events when disabled', () => {
|
||||
const changeHandler = jest.fn()
|
||||
const wrapper = mount(
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import React from 'react'
|
||||
|
||||
export interface RadioConfig {
|
||||
updateState?: (value: string) => void
|
||||
updateState?: (value: string | number) => void
|
||||
disabledAll: boolean
|
||||
value?: string
|
||||
value?: string | number
|
||||
inGroup: boolean
|
||||
}
|
||||
|
||||
|
||||
@@ -4,11 +4,11 @@ import { RadioContext } from './radio-context'
|
||||
import { NormalSizes } from 'components/utils/prop-types'
|
||||
|
||||
interface Props {
|
||||
value?: string
|
||||
initialValue?: string
|
||||
value?: string | number
|
||||
initialValue?: string | number
|
||||
disabled?: boolean
|
||||
size?: NormalSizes
|
||||
onChange?: (value: string) => void
|
||||
onChange?: (value: string | number) => void
|
||||
className?: string
|
||||
useRow?: boolean
|
||||
}
|
||||
@@ -44,8 +44,8 @@ const RadioGroup: React.FC<React.PropsWithChildren<RadioGroupProps>> = ({
|
||||
useRow,
|
||||
...props
|
||||
}) => {
|
||||
const [selfVal, setSelfVal] = useState<string | undefined>(initialValue)
|
||||
const updateState = (nextValue: string) => {
|
||||
const [selfVal, setSelfVal] = useState<string | number | undefined>(initialValue)
|
||||
const updateState = (nextValue: string | number) => {
|
||||
setSelfVal(nextValue)
|
||||
onChange && onChange(nextValue)
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ export interface RadioEvent {
|
||||
|
||||
interface Props {
|
||||
checked?: boolean
|
||||
value?: string
|
||||
value?: string | number
|
||||
size?: NormalSizes
|
||||
className?: string
|
||||
disabled?: boolean
|
||||
@@ -77,7 +77,7 @@ const Radio: React.FC<React.PropsWithChildren<RadioProps>> = ({
|
||||
}
|
||||
setSelfChecked(!selfChecked)
|
||||
if (inGroup) {
|
||||
updateState && updateState(radioValue as string)
|
||||
updateState && updateState(radioValue as string | number)
|
||||
}
|
||||
onChange && onChange(selfEvent)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user