Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 70 additions & 58 deletions packages/ui-select/src/Select/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ type: example
setIsShowingOptions(true)
if (inputValue || selectedOptionId || options.length === 0) return

switch (event.key) {
case 'ArrowDown':
return handleHighlightOption(event, { id: options[0].id })
case 'ArrowUp':
return handleHighlightOption(event, {
id: options[options.length - 1].id
})
if ('key' in event) {
Copy link
Contributor Author

@ToMESSKa ToMESSKa Dec 3, 2025

Choose a reason for hiding this comment

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

I changed a few examples in README a bit to make them TypeScript error-free

switch (event.key) {
case 'ArrowDown':
return handleHighlightOption(event, { id: options[0].id })
case 'ArrowUp':
return handleHighlightOption(event, {
id: options[options.length - 1].id
})
}
}
}

Expand Down Expand Up @@ -234,13 +236,15 @@ type: example
)
if (inputValue || selectedOptionId || options.length === 0) return

switch (event.key) {
case 'ArrowDown':
return handleHighlightOption(event, { id: options[0].id })
case 'ArrowUp':
return handleHighlightOption(event, {
id: options[options.length - 1].id
})
if ('key' in event) {
switch (event.key) {
case 'ArrowDown':
return handleHighlightOption(event, { id: options[0].id })
case 'ArrowUp':
return handleHighlightOption(event, {
id: options[options.length - 1].id
})
}
}
}

Expand Down Expand Up @@ -436,21 +440,23 @@ type: example

if (inputValue || options.length === 0) return

switch (event.key) {
case 'ArrowDown':
return handleHighlightOption(event, {
id: options.find((option) => !selectedOptionId.includes(option.id))
.id
})
case 'ArrowUp':
// Highlight last non-selected option
return handleHighlightOption(event, {
id: options[
options.findLastIndex(
(option) => !selectedOptionId.includes(option.id)
)
].id
})
if ('key' in event) {
switch (event.key) {
case 'ArrowDown':
return handleHighlightOption(event, {
id: options.find((option) => !selectedOptionId.includes(option.id))
.id
})
case 'ArrowUp':
// Highlight last non-selected option
return handleHighlightOption(event, {
id: options[
options.findLastIndex(
(option) => !selectedOptionId.includes(option.id)
)
].id
})
}
}
}

Expand Down Expand Up @@ -489,13 +495,13 @@ type: example
const newOptions = filterOptions(value)
setInputValue(value)
setFilteredOptions(newOptions)
sethHighlightedOptionId(newOptions.length > 0 ? newOptions[0].id : null)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this was a typo

Copy link
Collaborator

Choose a reason for hiding this comment

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

We should really type check these somehow...

setHighlightedOptionId(newOptions.length > 0 ? newOptions[0].id : null)
setIsShowingOptions(true)
setAnnouncement(getOptionsChangedMessage(newOptions))
}

