@@ -14,6 +14,7 @@ import {
14
14
OnActiveValue ,
15
15
} from './interface' ;
16
16
import { RawValueType , FlattenOptionsType } from './interface/generator' ;
17
+ import useMemo from '../_util/hooks/useMemo' ;
17
18
export interface OptionListProps {
18
19
prefixCls : string ;
19
20
id : string ;
@@ -78,6 +79,12 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
78
79
setup ( props ) {
79
80
const itemPrefixCls = computed ( ( ) => `${ props . prefixCls } -item` ) ;
80
81
82
+ const memoFlattenOptions = useMemo (
83
+ ( ) => props . flattenOptions ,
84
+ [ ( ) => props . open , ( ) => props . flattenOptions ] ,
85
+ ( prev , next ) => next [ 0 ] && prev [ 1 ] !== next [ 1 ] ,
86
+ ) ;
87
+
81
88
// =========================== List ===========================
82
89
const listRef = createRef ( ) ;
83
90
@@ -93,12 +100,12 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
93
100
94
101
// ========================== Active ==========================
95
102
const getEnabledActiveIndex = ( index : number , offset = 1 ) => {
96
- const len = props . flattenOptions . length ;
103
+ const len = memoFlattenOptions . value . length ;
97
104
98
105
for ( let i = 0 ; i < len ; i += 1 ) {
99
106
const current = ( index + i * offset + len ) % len ;
100
107
101
- const { group, data } = props . flattenOptions [ current ] ;
108
+ const { group, data } = memoFlattenOptions . value [ current ] ;
102
109
if ( ! group && ! ( data as OptionData ) . disabled ) {
103
110
return current ;
104
111
}
@@ -115,7 +122,7 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
115
122
const info = { source : fromKeyboard ? ( 'keyboard' as const ) : ( 'mouse' as const ) } ;
116
123
117
124
// Trigger active event
118
- const flattenItem = props . flattenOptions [ index ] ;
125
+ const flattenItem = memoFlattenOptions . value [ index ] ;
119
126
if ( ! flattenItem ) {
120
127
props . onActiveValue ( null , - 1 , info ) ;
121
128
return ;
@@ -127,7 +134,7 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
127
134
// Auto active first item when list length or searchValue changed
128
135
129
136
watch (
130
- computed ( ( ) => [ props . flattenOptions . length , props . searchValue ] ) ,
137
+ [ ( ) => memoFlattenOptions . value . length , ( ) => props . searchValue ] ,
131
138
( ) => {
132
139
setActive ( props . defaultActiveFirstOption !== false ? getEnabledActiveIndex ( 0 ) : - 1 ) ;
133
140
} ,
@@ -136,11 +143,11 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
136
143
// Auto scroll to item position in single mode
137
144
138
145
watch (
139
- computed ( ( ) => props . open ) ,
146
+ ( ) => props . open ,
140
147
( ) => {
141
148
if ( ! props . multiple && props . open && props . values . size === 1 ) {
142
149
const value = Array . from ( props . values ) [ 0 ] ;
143
- const index = props . flattenOptions . findIndex ( ( { data } ) => data . value === value ) ;
150
+ const index = memoFlattenOptions . value . findIndex ( ( { data } ) => data . value === value ) ;
144
151
setActive ( index ) ;
145
152
scrollIntoView ( index ) ;
146
153
}
@@ -167,7 +174,7 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
167
174
} ;
168
175
169
176
function renderItem ( index : number ) {
170
- const item = props . flattenOptions [ index ] ;
177
+ const item = memoFlattenOptions . value [ index ] ;
171
178
if ( ! item ) return null ;
172
179
173
180
const itemData = ( item . data || { } ) as OptionData ;
@@ -188,6 +195,7 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
188
195
) : null ;
189
196
}
190
197
return {
198
+ memoFlattenOptions,
191
199
renderItem,
192
200
listRef,
193
201
state,
@@ -220,7 +228,7 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
220
228
// >>> Select
221
229
case KeyCode . ENTER : {
222
230
// value
223
- const item = props . flattenOptions [ state . activeIndex ] ;
231
+ const item = memoFlattenOptions . value [ state . activeIndex ] ;
224
232
if ( item && ! item . data . disabled ) {
225
233
onSelectValue ( item . data . value ) ;
226
234
} else {
@@ -258,14 +266,14 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
258
266
itemPrefixCls,
259
267
setActive,
260
268
onSelectValue,
269
+ memoFlattenOptions,
261
270
} = this as any ;
262
271
const {
263
272
id,
264
273
childrenAsData,
265
274
values,
266
275
height,
267
276
itemHeight,
268
- flattenOptions,
269
277
menuItemSelectedIcon,
270
278
notFoundContent,
271
279
virtual,
@@ -274,7 +282,7 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
274
282
} = this . $props as OptionListProps ;
275
283
const { activeIndex } = this . state ;
276
284
// ========================== Render ==========================
277
- if ( flattenOptions . length === 0 ) {
285
+ if ( memoFlattenOptions . length === 0 ) {
278
286
return (
279
287
< div
280
288
role = "listbox"
@@ -296,7 +304,7 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
296
304
< List
297
305
itemKey = "key"
298
306
ref = { listRef }
299
- data = { flattenOptions }
307
+ data = { memoFlattenOptions }
300
308
height = { height }
301
309
itemHeight = { itemHeight }
302
310
fullHeight = { false }
0 commit comments