-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathlistbox-option.tsx
51 lines (48 loc) · 1.51 KB
/
listbox-option.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import React, { ComponentProps, FC, ReactElement } from 'react'
import classNames from 'classnames'
import { filterByType } from '../utils'
import { EbayIcon } from '../ebay-icon'
import { EbayListboxOptionDescription } from './listbox-option-description'
export type EbayListboxOptionProps = ComponentProps<'div'> & {
icon?: ReactElement;
text?: string;
value: string;
disabled?: boolean;
selected?: boolean;
};
export const EbayListboxOption: FC<EbayListboxOptionProps> = ({
className,
icon,
text,
children,
disabled,
tabIndex,
selected,
...rest
}) => {
const description = filterByType(children, EbayListboxOptionDescription)
const displayText = text || (!description?.length ? children : '')
return (
<div
{...rest}
tabIndex={disabled ? -1 : tabIndex}
className={classNames('listbox__option', className)}
aria-disabled={disabled}
aria-selected={selected}
role="option">
{icon ? (
<span className="listbox__value">
{icon}
{displayText ? <span>{displayText}</span> : null}
{description?.length ? description : null}
</span>
) : (
<>
<span className="listbox__value">{displayText}</span>
{description?.length ? description : null}
</>
)}
<EbayIcon name="tick16" />
</div>
)
}