@@ -20,6 +20,7 @@ import {ComboBox, Item, Section} from '../';
20
20
import Copy from '@spectrum-icons/workflow/Copy' ;
21
21
import Draw from '@spectrum-icons/workflow/Draw' ;
22
22
import { Flex } from '@react-spectrum/layout' ;
23
+ import { mergeProps } from '@react-aria/utils' ;
23
24
import React , { useState } from 'react' ;
24
25
import { storiesOf } from '@storybook/react' ;
25
26
import { Text } from '@react-spectrum/text' ;
@@ -179,7 +180,7 @@ storiesOf('ComboBox', module)
179
180
)
180
181
. add (
181
182
'isOpen' ,
182
- ( ) => < ControlledOpenCombobox isOpen /> ,
183
+ ( ) => < ControlledOpenCombobox isOpen allowsCustomValue defaultSelectedKey = "two" /> ,
183
184
{ note : 'Combobox needs focus to show dropdown.' }
184
185
)
185
186
. add (
@@ -419,6 +420,12 @@ storiesOf('ComboBox', module)
419
420
< AllControlledOpenComboBox selectedKey = "2" inputValue = "Kangaroo" disabledKeys = { [ '2' , '6' ] } />
420
421
)
421
422
)
423
+ . add (
424
+ 'inputValue, selectedKey, isOpen, allowsCustomValue (controlled)' ,
425
+ ( ) => (
426
+ < AllControlledOpenComboBox selectedKey = "2" inputValue = "Kangaroo" disabledKeys = { [ '2' , '6' ] } allowsCustomValue />
427
+ )
428
+ )
422
429
. add (
423
430
'custom filter' ,
424
431
( ) => (
@@ -540,8 +547,7 @@ let CustomFilterComboBox = (props) => {
540
547
541
548
return (
542
549
< ComboBox
543
- { ...actions }
544
- { ...props }
550
+ { ...mergeProps ( props , actions ) }
545
551
label = "Combobox"
546
552
items = { filteredItems }
547
553
inputValue = { filterValue }
@@ -642,7 +648,7 @@ let ControlledKeyComboBox = (props) => {
642
648
< Text > Clear key</ Text >
643
649
</ Button >
644
650
</ ButtonGroup >
645
- < ComboBox { ...props } selectedKey = { selectedKey } defaultItems = { withSection } label = "Combobox" onOpenChange = { action ( 'onOpenChange' ) } onInputChange = { action ( 'onInputChange' ) } onSelectionChange = { onSelectionChange } onBlur = { action ( 'onBlur' ) } onFocus = { action ( 'onFocus' ) } >
651
+ < ComboBox { ...mergeProps ( props , actions ) } selectedKey = { selectedKey } defaultItems = { withSection } label = "Combobox" onSelectionChange = { onSelectionChange } >
646
652
{ ( item : any ) => (
647
653
< Section items = { item . children } title = { item . name } >
648
654
{ ( item : any ) => < Item > { item . name } </ Item > }
@@ -674,7 +680,7 @@ let ControlledValueComboBox = (props) => {
674
680
< Text > Clear field</ Text >
675
681
</ Button >
676
682
</ ButtonGroup >
677
- < ComboBox { ...props } inputValue = { value } defaultItems = { withSection } label = "Combobox" onOpenChange = { action ( 'onOpenChange' ) } onInputChange = { onValueChange } onSelectionChange = { action ( 'onSelectionChange' ) } onBlur = { action ( 'onBlur' ) } onFocus = { action ( 'onFocus' ) } >
683
+ < ComboBox { ...mergeProps ( props , actions ) } inputValue = { value } defaultItems = { withSection } label = "Combobox" onInputChange = { onValueChange } >
678
684
{ ( item : any ) => (
679
685
< Section items = { item . children } title = { item . name } >
680
686
{ ( item : any ) => < Item > { item . name } </ Item > }
@@ -695,7 +701,7 @@ let CustomValueComboBox = (props) => {
695
701
return (
696
702
< div >
697
703
< div > Selected Key: { selectedKey } </ div >
698
- < ComboBox { ...props } selectedKey = { selectedKey } defaultItems = { withSection } label = "Combobox" onOpenChange = { action ( 'onOpenChange' ) } onInputChange = { action ( 'onInputChange' ) } onSelectionChange = { onSelectionChange } onBlur = { action ( 'onBlur' ) } onFocus = { action ( 'onFocus' ) } marginTop = { 20 } >
704
+ < ComboBox { ...mergeProps ( props , actions ) } selectedKey = { selectedKey } defaultItems = { withSection } label = "Combobox" onSelectionChange = { onSelectionChange } marginTop = { 20 } >
699
705
{ ( item : any ) => (
700
706
< Section items = { item . children } title = { item . name } >
701
707
{ ( item : any ) => < Item > { item . name } </ Item > }
@@ -712,7 +718,7 @@ let ControlledOpenCombobox = (props) => {
712
718
return (
713
719
< Flex direction = "column" >
714
720
< TextField label = "Email" />
715
- < ComboBox label = "Combobox" isOpen = { isOpen } { ...actions } onOpenChange = { setOpen } >
721
+ < ComboBox label = "Combobox" { ...mergeProps ( props , actions ) } isOpen = { isOpen } onOpenChange = { setOpen } >
716
722
< Item key = "one" > Item One</ Item >
717
723
< Item key = "two" textValue = "Item Two" >
718
724
< Copy size = "S" />
@@ -737,11 +743,11 @@ function AllControlledOpenComboBox(props) {
737
743
} ) ;
738
744
739
745
let onSelectionChange = ( key : React . Key ) => {
740
- setFieldState ( {
746
+ setFieldState ( prevState => ( {
741
747
isOpen : false ,
742
- inputValue : list . getItem ( key ) ?. value . name ?? '' ,
748
+ inputValue : list . getItem ( key ) ?. value . name ?? ( props . allowsCustomValue ? prevState . inputValue : '' ) ,
743
749
selectedKey : key
744
- } ) ;
750
+ } ) ) ;
745
751
} ;
746
752
747
753
let onInputChange = ( value : string ) => {
@@ -762,7 +768,7 @@ function AllControlledOpenComboBox(props) {
762
768
763
769
return (
764
770
< div >
765
- < ComboBox disabledKeys = { props . disabledKeys } selectedKey = { fieldState . selectedKey } inputValue = { fieldState . inputValue } defaultItems = { list . items } label = "Combobox" isOpen = { fieldState . isOpen } onOpenChange = { onOpenChange } onInputChange = { onInputChange } onSelectionChange = { onSelectionChange } onBlur = { action ( 'onBlur' ) } onFocus = { action ( 'onFocus' ) } >
771
+ < ComboBox allowsCustomValue = { props . allowsCustomValue } disabledKeys = { props . disabledKeys } selectedKey = { fieldState . selectedKey } inputValue = { fieldState . inputValue } defaultItems = { list . items } label = "Combobox" isOpen = { fieldState . isOpen } onOpenChange = { onOpenChange } onInputChange = { onInputChange } onSelectionChange = { onSelectionChange } onBlur = { action ( 'onBlur' ) } onFocus = { action ( 'onFocus' ) } >
766
772
{ item => (
767
773
< Section items = { item . children } title = { item . value . name } >
768
774
{ item => < Item > { item . value . name } </ Item > }
@@ -795,7 +801,7 @@ function ResizeCombobox() {
795
801
796
802
function render ( props = { } ) {
797
803
return (
798
- < ComboBox label = "Combobox" onOpenChange = { action ( 'onOpenChange' ) } onInputChange = { action ( 'onInputChange' ) } onSelectionChange = { action ( 'onSelectionChange' ) } onBlur = { action ( 'onBlur' ) } onFocus = { action ( 'onFocus' ) } { ...props } >
804
+ < ComboBox label = "Combobox" { ...mergeProps ( props , actions ) } >
799
805
< Item key = "one" > Item One</ Item >
800
806
< Item key = "two" textValue = "Item Two" >
801
807
< Copy size = "S" />
0 commit comments