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: DropdownMenuButton の内部ロジックを整理する #5314

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

Conversation

AtsushiM
Copy link
Member

関連URL

概要

変更内容

確認方法

@yagimushi yagimushi force-pushed the chore-refactoring-DropdownMenuButton branch 3 times, most recently from af4bfbe to c240c5e Compare January 22, 2025 00:14
Copy link

pkg-pr-new bot commented Jan 22, 2025

Open in Stackblitz

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

commit: 0590fe2

Comment on lines -87 to -92
if (['Up', 'ArrowUp', 'Left', 'ArrowLeft'].includes(e.key)) {
moveFocus(-1, enabledItems, focusedIndex, hoveredItem)
}

if (['Down', 'ArrowDown', 'Right', 'ArrowRight'].includes(e.key)) {
moveFocus(1, enabledItems, focusedIndex, hoveredItem)
Copy link
Member Author

Choose a reason for hiding this comment

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

直前で計算している allItems ですが、押したキーが合致しない場合、完全に無駄処理になります。
チェックする順序を調整して無駄処理が発生しないようにしています

},
)

if (['Up', 'ArrowUp', 'Left', 'ArrowLeft'].includes(e.key)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

配列に対してincludesを実行するより、正規表現一発でのチェックのほうが高速のため調整しています

Comment on lines +50 to +60
let nextIndex = 0

if (focusedIndex > -1) {
// フォーカスされているアイテムが存在する場合
nextIndex = (focusedIndex + direction + tabbableItems.length) % tabbableItems.length
} else if (hoveredItem) {
// ホバー状態のアイテムが存在する場合
nextIndex =
(tabbableItems.indexOf(hoveredItem) + direction + tabbableItems.length) % tabbableItems.length
} else if (direction === -1) {
nextIndex = tabbableItems.length - 1
Copy link
Member Author

Choose a reason for hiding this comment

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

元々は関数内にロジックが隠蔽されていましたが、変数の汚染などもないし、nextIndexに値を入れるだけだったのでただのif-elseにしました

@@ -4,35 +4,63 @@ const matchesDisabledState = (element: Element): boolean =>
element.matches(':disabled') || element.getAttribute('aria-disabled') === 'true'

const isElementDisabled = (element: Element): boolean => {
if (matchesDisabledState(element)) return true
return Array.from(element.querySelectorAll('*')).some((child) => matchesDisabledState(child))
Copy link
Member Author

Choose a reason for hiding this comment

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

matchesDisabledState を呼び出すための無名関数を生成していましたが、引数が完全一致のため、無意味でした。
matchesDisabledStateを直接参照渡しするようにしました

Comment on lines +56 to +60
const NameText = React.memo<PropsWithChildren<{ className: string }>>(
({ children, className }) =>
children && (
<Text as="p" size="S" weight="bold" color="TEXT_GREY" leading="NONE" className={className}>
{children}
Copy link
Member Author

Choose a reason for hiding this comment

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

childrenに対してReactNodeが渡されますが、たいていはstringになるはずなのでmemo化しました

Comment on lines 128 to 142
const TriggerLabel = React.memo<Pick<Props, 'label' | 'onlyIconTrigger' | 'triggerIcon'>>(
({ label, onlyIconTrigger, triggerIcon }) => {
if (!onlyIconTrigger) {
return label
}

const Icon = triggerIcon || FaEllipsisIcon

return <Icon alt={typeof label === 'string' ? label : innerText(label)} />
},
)

const ButtonSuffixIcon = React.memo<Pick<Props, 'onlyIconTrigger'>>(
({ onlyIconTrigger }) => !onlyIconTrigger && <FaCaretDownIcon alt="候補を開く" />,
)
Copy link
Member Author

Choose a reason for hiding this comment

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

useMemoされていましたが、内容がReactNodeなので、より適切なReact.memoを使うようにしています

@@ -70,59 +70,88 @@ export const DropdownMenuButton: FC<Props & ElementProps> = ({
label,
children,
triggerSize,
onlyIconTrigger = false,
triggerIcon: TriggerIcon,
onlyIconTrigger,
Copy link
Member Author

Choose a reason for hiding this comment

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

比較しかしていないため、初期値代入は不要でした

@yagimushi yagimushi force-pushed the chore-refactoring-DropdownMenuButton branch from c240c5e to 0590fe2 Compare January 22, 2025 00:22
@AtsushiM AtsushiM marked this pull request as ready for review January 22, 2025 02:31
@AtsushiM AtsushiM requested a review from a team as a code owner January 22, 2025 02:31
@AtsushiM AtsushiM requested review from Qs-F and removed request for a team January 22, 2025 02:31
@AtsushiM AtsushiM requested a review from masa0527 January 22, 2025 02:31
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.

1 participant