66import { css } from "goober" ;
77import React , { useEffect , useRef , useState } from "react" ;
88
9+ import getString from "../lib/get-string" ;
10+ import Cross from "../select-panel/cross" ;
911import Arrow from "./arrow" ;
1012import Loading from "./loading" ;
1113
@@ -19,6 +21,7 @@ interface IDropdownProps {
1921 labelledBy ?: string ;
2022 onMenuToggle ?;
2123 ArrowRenderer ?;
24+ ClearSelectedIcon ?;
2225}
2326
2427const PanelContainer = css ( {
@@ -65,6 +68,13 @@ const DropdownHeading = css({
6568 } ,
6669} ) ;
6770
71+ const ClearSelectedButton = css ( {
72+ cursor : "pointer" ,
73+ background : "none" ,
74+ border : 0 ,
75+ padding : 0 ,
76+ } ) ;
77+
6878const Dropdown = ( {
6979 children,
7080 contentComponent : ContentComponent ,
@@ -75,6 +85,7 @@ const Dropdown = ({
7585 labelledBy,
7686 onMenuToggle,
7787 ArrowRenderer,
88+ ClearSelectedIcon,
7889} : IDropdownProps ) => {
7990 const [ expanded , setExpanded ] = useState ( false ) ;
8091 const [ hasFocus , setHasFocus ] = useState ( false ) ;
@@ -125,6 +136,11 @@ const Dropdown = ({
125136 const toggleExpanded = ( ) =>
126137 setExpanded ( isLoading || disabled ? false : ! expanded ) ;
127138
139+ const handleClearSelected = ( e ) => {
140+ e . stopPropagation ( ) ;
141+ contentProps [ "onChange" ] ( [ ] ) ;
142+ } ;
143+
128144 return (
129145 < div
130146 tabIndex = { 0 }
@@ -146,6 +162,19 @@ const Dropdown = ({
146162 >
147163 < div className = "dropdown-heading-value" > { children } </ div >
148164 { isLoading && < Loading /> }
165+ { contentProps [ "value" ] . length > 0 && (
166+ < button
167+ type = "button"
168+ className = { `${ ClearSelectedButton } clear-selected-button` }
169+ onClick = { handleClearSelected }
170+ aria-label = { getString (
171+ "clearSelected" ,
172+ contentProps [ "overrideStrings" ]
173+ ) }
174+ >
175+ { ClearSelectedIcon || < Cross /> }
176+ </ button >
177+ ) }
149178 < FinalArrow expanded = { expanded } />
150179 </ div >
151180 { expanded && (
0 commit comments