1- import React , { useState , useLayoutEffect , useEffect , useRef } from "react" ;
2- import styled , { ThemeProvider } from "styled-components" ;
3- import useTheme from "../useTheme" ;
1+ import React , { useLayoutEffect , useRef } from "react" ;
2+ import styled from "styled-components" ;
43import useTranslatedLabels from "../useTranslatedLabels" ;
54import { ListboxProps } from "./types" ;
65import Option from "./Option" ;
@@ -20,14 +19,12 @@ const Listbox = ({
2019 optionalItem,
2120 searchable,
2221 handleOptionOnClick,
23- getSelectWidth ,
22+ styles ,
2423} : ListboxProps ) : JSX . Element => {
25- const colorsTheme = useTheme ( ) ;
2624 const translatedLabels = useTranslatedLabels ( ) ;
2725 const listboxRef = useRef ( null ) ;
28- const [ styles , setStyles ] = useState ( null ) ;
2926
30- let globalIndex = optional && ! multiple ? 0 : - 1 ; // index for options, starting from 0 to options.length -1
27+ let globalIndex = optional && ! multiple ? 0 : - 1 ;
3128 const mapOptionFunc = ( option , mapIndex ) => {
3229 if ( option . options ) {
3330 const groupId = `group-${ mapIndex } ` ;
@@ -90,60 +87,43 @@ const Listbox = ({
9087 visualFocusedOptionEl ?. scrollIntoView ?.( { block : "nearest" , inline : "start" } ) ;
9188 } , [ visualFocusIndex ] ) ;
9289
93- const handleResize = ( ) => {
94- setStyles ( { width : getSelectWidth ( ) } ) ;
95- } ;
96-
97- useLayoutEffect ( ( ) => {
98- handleResize ( ) ;
99- } , [ getSelectWidth ] ) ;
100-
101- useEffect ( ( ) => {
102- window . addEventListener ( "resize" , handleResize ) ;
103- return ( ) => {
104- window . removeEventListener ( "resize" , handleResize ) ;
105- } ;
106- } , [ getSelectWidth ] ) ;
107-
10890 return (
109- < ThemeProvider theme = { colorsTheme . select } >
110- < ListboxContainer
111- id = { id }
112- onClick = { ( event ) => {
113- event . stopPropagation ( ) ;
114- } }
115- onMouseDown = { ( event ) => {
116- event . preventDefault ( ) ;
117- } }
118- ref = { listboxRef }
119- role = "listbox"
120- aria-multiselectable = { multiple }
121- style = { styles }
122- >
123- { searchable && ( options . length === 0 || ! groupsHaveOptions ( options ) ) ? (
124- < OptionsSystemMessage >
125- < NoMatchesFoundIcon > { selectIcons . searchOff } </ NoMatchesFoundIcon >
126- { translatedLabels . select . noMatchesErrorMessage }
127- </ OptionsSystemMessage >
128- ) : (
129- optional &&
130- ! multiple && (
131- < Option
132- key = { `option-${ optionalItem . value } ` }
133- id = { `option-${ 0 } ` }
134- option = { optionalItem }
135- onClick = { handleOptionOnClick }
136- multiple = { multiple }
137- visualFocused = { visualFocusIndex === 0 }
138- isGroupedOption = { false }
139- isLastOption = { lastOptionIndex === 0 }
140- isSelected = { multiple ? currentValue . includes ( optionalItem . value ) : currentValue === optionalItem . value }
141- />
142- )
143- ) }
144- { options . map ( mapOptionFunc ) }
145- </ ListboxContainer >
146- </ ThemeProvider >
91+ < ListboxContainer
92+ id = { id }
93+ onClick = { ( event ) => {
94+ event . stopPropagation ( ) ;
95+ } }
96+ onMouseDown = { ( event ) => {
97+ event . preventDefault ( ) ;
98+ } }
99+ ref = { listboxRef }
100+ role = "listbox"
101+ aria-multiselectable = { multiple }
102+ style = { styles }
103+ >
104+ { searchable && ( options . length === 0 || ! groupsHaveOptions ( options ) ) ? (
105+ < OptionsSystemMessage >
106+ < NoMatchesFoundIcon > { selectIcons . searchOff } </ NoMatchesFoundIcon >
107+ { translatedLabels . select . noMatchesErrorMessage }
108+ </ OptionsSystemMessage >
109+ ) : (
110+ optional &&
111+ ! multiple && (
112+ < Option
113+ key = { `option-${ optionalItem . value } ` }
114+ id = { `option-${ 0 } ` }
115+ option = { optionalItem }
116+ onClick = { handleOptionOnClick }
117+ multiple = { multiple }
118+ visualFocused = { visualFocusIndex === 0 }
119+ isGroupedOption = { false }
120+ isLastOption = { lastOptionIndex === 0 }
121+ isSelected = { multiple ? currentValue . includes ( optionalItem . value ) : currentValue === optionalItem . value }
122+ />
123+ )
124+ ) }
125+ { options . map ( mapOptionFunc ) }
126+ </ ListboxContainer >
147127 ) ;
148128} ;
149129
0 commit comments