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

chore: RadioButtonPanelの内部ロジックをリファクタリングする #5267

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import React, { ComponentProps, useMemo, useRef } from 'react'
import React, { ComponentProps, useCallback, useMemo, useRef } from 'react'
import { tv } from 'tailwind-variants'

import { Base } from '../Base'
Expand All @@ -10,7 +10,7 @@ type Props = ComponentProps<typeof RadioButton> & {
as?: string | React.ComponentType<any>
}

const MEANLESS_TAG_NAMES = ['div', 'span']
const MEANLESS_TAG_NAME_REGEX = /^(div|span)$/
Copy link
Member Author

Choose a reason for hiding this comment

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

配列と比較チェックするより、正規表現で一発でチェックするほうが高速のため、調整しました。
正規表現にすると読みづらくなる場合もありますが、今回は許容範囲かな、と思います


const radioButtonPanel = tv({
base: [
Expand All @@ -32,24 +32,22 @@ const radioButtonPanel = tv({
export const RadioButtonPanel: React.FC<Props> = ({ onClick, as, className, ...props }) => {
const styles = useMemo(
() => radioButtonPanel({ disabled: !!props.disabled, className }),
[className, props.disabled],
[props.disabled, className],
)
const role = useMemo(
() => (MEANLESS_TAG_NAME_REGEX.test(as ? `${as}` : '') ? 'presentation' : undefined),
[as],
)

// 外側の装飾を押しても内側のラジオボタンが押せるようにする
const innerRef = useRef<HTMLInputElement>(null)
const handleOuterClick = () => {
const handleOuterClick = useCallback(() => {
innerRef.current?.click()
}
}, [innerRef.current])

return (
// eslint-disable-next-line smarthr/a11y-delegate-element-has-role-presentation
<Base
padding={1}
role={MEANLESS_TAG_NAMES.includes(`${as || ''}`) ? 'presentation' : undefined}
onClick={handleOuterClick}
as={as}
className={styles}
>
<Base padding={1} role={role} onClick={handleOuterClick} as={as} className={styles}>
<RadioButton {...props} ref={innerRef} />
</Base>
)
Expand Down
Loading