Skip to content
Open
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
29 changes: 29 additions & 0 deletions src/Checkbox/Checkbox.test.tsx

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ほぼclsxのテストなので若干過剰か?とは思ったりした

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かに過剰です!修正します

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { renderToStaticMarkup } from 'react-dom/server'
import { describe, expect, it } from 'vitest'

import { Checkbox } from './index'

/** SSR 出力からルートの label の class 属性をクラス名の配列として取り出す */
const getRootLabelClasses = (html: string): string[] => {
const className = html.match(/^<label[^>]*\bclass="([^"]*)"/)?.[1]
if (className === undefined) {
throw new Error(`ルートの label に class 属性が見つかりません: ${html}`)
}
return className.split(' ')
}

describe('Checkbox', () => {
it('渡した className がルートの label にマージされる', () => {
const html = renderToStaticMarkup(<Checkbox className="my-card">ラベル</Checkbox>)
const classes = getRootLabelClasses(html)

expect(classes).toContain('aria-checkbox')
expect(classes).toContain('my-card')
})

it('className を渡さない場合は既存のクラスのみが付く', () => {
const html = renderToStaticMarkup(<Checkbox>ラベル</Checkbox>)

expect(getRootLabelClasses(html)).toEqual(['aria-checkbox'])
})
})
4 changes: 2 additions & 2 deletions src/Checkbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const useCheckboxState = (ref: React.RefObject<HTMLLabelElement | null>) => {
*
* @summary 複数選択用のチェックボックス
*/
const Checkbox = ({ children, ...props }: CheckboxProps) => {
const Checkbox = ({ children, className, ...props }: CheckboxProps) => {
const ariaRef = useRef<HTMLLabelElement>(null)
const { isSelected, isIndeterminate, isDisabled, isHovered, isFocused } =
useCheckboxState(ariaRef)
Expand All @@ -78,7 +78,7 @@ const Checkbox = ({ children, ...props }: CheckboxProps) => {
})

return (
<AriaCheckbox {...props} ref={ariaRef} className="aria-checkbox">
<AriaCheckbox {...props} ref={ariaRef} className={clsx('aria-checkbox', className)}>
<div className={checkboxClasses} />
{children && (
<Typography component="span" className={labelClasses}>
Expand Down
Loading