Skip to content

Commit d25d315

Browse files
committed
fix(ui): fix get item by index maybe undefined
1 parent 80af417 commit d25d315

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

packages/ui/src/components/slides/Slides.tsx

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ export function DSlides<ID extends DId, T extends DSlideItem<ID>>(props: DSlides
127127
dataRef.current.clearTid = async.setTimeout(
128128
() => {
129129
dataRef.current.clearTid = undefined;
130-
changeActiveId(dList[(activeIndex + 1) % dList.length].id);
130+
const id = nth(dList, (activeIndex + 1) % dList.length)?.id;
131+
if (!isUndefined(id)) {
132+
changeActiveId(id);
133+
}
131134
},
132135
autoplay.delay,
133136
() => {
@@ -219,10 +222,16 @@ export function DSlides<ID extends DId, T extends DSlideItem<ID>>(props: DSlides
219222
}
220223
if (newIndex === activeIndex) {
221224
if (performance.now() - dataRef.current.startDragTime < 300 && Math.abs(dragDistance) > 30) {
222-
changeActiveId(dList[Math.max(newIndex - 1, 0)].id);
225+
const id = nth(dList, Math.max(newIndex - 1, 0))?.id;
226+
if (!isUndefined(id)) {
227+
changeActiveId(id);
228+
}
223229
}
224230
} else {
225-
changeActiveId(dList[newIndex].id);
231+
const id = nth(dList, newIndex)?.id;
232+
if (!isUndefined(id)) {
233+
changeActiveId(id);
234+
}
226235
}
227236
} else {
228237
let newIndex = activeIndex;
@@ -237,10 +246,16 @@ export function DSlides<ID extends DId, T extends DSlideItem<ID>>(props: DSlides
237246
}
238247
if (newIndex === activeIndex) {
239248
if (performance.now() - dataRef.current.startDragTime < 300 && Math.abs(dragDistance) > 30) {
240-
changeActiveId(dList[Math.min(newIndex + 1, dList.length - 1)].id);
249+
const id = nth(dList, Math.min(newIndex + 1, dList.length - 1))?.id;
250+
if (!isUndefined(id)) {
251+
changeActiveId(id);
252+
}
241253
}
242254
} else {
243-
changeActiveId(dList[newIndex].id);
255+
const id = nth(dList, newIndex)?.id;
256+
if (!isUndefined(id)) {
257+
changeActiveId(id);
258+
}
244259
}
245260
}
246261
}
@@ -363,7 +378,10 @@ export function DSlides<ID extends DId, T extends DSlideItem<ID>>(props: DSlides
363378
tabIndex={-1}
364379
disabled={activeIndex === 0}
365380
onClick={() => {
366-
changeActiveId(dList[activeIndex - 1].id);
381+
const id = nth(dList, activeIndex - 1)?.id;
382+
if (!isUndefined(id)) {
383+
changeActiveId(id);
384+
}
367385
}}
368386
>
369387
<LeftOutlined />
@@ -375,7 +393,10 @@ export function DSlides<ID extends DId, T extends DSlideItem<ID>>(props: DSlides
375393
tabIndex={-1}
376394
disabled={activeIndex === dList.length - 1}
377395
onClick={() => {
378-
changeActiveId(dList[activeIndex + 1].id);
396+
const id = nth(dList, activeIndex + 1)?.id;
397+
if (!isUndefined(id)) {
398+
changeActiveId(id);
399+
}
379400
}}
380401
>
381402
<RightOutlined />

packages/ui/src/components/stepper/Stepper.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isNumber, isUndefined } from 'lodash';
1+
import { isNumber, isUndefined, nth } from 'lodash';
22

33
import { CheckOutlined, CloseOutlined } from '@react-devui/icons';
44
import { checkNodeExist, getClassName } from '@react-devui/utils';
@@ -46,7 +46,7 @@ export function DStepper<T extends DStepperItem>(props: DStepperProps<T>): JSX.E
4646
const dPrefix = usePrefixConfig();
4747
//#endregion
4848

49-
const active = dActive ?? dList[0].step ?? 1;
49+
const active = dActive ?? nth(dList, 0)?.step ?? 1;
5050

5151
return (
5252
<div

0 commit comments

Comments
 (0)