Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: Radio add click event #6228

Merged
merged 3 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/radio/index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type RadioValue = string | number
| icon | Customized `icon` icon | `(checked: boolean) => React.ReactNode` | - |
| id | The id of the input element, often used in conjunction with the label | `string` | - |
| onChange | Callback function when checked is changed | `(val: boolean) => void` | - |
| onClick | Radio's click event | `(event: React.MouseEvent<HTMLLabelElement, MouseEvent>) => void` | - |
| value | Value is carrying identification, used in `Group` mode | `RadioValue` | - |

### Radio.Group
Expand Down
1 change: 1 addition & 0 deletions src/components/radio/index.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type RadioValue = string | number
| icon | 自定义 `icon` 图标 | `(checked: boolean) => React.ReactNode` | - |
| id | `input` 元素的 `id`,常用来配合 `label` 使用 | `string` | - |
| onChange | 变化时回调函数 | `(val: boolean) => void` | - |
| onClick | Radio 的点击事件 | `(event: React.MouseEvent<HTMLLabelElement, MouseEvent>) => void` | - |
| value | 携带的标识值,用于 `Group` 模式 | `RadioValue` | - |

### Radio.Group
Expand Down
2 changes: 2 additions & 0 deletions src/components/radio/radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type RadioProps = {
id?: string
icon?: (checked: boolean) => React.ReactNode
children?: React.ReactNode
onClick?: (event: React.MouseEvent<HTMLLabelElement, MouseEvent>) => void
} & NativeProps<'--icon-size' | '--font-size' | '--gap'>

const defaultProps = {
Expand Down Expand Up @@ -86,6 +87,7 @@ export const Radio: FC<RadioProps> = p => {
return withNativeProps(
props,
<label
onClick={props.onClick}
1587315093 marked this conversation as resolved.
Show resolved Hide resolved
className={classNames(classPrefix, {
[`${classPrefix}-checked`]: checked,
[`${classPrefix}-disabled`]: disabled,
Expand Down
10 changes: 9 additions & 1 deletion src/components/radio/tests/radio.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react'
import { fireEvent, render, testA11y, userEvent, screen } from 'testing'
import Radio from '../'
import { RadioGroupProps } from '../group'

const classPrefix = `adm-radio`

Expand All @@ -22,7 +21,16 @@ describe('Radio', () => {
expect(radio).toBeChecked()
expect(label).toHaveClass(`${classPrefix}-checked`)
})
test('Radio click ', async () => {
const handleClick = jest.fn()
render(<Radio onClick={handleClick}>Radio</Radio>)

const label = document.getElementsByTagName('label')[0]
expect(label).toHaveClass(`${classPrefix}`)

fireEvent.click(label)
expect(handleClick).toBeCalledTimes(1)
})
test('onChange should be call once when the selected item is clicked multiple times', async () => {
const onChange = jest.fn()
render(
Expand Down
Loading