-
Notifications
You must be signed in to change notification settings - Fork 142
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: CheckBoxの内部処理を最適化する #5340
Open
AtsushiM
wants to merge
11
commits into
master
Choose a base branch
from
chore-refactoring-CheckBox
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+52
−51
Open
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ec51f70
chore: CheckBox内のhandleOnClickは無駄なmemo化だったのでonClickを直接利用するようにする
AtsushiM 802fe1e
chore: CheckBoxのAriaHiddenBoxを切り出す
AtsushiM 719bd53
chore: CheckBoxのstylesをmemo化する
AtsushiM d9cbf26
chore: CheckBoxのCheckIconAreaを切り出す
AtsushiM 1bdb608
chore: CheckBoxのdisabledによるstyle変更を調整
AtsushiM 904d315
chore: CheckBoxのLabeledChildrenを切り出す
AtsushiM d9400a9
chore: CheckBoxのdisabledの調整
AtsushiM 969edea
chore: style -> className
AtsushiM e919ec1
Merge branch 'master' of https://github.com/kufu/smarthr-ui into chor…
AtsushiM b7bbe2b
Merge branch 'master' of https://github.com/kufu/smarthr-ui into chor…
AtsushiM b139c4b
chore: CheckboxのclassName生成を調整
AtsushiM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
'use client' | ||
|
||
import React, { | ||
ChangeEventHandler, | ||
ComponentPropsWithRef, | ||
PropsWithChildren, | ||
forwardRef, | ||
useCallback, | ||
useEffect, | ||
useId, | ||
useImperativeHandle, | ||
|
@@ -59,48 +57,29 @@ const checkbox = tv({ | |
'shr-relative shr-box-border shr-inline-block shr-h-[theme(fontSize.base)] shr-w-[theme(fontSize.base)] shr-shrink-0 shr-translate-y-[0.125em] shr-leading-none', | ||
label: [ | ||
'smarthr-ui-CheckBox-label shr-ms-0.5 shr-cursor-pointer shr-text-base shr-leading-tight', | ||
'[[data-disabled=true]>&]:shr-pointer-events-none [[data-disabled=true]>&]:shr-cursor-not-allowed [[data-disabled=true]>&]:shr-text-disabled', | ||
], | ||
}, | ||
variants: { | ||
disabled: { | ||
true: { | ||
label: 'shr-pointer-events-none shr-cursor-not-allowed shr-text-disabled', | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
export const CheckBox = forwardRef<HTMLInputElement, Props>( | ||
({ checked, mixed = false, error, onChange, className, children, ...props }, ref) => { | ||
const { | ||
wrapperStyle, | ||
innerWrapperStyle, | ||
boxStyle, | ||
inputStyle, | ||
iconWrapStyle, | ||
iconStyle, | ||
labelStyle, | ||
} = useMemo(() => { | ||
({ checked, mixed, error, className, children, disabled, ...props }, ref) => { | ||
const styles = useMemo(() => { | ||
const { wrapper, innerWrapper, box, input, iconWrap, icon, label } = checkbox() | ||
|
||
return { | ||
wrapperStyle: wrapper({ className }), | ||
innerWrapperStyle: innerWrapper(), | ||
boxStyle: box(), | ||
inputStyle: input(), | ||
iconWrapStyle: iconWrap(), | ||
iconStyle: icon(), | ||
labelStyle: label({ disabled: props.disabled }), | ||
wrapper: wrapper({ className }), | ||
innerWrapper: innerWrapper(), | ||
box: box(), | ||
input: input(), | ||
iconWrap: iconWrap(), | ||
icon: icon(), | ||
label: label(), | ||
} | ||
}, [className, props.disabled]) | ||
|
||
const handleChange = useCallback<ChangeEventHandler<HTMLInputElement>>( | ||
(e) => { | ||
if (onChange) onChange(e) | ||
}, | ||
[onChange], | ||
) | ||
}, [className]) | ||
|
||
const inputRef = useRef<HTMLInputElement>(null) | ||
|
||
useImperativeHandle<HTMLInputElement | null, HTMLInputElement | null>( | ||
ref, | ||
() => inputRef.current, | ||
|
@@ -116,31 +95,48 @@ export const CheckBox = forwardRef<HTMLInputElement, Props>( | |
const checkBoxId = props.id || defaultId | ||
|
||
return ( | ||
<span className={wrapperStyle}> | ||
<span className={innerWrapperStyle}> | ||
<span data-disabled={disabled?.toString()} className={styles.wrapper}> | ||
<span className={styles.innerWrapper}> | ||
<input | ||
{...props} | ||
data-smarthr-ui-input="true" | ||
ref={inputRef} | ||
type="checkbox" | ||
id={checkBoxId} | ||
checked={checked} | ||
onChange={handleChange} | ||
className={inputStyle} | ||
ref={inputRef} | ||
disabled={disabled} | ||
aria-invalid={error || undefined} | ||
className={styles.input} | ||
data-smarthr-ui-input="true" | ||
/> | ||
<span className={boxStyle} aria-hidden="true" /> | ||
<span className={iconWrapStyle}> | ||
{mixed ? <FaMinusIcon className={iconStyle} /> : <FaCheckIcon className={iconStyle} />} | ||
</span> | ||
<AriaHiddenBox className={styles.box} /> | ||
<CheckIconArea mixed={mixed} className={styles.iconWrap} iconStyle={styles.icon} /> | ||
</span> | ||
|
||
{children && ( | ||
<label className={labelStyle} htmlFor={checkBoxId}> | ||
{children} | ||
</label> | ||
)} | ||
<LabeledChildren htmlFor={checkBoxId} className={styles.label}> | ||
{children} | ||
</LabeledChildren> | ||
</span> | ||
) | ||
}, | ||
) | ||
|
||
const AriaHiddenBox = React.memo<{ className: string }>(({ className }) => ( | ||
<span className={className} aria-hidden="true" /> | ||
)) | ||
|
||
const CheckIconArea = React.memo<Pick<Props, 'mixed'> & { className: string; iconStyle: string }>( | ||
({ mixed, className, iconStyle }) => ( | ||
<span className={className}> | ||
{mixed ? <FaMinusIcon className={iconStyle} /> : <FaCheckIcon className={iconStyle} />} | ||
</span> | ||
), | ||
) | ||
|
||
const LabeledChildren = React.memo<PropsWithChildren<{ className: string; htmlFor: string }>>( | ||
({ children, htmlFor, className }) => | ||
children && ( | ||
<label htmlFor={htmlFor} className={className}> | ||
{children} | ||
</label> | ||
), | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. これらの箇所はほぼ書き換わる可能性がないため、memo化しています。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
動的にclassを変更するのではなく、親要素の
data-disabled
を確認、それに応じてstyleを変更するようにしました。これにより、memo化の効率が良くなります
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q:
data-disabled
よりhas-[input:disabled]
のほうがsmarthr-ui内ではよく使われているかなと思ったんですが、data属性の方を選んだ理由ってありますか?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Qs-F
これややこしいんですが、labelはinputを内包していないんですよ。
構造的には
となっています。