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: ComboBoxのカスタムhookを整理する #5334

Merged
merged 64 commits into from
Feb 25, 2025

Conversation

AtsushiM
Copy link
Member

関連URL

概要

変更内容

確認方法

@yagimushi yagimushi force-pushed the chore-refactoring-ComboBox-useFocusControl branch from 45cd39a to 0ee6ef6 Compare February 6, 2025 02:53
@yagimushi yagimushi force-pushed the chore-refactoring-ComboBox-useFocusControl branch from 856ccf7 to 9c0181a Compare February 16, 2025 22:27
@yagimushi yagimushi force-pushed the chore-refactoring-ComboBox-useFocusControl branch from 9c0181a to 25e7e80 Compare February 17, 2025 00:44
@AtsushiM AtsushiM marked this pull request as ready for review February 17, 2025 00:47
@AtsushiM AtsushiM requested a review from a team as a code owner February 17, 2025 00:47
@AtsushiM AtsushiM requested review from oti and uknmr and removed request for a team February 17, 2025 00:47
Comment on lines +27 to +33
let nextIndex = 0

if (currentActiveIndex !== -1) {
nextIndex = (currentActiveIndex + delta + options.length) % options.length
} else if (delta !== 1) {
nextIndex = options.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だけであり、他の変数の生成などもないため、無名関数でスコープを切り出すほどではないと判断しました。
またletでnextIndexの初期値を0にすることで条件分岐を整理しています

Comment on lines +22 to +27
if (focusedIndex !== null) {
const nextIndex = Math.max(focusedIndex - 1, 0)

deletionButtonRefs[nextIndex].current?.focus()
setFocusedIndex(nextIndex)
} else if (inputRef.current?.selectionStart === 0) {
Copy link
Member Author

Choose a reason for hiding this comment

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

条件がややこしいことになっていたのでネストしなくても良いように条件調整しました

deletionButtonRefs[nextIndex].current?.focus()
setFocusedIndex(nextIndex)
}
}, [deletionButtonRefs, focusedIndex, selectedItemLength])

const focusNextDeletionButton = useCallback(() => {
if (deletionButtonRefs.length === 0) {
if (deletionButtonRefs.length === 0 || focusedIndex === null) {
Copy link
Member Author

Choose a reason for hiding this comment

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

せっかく早期returnしているので、即終了する条件をまとめています

Comment on lines -48 to -54
const moveActivePositionDown = useCallback(() => {
moveActiveOptionIndex(activeOption, 1)
}, [activeOption, moveActiveOptionIndex])

const moveActivePositionUp = useCallback(() => {
moveActiveOptionIndex(activeOption, -1)
}, [activeOption, moveActiveOptionIndex])
Copy link
Member Author

Choose a reason for hiding this comment

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

activeOption が変更される度にmemoが更新され、また利用箇所も一箇所だけ、引数や処理なども複雑ではないため、あまり旨味のあるuseCallbackでは有りませんでした。
moveActiveOptionIndexをexportするようにします

Copy link
Contributor

@Qs-F Qs-F left a comment

Choose a reason for hiding this comment

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

LGTM

@AtsushiM AtsushiM merged commit 7421091 into master Feb 25, 2025
15 checks passed
@AtsushiM AtsushiM deleted the chore-refactoring-ComboBox-useFocusControl branch February 25, 2025 22: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