1- import React , { useId , useLayoutEffect , useRef } from "react" ;
1+ import React , { useLayoutEffect , useRef } from "react" ;
22import styled from "styled-components" ;
33import useTranslatedLabels from "../useTranslatedLabels" ;
44import { ListboxProps } from "./types" ;
55import Option from "./Option" ;
66import DxcIcon from "../icon/Icon" ;
7-
8- const groupsHaveOptions = ( options ) =>
9- options ?. [ 0 ] . options ? options . some ( ( groupOption ) => groupOption . options ?. length > 0 ) : true ;
7+ import { groupsHaveOptions } from "./selectUtils" ;
108
119const Listbox = ( {
1210 id,
@@ -23,25 +21,24 @@ const Listbox = ({
2321} : ListboxProps ) : JSX . Element => {
2422 const translatedLabels = useTranslatedLabels ( ) ;
2523 const listboxRef = useRef ( null ) ;
26- const listboxId = `select-${ useId ( ) } ` ;
27-
24+
2825 let globalIndex = optional && ! multiple ? 0 : - 1 ;
2926 const mapOptionFunc = ( option , mapIndex ) => {
30- const groupId = `${ listboxId } -group-${ mapIndex } ` ;
27+ const groupId = `${ id } -group-${ mapIndex } ` ;
3128 if ( option . options ) {
3229 return (
3330 option . options . length > 0 && (
3431 < li key = { groupId } >
35- < GroupList role = "listbox" aria-labelledby = { groupId } >
32+ < ul role = "listbox" aria-labelledby = { groupId } style = { { padding : 0 } } >
3633 < GroupLabel role = "presentation" id = { groupId } >
3734 { option . label }
3835 </ GroupLabel >
3936 { option . options . map ( ( singleOption ) => {
4037 globalIndex ++ ;
4138 return (
4239 < Option
43- key = { `${ listboxId } -option-${ singleOption . value } ` }
44- id = { `${ listboxId } -option-${ globalIndex } ` }
40+ key = { `${ id } -option-${ singleOption . value } ` }
41+ id = { `${ id } -option-${ globalIndex } ` }
4542 option = { singleOption }
4643 onClick = { handleOptionOnClick }
4744 multiple = { multiple }
@@ -54,16 +51,16 @@ const Listbox = ({
5451 />
5552 ) ;
5653 } ) }
57- </ GroupList >
54+ </ ul >
5855 </ li >
5956 )
6057 ) ;
6158 } else {
6259 globalIndex ++ ;
6360 return (
6461 < Option
65- key = { `${ listboxId } -option-${ option . value } ` }
66- id = { `${ listboxId } -option-${ globalIndex } ` }
62+ key = { `${ id } -option-${ option . value } ` }
63+ id = { `${ id } -option-${ globalIndex } ` }
6764 option = { option }
6865 onClick = { handleOptionOnClick }
6966 multiple = { multiple }
@@ -88,7 +85,7 @@ const Listbox = ({
8885 visualFocusedOptionEl ?. scrollIntoView ?.( { block : "nearest" , inline : "start" } ) ;
8986 } , [ visualFocusIndex ] ) ;
9087
91- const hasOptionGroups = options . some ( option => option . options ?. length > 0 ) ;
88+ const hasOptionGroups = options . some ( ( option ) => option . options ?. length > 0 ) ;
9289
9390 return (
9491 < ListboxContainer
@@ -100,7 +97,7 @@ const Listbox = ({
10097 event . preventDefault ( ) ;
10198 } }
10299 ref = { listboxRef }
103- aria-multiselectable = { ! hasOptionGroups ? multiple : undefined }
100+ aria-multiselectable = { ! hasOptionGroups ? multiple : undefined }
104101 style = { styles }
105102 role = { hasOptionGroups ? "list" : "listbox" }
106103 aria-label = "List of options"
@@ -143,12 +140,13 @@ const ListboxContainer = styled.ul`
143140 border: 1px solid ${ ( props ) => props . theme . listDialogBorderColor } ;
144141 border-radius: 0.25rem;
145142 box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
146- cursor: default;
147143 color: ${ ( props ) => props . theme . listOptionFontColor } ;
148144 font-family: ${ ( props ) => props . theme . fontFamily } ;
149145 font-size: ${ ( props ) => props . theme . listOptionFontSize } ;
150146 font-style: ${ ( props ) => props . theme . listOptionFontStyle } ;
151147 font-weight: ${ ( props ) => props . theme . listOptionFontWeight } ;
148+ line-height: 24px;
149+ cursor: default;
152150` ;
153151
154152const OptionsSystemMessage = styled . span `
@@ -170,14 +168,10 @@ const NoMatchesFoundIcon = styled.span`
170168 font-size: 16px;
171169` ;
172170
173- const GroupList = styled . ul `
174- padding: 0;
175- ` ;
176-
177171const GroupLabel = styled . li `
178172 padding: 4px 16px;
179173 font-weight: ${ ( props ) => props . theme . listGroupLabelFontWeight } ;
180174 line-height: 1.715em;
181175` ;
182176
183- export default React . memo ( Listbox ) ;
177+ export default Listbox ;
0 commit comments