@@ -4,7 +4,7 @@ import Select, {
4
4
MultiValueProps ,
5
5
SingleValue as SingleValueT ,
6
6
} from "react-select" ;
7
- import { Body , ListSize , LocalizedString } from ".." ;
7
+ import { Body , Chip , ListSize , LocalizedString } from ".." ;
8
8
import { useField } from "@react-aria/label" ;
9
9
import { useEffect , useMemo } from "react" ;
10
10
import { FieldProps } from "../Field/FieldProps" ;
@@ -24,11 +24,20 @@ export type SelectOption<A> = Omit<
24
24
25
25
type MultiProps < A > = {
26
26
isMulti : true ;
27
- multiValueMessage ?: ( numberOfSelectedOptions : number ) => LocalizedString ;
28
27
showMultiSelectBulkActions ?: boolean ;
29
28
selectAllButtonLabel ?: LocalizedString ;
30
29
clearAllButtonLabel ?: LocalizedString ;
31
- } & FieldProps < A [ ] > ;
30
+ } & FieldProps < A [ ] > &
31
+ (
32
+ | {
33
+ multiSelectMode ?: "summary" ;
34
+ multiValueMessage ?: ( numberOfSelectedOptions : number ) => LocalizedString ;
35
+ }
36
+ | {
37
+ multiSelectMode : "chips" ;
38
+ multiValueMessage ?: never ;
39
+ }
40
+ ) ;
32
41
33
42
type SingleProps < A > = {
34
43
isMulti ?: false ;
@@ -43,6 +52,8 @@ type Props<A> = {
43
52
searchable ?: boolean ;
44
53
} & ( SingleProps < A > | MultiProps < A > ) ;
45
54
55
+ export type { Props as SelectFieldProps } ;
56
+
46
57
declare module "react-select/dist/declarations/src/Select" {
47
58
export interface Props < Option , IsMulti extends boolean , Group extends GroupBase < Option > > {
48
59
menuSize ?: ListSize ;
@@ -52,6 +63,7 @@ declare module "react-select/dist/declarations/src/Select" {
52
63
showMultiSelectBulkActions ?: boolean ;
53
64
selectAllButtonLabel ?: LocalizedString ;
54
65
clearAllButtonLabel ?: LocalizedString ;
66
+ multiSelectMode ?: "summary" | "chips" ;
55
67
}
56
68
}
57
69
@@ -165,7 +177,7 @@ export function SelectField<A>(props: Props<A>) {
165
177
isClearable = { false }
166
178
noOptionsMessage = { ( ) => noOptionsMessage ?? defaultMessages . SelectField . noOptionsMessage }
167
179
multiValueMessage = {
168
- isMulti
180
+ props . isMulti && ( ! props . multiSelectMode || props . multiSelectMode === "summary" )
169
181
? props . multiValueMessage ?? defaultMessages . SelectField . multiOptionsSelected
170
182
: undefined
171
183
}
@@ -185,6 +197,7 @@ export function SelectField<A>(props: Props<A>) {
185
197
? props . selectAllButtonLabel ?? defaultMessages . SelectField . selectAllButtonLabel
186
198
: undefined
187
199
}
200
+ multiSelectMode = { isMulti ? props . multiSelectMode : undefined }
188
201
/>
189
202
</ Field >
190
203
</ BentoConfigProvider >
@@ -194,23 +207,33 @@ export function SelectField<A>(props: Props<A>) {
194
207
// NOTE(gabro): we override MultiValue instead of ValueContainer (which would be more natural)
195
208
// because overriding ValueContainer breaks the logic for closing the menu when clicking away.
196
209
// See: https://github.com/JedWatson/react-select/issues/2239#issuecomment-861848975
197
- function MultiValue < A > ( props : MultiValueProps < A > ) {
210
+ function MultiValue < A extends SelectOption < unknown > > ( props : MultiValueProps < A > ) {
198
211
const inputConfig = useBentoConfig ( ) . input ;
199
- const numberOfSelectedOptions = props . getValue ( ) . length ;
212
+ const dropdownConfig = useBentoConfig ( ) . dropdown ;
213
+ switch ( props . selectProps . multiSelectMode ?? "summary" ) {
214
+ case "summary" :
215
+ const numberOfSelectedOptions = props . getValue ( ) . length ;
200
216
201
- if ( props . index > 0 || ! props . selectProps . multiValueMessage ) {
202
- return null ;
203
- }
217
+ if ( props . index > 0 || ! props . selectProps . multiValueMessage ) {
218
+ return null ;
219
+ }
204
220
205
- if ( numberOfSelectedOptions === 1 ) {
206
- return selectComponents . SingleValue ( props ) ;
207
- }
221
+ if ( numberOfSelectedOptions === 1 ) {
222
+ return selectComponents . SingleValue ( props ) ;
223
+ }
208
224
209
- return (
210
- < Body size = { inputConfig . fontSize } >
211
- { props . selectProps . multiValueMessage ( numberOfSelectedOptions ) }
212
- </ Body >
213
- ) ;
225
+ return (
226
+ < Body size = { inputConfig . fontSize } >
227
+ { props . selectProps . multiValueMessage ( numberOfSelectedOptions ) }
228
+ </ Body >
229
+ ) ;
230
+ case "chips" :
231
+ return (
232
+ < Chip
233
+ color = { dropdownConfig . chipColor }
234
+ label = { props . data . label as LocalizedString }
235
+ onDismiss = { props . removeProps . onClick as ( ) => void }
236
+ />
237
+ ) ;
238
+ }
214
239
}
215
-
216
- export type { Props as SelectFieldProps } ;
0 commit comments