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

Fix EbayListboxButton console warning #252

Merged
merged 3 commits into from
Oct 19, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ exports[`Storyshots ebay-listbox-button Borderless 1`] = `
<select
className="listbox-button__native"
hidden={true}
onChange={[Function]}
value="BB"
>
<option
Expand Down Expand Up @@ -87,6 +88,7 @@ exports[`Storyshots ebay-listbox-button Default - no selected option 1`] = `
<select
className="listbox-button__native"
hidden={true}
onChange={[Function]}
value=""
>
<option
Expand Down Expand Up @@ -148,6 +150,7 @@ Array [
<select
className="listbox-button__native"
hidden={true}
onChange={[Function]}
value="BB"
>
<option
Expand Down Expand Up @@ -200,6 +203,7 @@ exports[`Storyshots ebay-listbox-button Default 1`] = `
<select
className="listbox-button__native"
hidden={true}
onChange={[Function]}
value="BB"
>
<option
Expand Down Expand Up @@ -252,6 +256,7 @@ exports[`Storyshots ebay-listbox-button Disabled State 1`] = `
<select
className="listbox-button__native"
hidden={true}
onChange={[Function]}
value="BB"
>
<option
Expand Down Expand Up @@ -308,6 +313,7 @@ exports[`Storyshots ebay-listbox-button Floating label 1`] = `
<select
className="listbox-button__native"
hidden={true}
onChange={[Function]}
value=""
>
<option
Expand Down Expand Up @@ -359,6 +365,7 @@ exports[`Storyshots ebay-listbox-button Fluid 1`] = `
<select
className="listbox-button__native"
hidden={true}
onChange={[Function]}
value="BB"
>
<option
Expand Down Expand Up @@ -411,6 +418,7 @@ exports[`Storyshots ebay-listbox-button Invalid State 1`] = `
<select
className="listbox-button__native"
hidden={true}
onChange={[Function]}
value="BB"
>
<option
Expand Down Expand Up @@ -467,6 +475,7 @@ exports[`Storyshots ebay-listbox-button Prefix label 1`] = `
<select
className="listbox-button__native"
hidden={true}
onChange={[Function]}
value=""
>
<option
Expand Down Expand Up @@ -518,6 +527,7 @@ exports[`Storyshots ebay-listbox-button Preselected index 1`] = `
<select
className="listbox-button__native"
hidden={true}
onChange={[Function]}
value="BB"
>
<option
Expand Down Expand Up @@ -570,6 +580,7 @@ exports[`Storyshots ebay-listbox-button Statefull component 1`] = `
<select
className="listbox-button__native"
hidden={true}
onChange={[Function]}
value=""
>
<option
Expand Down Expand Up @@ -646,6 +657,7 @@ exports[`Storyshots ebay-listbox-button Too many options 1`] = `
<select
className="listbox-button__native"
hidden={true}
onChange={[Function]}
value="BB"
>
<option
Expand Down
21 changes: 12 additions & 9 deletions src/ebay-listbox-button/listbox-button.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {
Children, cloneElement, useEffect, useRef, useState,
ComponentProps, FC, KeyboardEvent, ReactElement
ComponentProps, FC, KeyboardEvent, ReactElement, useCallback
} from 'react'
import classNames from 'classnames'
import { EbayIcon } from '../ebay-icon'
Expand Down Expand Up @@ -86,10 +86,12 @@ const ListboxButton: FC<EbayListboxButtonProps> = ({

const childrenArray = Children.toArray(children) as ReactElement[]
const getSelectedValueByIndex = (index: number) => childrenArray[index].props.value
const getIndexByValue = useCallback((selectedValue) =>
childrenArray.findIndex(({ props }) => props.value === selectedValue), [childrenArray])
const getSelectedOption = (currentIndex: number) => optionsByIndexRef.current.get(currentIndex)
const setActiveDescendant = (index: number) => {
const optionsContainerEle = optionsContainerRef.current
optionsContainerEle.setAttribute(`aria-activedescendant`, getSelectedOption(index).id)
optionsContainerEle?.setAttribute(`aria-activedescendant`, getSelectedOption(index).id)
}

const collapseListbox = () => {
Expand All @@ -110,7 +112,7 @@ const ListboxButton: FC<EbayListboxButtonProps> = ({
}
}

const onOptionsSelect = (e, optionValue, index) => {
const onOptionsSelect = (e, index) => {
// OnSelect set the selectedValue to the state and expanded to false to close the list box
setSelectedOption(childrenArray[index])
setSelectedIndex(index)
Expand Down Expand Up @@ -224,14 +226,14 @@ const ListboxButton: FC<EbayListboxButtonProps> = ({
}
}

// We want to minic the select box behavior so we take the onSelect that passed
// at the parent level and use it for the OnClick on the list box since its a fake dropdown
const updatelistBoxButtonOptions = listBoxButtonOptions
// We want to mimic the select box behavior, so we take the onSelect that passed
// at the parent level and use it for the OnClick on the list box since it is a fake dropdown
const updateListBoxButtonOptions = listBoxButtonOptions
.map((child, index) => cloneElement(child, {
index,
key: index,
selected: selectedOption && child.props.value === selectedOption.props.value,
onClick: (e, optionValue) => onOptionsSelect(e, optionValue, index),
onClick: (e) => onOptionsSelect(e, index),
innerRef: optionNode => !optionNode
? optionsByIndexRef.current.delete(index)
: optionsByIndexRef.current.set(index, optionNode)
Expand Down Expand Up @@ -296,16 +298,17 @@ const ListboxButton: FC<EbayListboxButtonProps> = ({
setTimeout(() => buttonRef.current.focus(), 0)
}}
>
{updatelistBoxButtonOptions}
{updateListBoxButtonOptions}
</div>
</div>}
<select
hidden
className="listbox-button__native"
name={name}
onChange={(e) => onOptionsSelect(e, getIndexByValue(e.target.value))}
value={selectedOption ? selectedOption?.props.value : ''}>
{
updatelistBoxButtonOptions.map((option, i) =>
updateListBoxButtonOptions.map((option, i) =>
<option value={option.props.value} key={i} />)
}
</select>
Expand Down
Loading