const handleKeyDown = (event) => {
if (event.keyCode === 8) {
if ('keyCode' in event && event.keyCode === 8) {
// when backspace key is pressed
if (inputValue === '' && selectedOptionId.length > 0) {
// remove last selected option, if input has no entered text
Expand Down Expand Up @@ -660,17 +666,19 @@ const GroupSelectExample = ({ options }) => {
const handleShowOptions = (event) => {
setIsShowingOptions(true)
setHighlightedOptionId(null)
if (inputValue || selectedOptionId || options.length === 0) return
if (inputValue || selectedOptionId || Object.keys(options).length === 0) return

switch (event.key) {
case 'ArrowDown':
return handleHighlightOption(event, {
id: options[Object.keys(options)[0]][0].id
})
case 'ArrowUp':
return handleHighlightOption(event, {
id: Object.values(options).at(-1)?.at(-1)?.id
})
if ('key' in event) {
switch (event.key) {
case 'ArrowDown':
return handleHighlightOption(event, {
id: options[Object.keys(options)[0]][0].id
})
case 'ArrowUp':
return handleHighlightOption(event, {
id: Object.values(options).at(-1)?.at(-1)?.id
})
}
}
}

Expand Down Expand Up @@ -840,17 +848,19 @@ const GroupSelectAutocompleteExample = ({ options }) => {
setIsShowingOptions(true)
setHighlightedOptionId(null)

if (inputValue || selectedOptionId || options.length === 0) return
if (inputValue || selectedOptionId || Object.keys(options).length === 0) return

switch (event.key) {
case 'ArrowDown':
return handleHighlightOption(event, {
id: options[Object.keys(options)[0]][0].id
})
case 'ArrowUp':
return handleHighlightOption(event, {
id: Object.values(options).at(-1)?.at(-1)?.id
})
if ('key' in event) {
switch (event.key) {
case 'ArrowDown':
return handleHighlightOption(event, {
id: options[Object.keys(options)[0]][0].id
})
case 'ArrowUp':
return handleHighlightOption(event, {
id: Object.values(options).at(-1)?.at(-1)?.id
})
}
}
}

Expand Down Expand Up @@ -1205,13 +1215,15 @@ const SingleSelectExample = ({ options }) => {

if (inputValue || selectedOptionId || options.length === 0) return

switch (event.key) {
case 'ArrowDown':
return handleHighlightOption(event, { id: options[0].id })
case 'ArrowUp':
return handleHighlightOption(event, {
id: options[options.length - 1].id
})
if ('key' in event) {
switch (event.key) {
case 'ArrowDown':
return handleHighlightOption(event, { id: options[0].id })
case 'ArrowUp':
return handleHighlightOption(event, {
id: options[options.length - 1].id
})
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/ui-select/src/Select/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ class Select extends Component<SelectProps> {
}
}

highlightOption(event: React.SyntheticEvent, id: string) {
highlightOption(event: React.KeyboardEvent | React.MouseEvent, id: string) {
const { onRequestHighlightOption } = this.props
if (id) {
onRequestHighlightOption?.(event, { id })
Expand Down
10 changes: 6 additions & 4 deletions packages/ui-select/src/Select/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,26 +142,28 @@ type PropsFromSelectable = {
/**
* Callback fired requesting that the options list be shown.
*/
onRequestShowOptions?: (event: React.SyntheticEvent) => void
onRequestShowOptions?: (event: React.KeyboardEvent | React.MouseEvent) => void

/**
* Callback fired requesting that the options list be hidden.
*/
onRequestHideOptions?: (event: React.SyntheticEvent) => void
onRequestHideOptions?: (
event: React.KeyboardEvent | React.MouseEvent | React.FocusEvent
) => void

/**
* Callback fired requesting a particular option be highlighted.
*/
onRequestHighlightOption?: (
event: React.SyntheticEvent,
event: React.KeyboardEvent | React.MouseEvent,
data: { id?: string; direction?: 1 | -1 }
) => void

/**
* Callback fired requesting a particular option be selected.
*/
onRequestSelectOption?: (
event: React.SyntheticEvent,
event: React.KeyboardEvent | React.MouseEvent,
data: { id?: string }
) => void
}
Expand Down
18 changes: 10 additions & 8 deletions packages/ui-selectable/src/Selectable/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,39 @@ type SelectableOwnProps = {
/**
* Callback fired when the options want to become visible
*/
onRequestShowOptions?: (event: React.SyntheticEvent) => void
onRequestShowOptions?: (event: React.KeyboardEvent | React.MouseEvent) => void

/**
* Callback fired when the options no longer want to be visible
*/
onRequestHideOptions?: (event: React.SyntheticEvent) => void
onRequestHideOptions?: (
event: React.KeyboardEvent | React.MouseEvent | React.FocusEvent
) => void

/**
* Callback fired when option is hovered or highlighted via keyboard.
* Either the `id` or the `direction` parameter is supplied
*/
onRequestHighlightOption?: (
event: React.SyntheticEvent,
event: React.KeyboardEvent | React.MouseEvent,
data: { id?: string; direction?: 1 | -1 }
) => void

/**
* Callback fired when first option should be highlighted
* Callback fired when first option should be highlighted (triggered by the Home key)
*/
onRequestHighlightFirstOption?: (event: React.SyntheticEvent) => void
onRequestHighlightFirstOption?: (event: React.KeyboardEvent) => void

/**
* Callback fired when last option should be highlighted
* Callback fired when last option should be highlighted (triggered by the End key)
*/
onRequestHighlightLastOption?: (event: React.SyntheticEvent) => void
onRequestHighlightLastOption?: (event: React.KeyboardEvent) => void

/**
* Callback fired when option clicked or selected via keyboard
*/
onRequestSelectOption?: (
event: React.SyntheticEvent,
event: React.KeyboardEvent | React.MouseEvent,
data: { id?: string }
) => void

Expand Down
Loading