@@ -4,6 +4,7 @@ import type {
4
4
OptionData ,
5
5
OptionGroupData ,
6
6
FlattenOptionData ,
7
+ FieldNames ,
7
8
} from '../interface' ;
8
9
import type {
9
10
LabelValueType ,
@@ -32,32 +33,56 @@ function getKey(data: OptionData | OptionGroupData, index: number) {
32
33
return `rc-index-key-${ index } ` ;
33
34
}
34
35
36
+ export function fillFieldNames ( fieldNames ?: FieldNames ) {
37
+ const { label, value, options } = fieldNames || { } ;
38
+
39
+ return {
40
+ label : label || 'label' ,
41
+ value : value || 'value' ,
42
+ options : options || 'options' ,
43
+ } ;
44
+ }
45
+
35
46
/**
36
47
* Flat options into flatten list.
37
48
* We use `optionOnly` here is aim to avoid user use nested option group.
38
49
* Here is simply set `key` to the index if not provided.
39
50
*/
40
- export function flattenOptions ( options : SelectOptionsType ) : FlattenOptionData [ ] {
51
+ export function flattenOptions (
52
+ options : SelectOptionsType ,
53
+ { fieldNames } : { fieldNames ?: FieldNames } = { } ,
54
+ ) : FlattenOptionData [ ] {
41
55
const flattenList : FlattenOptionData [ ] = [ ] ;
42
56
57
+ const {
58
+ label : fieldLabel ,
59
+ value : fieldValue ,
60
+ options : fieldOptions ,
61
+ } = fillFieldNames ( fieldNames ) ;
62
+
43
63
function dig ( list : SelectOptionsType , isGroupOption : boolean ) {
44
64
list . forEach ( ( data ) => {
45
- if ( isGroupOption || ! ( 'options' in data ) ) {
65
+ const label = data [ fieldLabel ] ;
66
+
67
+ if ( isGroupOption || ! ( fieldOptions in data ) ) {
46
68
// Option
47
69
flattenList . push ( {
48
70
key : getKey ( data , flattenList . length ) ,
49
71
groupOption : isGroupOption ,
50
72
data,
73
+ label,
74
+ value : data [ fieldValue ] ,
51
75
} ) ;
52
76
} else {
53
77
// Option Group
54
78
flattenList . push ( {
55
79
key : getKey ( data , flattenList . length ) ,
56
80
group : true ,
57
81
data,
82
+ label,
58
83
} ) ;
59
84
60
- dig ( data . options , true ) ;
85
+ dig ( data [ fieldOptions ] , true ) ;
61
86
}
62
87
} ) ;
63
88
}
@@ -94,11 +119,10 @@ export function findValueOption(
94
119
) : OptionData [ ] {
95
120
const optionMap : Map < RawValueType , OptionData > = new Map ( ) ;
96
121
97
- options . forEach ( ( flattenItem ) => {
98
- if ( ! flattenItem . group ) {
99
- const data = flattenItem . data as OptionData ;
122
+ options . forEach ( ( { data, group, value } ) => {
123
+ if ( ! group ) {
100
124
// Check if match
101
- optionMap . set ( data . value , data ) ;
125
+ optionMap . set ( value , data as OptionData ) ;
102
126
}
103
127
} ) ;
104
128
@@ -120,7 +144,7 @@ export function findValueOption(
120
144
export const getLabeledValue : GetLabeledValue < FlattenOptionData [ ] > = (
121
145
value ,
122
146
{ options, prevValueMap, labelInValue, optionLabelProp } ,
123
- ) => {
147
+ ) : LabelValueType => {
124
148
const item = findValueOption ( [ value ] , options ) [ 0 ] ;
125
149
const result : LabelValueType = {
126
150
value,
0 commit comments