@@ -9,7 +9,14 @@ import addClass from "dom-helpers/addClass";
99import removeClass from "dom-helpers/removeClass" ;
1010import scrollIntoView from "dom-helpers/scrollTo" ;
1111
12- export type Item = Record < string | number , any > ;
12+ export type ItemData = Record < string | number , any > ;
13+
14+ export type Item = {
15+ value : any ;
16+ label : React . ReactNode ;
17+ children ?: Item [ ] ;
18+ data : ItemData ;
19+ } ;
1320
1421export interface ListBoxProps {
1522 prefixCls : string ;
@@ -29,6 +36,7 @@ export interface ListBoxProps {
2936 headerStyle : any ;
3037 footerStyle : any ;
3138 bodyStyle : any ;
39+ data : ItemData [ ] ;
3240 items : any ;
3341 itemsMap : any ;
3442 emptyLabel : any ;
@@ -51,7 +59,11 @@ export interface ListBoxProps {
5159 bodyWrapperComponent : any ;
5260 footerWrapperComponent : any ;
5361}
54- export interface ListBoxState { }
62+ export interface ListBoxState {
63+ items : Item [ ] ;
64+ itemsMap : Record < any , Item > ;
65+ value : any [ ] ;
66+ }
5567
5668function isIE ( ) {
5769 const userAgent = navigator . userAgent ;
@@ -67,12 +79,17 @@ function copy(data) {
6779 return isArray ( data ) ? [ ] . concat ( data ) : data ;
6880}
6981
70- function getItemsMap ( props ) {
71- const { items, valueField, childrenField } = props ;
72- const maps = { } ;
82+ function dataProcessor ( props : ListBoxProps ) {
83+ const { data, valueField, childrenField, labelField } = props ;
84+ const items : Item [ ] = [ ] ;
85+ const itemsMap : Record < any , Item > = { } ;
7386
74- function toMaps ( items ) {
75- items . forEach ( ( item ) => {
87+ function walk ( data : Item [ ] , pChildren : Item [ ] ) {
88+ data . forEach ( ( item ) => {
89+ pChildren . push ( {
90+ value : data [ valueField ] ,
91+ label : data [ labelField ] ,
92+ } ) ;
7693 if ( item [ childrenField ] && Array . isArray ( item [ childrenField ] ) ) {
7794 toMaps ( item [ childrenField ] ) ;
7895 } else {
@@ -81,7 +98,7 @@ function getItemsMap(props) {
8198 } ) ;
8299 }
83100
84- toMaps ( items ) ;
101+ walk ( data , items ) ;
85102
86103 return maps ;
87104}
@@ -97,7 +114,7 @@ export class ListBox extends React.Component<ListBoxProps, ListBoxState> {
97114 emptyLabel : "Not Found" ,
98115 enableDownUpSelect : true ,
99116 fixListBodyHeightOnIE : true ,
100- items : [ ] ,
117+ data : [ ] ,
101118 itemsMap : null ,
102119 onFocus : noop ,
103120 onBlur : noop ,
@@ -108,19 +125,42 @@ export class ListBox extends React.Component<ListBoxProps, ListBoxState> {
108125 footerWrapperComponent : "div" ,
109126 } ;
110127
111- constructor ( props ) {
112- super ( props ) ;
128+ static getDerivedStateFromProps ( nextProps : ListBoxProps , state : ListBoxState ) {
129+ // const value = nextProps.value;
130+ // let itemsMap = nextProps.itemsMap || {};
131+
132+ // if (!nextProps.itemsMap) {
133+ // itemsMap = getItemsMap(nextProps);
134+ // }
135+
136+ // const newState: Partial<ListBoxState> = {
137+ // itemsMap,
138+ // };
139+
140+ return {
141+ ...dataProcessor ( nextProps ) ,
142+ value :
143+ nextProps . value === undefined
144+ ? state . value
145+ : Array . isArray ( nextProps . value )
146+ ? nextProps . value
147+ : [ nextProps . value ] ,
148+ } ;
149+ }
113150
114- const selectedValue = [ ] ;
115- let value ;
151+ constructor ( props : ListBoxProps , context : any ) {
152+ super ( props , context ) ;
116153
117- if ( ! isUndefined ( props . defaultValue ) ) {
118- value = isArray ( props . defaultValue ) ? props . defaultValue : [ props . defaultValue ] ;
119- }
154+ // const selectedValue = [];
155+ // let value;
120156
121- if ( value ) {
122- selectedValue . push ( ...value ) ;
123- }
157+ // if (!isUndefined(props.defaultValue)) {
158+ // value = isArray(props.defaultValue) ? props.defaultValue : [props.defaultValue];
159+ // }
160+
161+ // if (value) {
162+ // selectedValue.push(...value);
163+ // }
124164
125165 //item 索引id
126166 this . _itemIndex = 0 ;
@@ -129,29 +169,17 @@ export class ListBox extends React.Component<ListBoxProps, ListBoxState> {
129169 this . _activeIndex = null ;
130170
131171 this . state = {
132- selectedValue,
172+ items : [ ] ,
173+ itemsMap : { } ,
174+ value :
175+ props . defaultValue === undefined
176+ ? [ ]
177+ : Array . isArray ( props . defaultValue )
178+ ? props . defaultValue
179+ : [ props . defaultValue ] ,
133180 } ;
134181 }
135182
136- static getDerivedStateFromProps ( nextProps , prevState ) {
137- const value = nextProps . value ;
138- let itemsMap = nextProps . itemsMap || { } ;
139-
140- if ( ! nextProps . itemsMap ) {
141- itemsMap = getItemsMap ( nextProps ) ;
142- }
143-
144- const newState = {
145- itemsMap,
146- } ;
147-
148- if ( ! isUndefined ( value ) ) {
149- newState . selectedValue = isArray ( value ) ? copy ( value ) : [ value ] ;
150- }
151-
152- return newState ;
153- }
154-
155183 componentDidMount ( ) {
156184 const { prefixCls, autoFocus } = this . props ;
157185 const el = findDOMNode ( this ) ;
0 commit comments