-
Notifications
You must be signed in to change notification settings - Fork 141
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
base: master
Are you sure you want to change the base?
Conversation
af4bfbe
to
c240c5e
Compare
commit: |
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) |
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.
直前で計算している allItems
ですが、押したキーが合致しない場合、完全に無駄処理になります。
チェックする順序を調整して無駄処理が発生しないようにしています
}, | ||
) | ||
|
||
if (['Up', 'ArrowUp', 'Left', 'ArrowLeft'].includes(e.key)) { |
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.
配列に対してincludesを実行するより、正規表現一発でのチェックのほうが高速のため調整しています
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 |
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.
元々は関数内にロジックが隠蔽されていましたが、変数の汚染などもないし、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)) |
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.
matchesDisabledState
を呼び出すための無名関数を生成していましたが、引数が完全一致のため、無意味でした。
matchesDisabledStateを直接参照渡しするようにしました
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} |
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.
childrenに対してReactNodeが渡されますが、たいていはstringになるはずなのでmemo化しました
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="候補を開く" />, | ||
) |
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.
useMemoされていましたが、内容がReactNodeなので、より適切なReact.memoを使うようにしています
@@ -70,59 +70,88 @@ export const DropdownMenuButton: FC<Props & ElementProps> = ({ | |||
label, | |||
children, | |||
triggerSize, | |||
onlyIconTrigger = false, | |||
triggerIcon: TriggerIcon, | |||
onlyIconTrigger, |
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.
比較しかしていないため、初期値代入は不要でした
c240c5e
to
0590fe2
Compare
関連URL
概要
変更内容
確認方法