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: Checkbox add click event #6235

Merged
merged 2 commits into from
Jul 5, 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
2 changes: 2 additions & 0 deletions src/components/checkbox/checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type CheckboxProps = {
id?: string
icon?: (checked: boolean, indeterminate: 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 @@ -110,6 +111,7 @@ export const Checkbox = forwardRef<CheckboxRef, CheckboxProps>((p, ref) => {
return withNativeProps(
props,
<label
onClick={props.onClick}
className={classNames(classPrefix, {
[`${classPrefix}-checked`]: checked && !props.indeterminate,
[`${classPrefix}-indeterminate`]: props.indeterminate,
Expand Down
1 change: 1 addition & 0 deletions src/components/checkbox/index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type CheckboxValue = string | number
| id | The id of the input element, often used in conjunction with the label | `string` | - |
| indeterminate | To set the `indeterminate` state, only responsible for style control | `boolean` | `false` |
| onChange | Callback function when changing | `(val: boolean) => void` | - |
| onClick | Checkbox's click event | `(event: React.MouseEvent<HTMLLabelElement, MouseEvent>) => void` | - |
| value | The value carrying identification, used in `Group` mode | `CheckboxValue` | - |

### Ref
Expand Down
1 change: 1 addition & 0 deletions src/components/checkbox/index.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type CheckboxValue = string | number
| id | `input` 元素的 `id`,常用来配合 `label` 使用 | `string` | - |
| indeterminate | 设置 `indeterminate` 状态,只负责样式控制 | `boolean` | `false` |
| onChange | 变化时回调函数 | `(val: boolean) => void` | - |
| onClick | Checkbox 的点击事件 | `(event: React.MouseEvent<HTMLLabelElement, MouseEvent>) => void` | - |
| value | 携带的标识值,用于 `Group` 模式 | `CheckboxValue` | - |

### Ref
Expand Down
9 changes: 9 additions & 0 deletions src/components/checkbox/tests/checkbox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@ describe('Checkbox', () => {
expect(input).not.toBeChecked()
expect(checkbox).not.toHaveClass(`${classPrefix}-checked`)
})
test('Checkbox click', () => {
const handleClick = jest.fn()
const { container } = render(
<Checkbox onClick={handleClick}>click</Checkbox>
)
const checkbox = container.getElementsByTagName('label')[0]

fireEvent.click(checkbox)
expect(handleClick).toHaveBeenCalledTimes(1)
})
test('renders with disabled', async () => {
const { container } = render(<Checkbox disabled>Checkbox</Checkbox>)
const input = container.querySelectorAll('input')[0]
Expand Down
Loading