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: CheckBoxの内部処理を最適化する #5340

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

AtsushiM
Copy link
Member

関連URL

概要

変更内容

確認方法

@yagimushi yagimushi force-pushed the chore-refactoring-CheckBox branch from 95b4662 to d9400a9 Compare January 28, 2025 04:46
Copy link

pkg-pr-new bot commented Jan 28, 2025

Open in Stackblitz

npm i https://pkg.pr.new/kufu/smarthr-ui@5340

commit: e919ec1

@@ -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',
Copy link
Member Author

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化の効率が良くなります

Copy link
Contributor

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属性の方を選んだ理由ってありますか?

Copy link
Member Author

Choose a reason for hiding this comment

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

@Qs-F
これややこしいんですが、labelはinputを内包していないんですよ。
構造的には

span[data-disabled]
  input
  label

となっています。

Comment on lines +123 to +142
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>
),
)
Copy link
Member Author

Choose a reason for hiding this comment

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

これらの箇所はほぼ書き換わる可能性がないため、memo化しています。
LabeledChildrenはこのコンポーネント自体のchildrenを受け取っていますが、CheckBoxの特性上、ReactNodeでわたってくる可能性はひくく、ほとんどがstringとなるため、効率は良いはずです

@AtsushiM AtsushiM marked this pull request as ready for review January 28, 2025 06:13
@AtsushiM AtsushiM requested a review from a team as a code owner January 28, 2025 06:13
@AtsushiM AtsushiM requested review from moshisora and uknmr and removed request for a team January 28, 2025 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants