@@ -11,6 +11,7 @@ import React, {
1111import classnames from 'classnames' ;
1212import { FormElement , FormElementProps } from './FormElement' ;
1313import { Icon } from './Icon' ;
14+ import { Button , ButtonProps } from './Button' ;
1415import {
1516 DropdownMenu ,
1617 DropdownMenuItem ,
@@ -54,8 +55,6 @@ const PicklistContext = createContext<{
5455 *
5556 */
5657export type PicklistProps < MultiSelect extends boolean | undefined > = {
57- id ?: string ;
58- className ?: string ;
5958 label ?: string ;
6059 required ?: boolean ;
6160 multiSelect ?: MultiSelect ;
@@ -85,8 +84,10 @@ export type PicklistProps<MultiSelect extends boolean | undefined> = {
8584 onKeyDown ?: ( e : React . KeyboardEvent ) => void ;
8685 onBlur ?: ( ) => void ;
8786 onComplete ?: ( ) => void ;
88- children ?: React . ReactNode ;
89- } ;
87+ } & Omit <
88+ ButtonProps ,
89+ 'type' | 'value' | 'defaultValue' | 'onSelect' | 'onBlur' | 'onKeyDown'
90+ > ;
9091
9192/**
9293 *
@@ -115,6 +116,7 @@ export const Picklist: (<MultiSelect extends boolean | undefined>(
115116 tooltip,
116117 tooltipIcon,
117118 elementRef : elementRef_ ,
119+ buttonRef : buttonRef_ ,
118120 dropdownRef : dropdownRef_ ,
119121 onSelect,
120122 onComplete,
@@ -160,7 +162,8 @@ export const Picklist: (<MultiSelect extends boolean | undefined>(
160162
161163 const elRef = useRef < HTMLDivElement | null > ( null ) ;
162164 const elementRef = useMergeRefs ( [ elRef , elementRef_ ] ) ;
163- const comboboxElRef = useRef < HTMLDivElement | null > ( null ) ;
165+ const buttonElRef = useRef < HTMLButtonElement | null > ( null ) ;
166+ const buttonRef = useMergeRefs ( [ buttonElRef , buttonRef_ ] ) ;
164167 const dropdownElRef = useRef < HTMLDivElement | null > ( null ) ;
165168 const dropdownRef = useMergeRefs ( [ dropdownElRef , dropdownRef_ ] ) ;
166169
@@ -194,11 +197,6 @@ export const Picklist: (<MultiSelect extends boolean | undefined>(
194197 } else {
195198 // set only one value
196199 setPicklistValues ( [ itemValue ] ) ;
197- setOpened ( false ) ;
198- setTimeout ( ( ) => {
199- comboboxElRef . current ?. focus ( ) ;
200- onComplete ?.( ) ;
201- } , 10 ) ;
202200 }
203201 } ) ;
204202
@@ -225,14 +223,29 @@ export const Picklist: (<MultiSelect extends boolean | undefined>(
225223 } ) ;
226224
227225 const onClick = useEventCallback ( ( ) => {
228- if ( ! disabled ) {
229- setOpened ( ( opened ) => ! opened ) ;
230- }
226+ setOpened ( ( opened ) => ! opened ) ;
227+ setTimeout ( ( ) => {
228+ focusToTargetItemEl ( ) ;
229+ } , 10 ) ;
231230 } ) ;
232231
233232 const onPicklistItemSelect = useEventCallback ( ( value : PicklistValue ) => {
234233 updateItemValue ( value ) ;
235234 onSelect ?.( value ) ;
235+ if ( ! multiSelect ) {
236+ // close if only single select
237+ setTimeout ( ( ) => {
238+ setOpened ( false ) ;
239+ onComplete ?.( ) ;
240+ buttonElRef . current ?. focus ( ) ;
241+ } , 200 ) ;
242+ }
243+ } ) ;
244+
245+ const onPicklistClose = useEventCallback ( ( ) => {
246+ buttonElRef . current ?. focus ( ) ;
247+ setOpened ( false ) ;
248+ onComplete ?.( ) ;
236249 } ) ;
237250
238251 const onBlur = useEventCallback ( ( ) => {
@@ -271,7 +284,7 @@ export const Picklist: (<MultiSelect extends boolean | undefined>(
271284 function getSelectedItemLabel ( ) {
272285 // many items selected
273286 if ( values . length > 1 ) {
274- return ` ${ values . length } ${ optionsSelectedText } ` ;
287+ return optionsSelectedText ;
275288 }
276289
277290 // one item
@@ -343,29 +356,35 @@ export const Picklist: (<MultiSelect extends boolean | undefined>(
343356 className = 'slds-combobox__form-element slds-input-has-icon slds-input-has-icon_right'
344357 role = 'none'
345358 >
346- < div
347- ref = { comboboxElRef }
359+ < Button
360+ id = { id }
361+ buttonRef = { buttonRef }
362+ { ...rprops }
363+ className = { inputClassNames }
364+ style = { { justifyContent : 'normal' } }
365+ type = 'neutral'
366+ disabled = { disabled }
367+ onClick = { disabled ? undefined : onClick }
368+ onBlur = { disabled ? undefined : onBlur }
369+ onKeyDown = { disabled ? undefined : onKeydown }
348370 role = 'combobox'
349371 tabIndex = { disabled ? - 1 : 0 }
350- className = { inputClassNames }
351372 aria-labelledby = { label ? id : undefined }
352373 aria-controls = { listboxId }
353374 aria-expanded = { opened }
354375 aria-haspopup = 'listbox'
355376 aria-disabled = { disabled }
356- onClick = { onClick }
357- onKeyDown = { onKeydown }
358- onBlur = { onBlur }
359- { ...rprops }
360377 >
361378 < span className = 'slds-truncate' > { getSelectedItemLabel ( ) } </ span >
362- </ div >
363- < span className = 'slds-icon_container slds-icon-utility-down slds-input__icon slds-input__icon_right' >
364- < Icon
365- icon = 'down'
366- className = 'slds-icon slds-icon_x-small slds-icon-text-default'
367- />
368- </ span >
379+ < div >
380+ < Icon
381+ container = { true }
382+ containerClassName = 'slds-icon-utility-down slds-input__icon slds-input__icon_right'
383+ icon = 'down'
384+ className = 'slds-icon slds-icon_x-small slds-icon-text-default'
385+ />
386+ </ div >
387+ </ Button >
369388 </ div >
370389 { opened && (
371390 < DropdownMenu
@@ -374,10 +393,7 @@ export const Picklist: (<MultiSelect extends boolean | undefined>(
374393 size = { menuSize }
375394 style = { menuStyle }
376395 onMenuSelect = { onPicklistItemSelect }
377- onMenuClose = { ( ) => {
378- setOpened ( false ) ;
379- onComplete ?.( ) ;
380- } }
396+ onMenuClose = { onPicklistClose }
381397 onBlur = { onBlur }
382398 >
383399 < PicklistContext . Provider value = { contextValue } >
0 commit comments