Skip to content

Commit

Permalink
fix(ui): fix get item by index maybe undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
xiejay97 committed Nov 22, 2022
1 parent 80af417 commit d25d315
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
35 changes: 28 additions & 7 deletions packages/ui/src/components/slides/Slides.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ export function DSlides<ID extends DId, T extends DSlideItem<ID>>(props: DSlides
dataRef.current.clearTid = async.setTimeout(
() => {
dataRef.current.clearTid = undefined;
changeActiveId(dList[(activeIndex + 1) % dList.length].id);
const id = nth(dList, (activeIndex + 1) % dList.length)?.id;
if (!isUndefined(id)) {
changeActiveId(id);
}
},
autoplay.delay,
() => {
Expand Down Expand Up @@ -219,10 +222,16 @@ export function DSlides<ID extends DId, T extends DSlideItem<ID>>(props: DSlides
}
if (newIndex === activeIndex) {
if (performance.now() - dataRef.current.startDragTime < 300 && Math.abs(dragDistance) > 30) {
changeActiveId(dList[Math.max(newIndex - 1, 0)].id);
const id = nth(dList, Math.max(newIndex - 1, 0))?.id;
if (!isUndefined(id)) {
changeActiveId(id);
}
}
} else {
changeActiveId(dList[newIndex].id);
const id = nth(dList, newIndex)?.id;
if (!isUndefined(id)) {
changeActiveId(id);
}
}
} else {
let newIndex = activeIndex;
Expand All @@ -237,10 +246,16 @@ export function DSlides<ID extends DId, T extends DSlideItem<ID>>(props: DSlides
}
if (newIndex === activeIndex) {
if (performance.now() - dataRef.current.startDragTime < 300 && Math.abs(dragDistance) > 30) {
changeActiveId(dList[Math.min(newIndex + 1, dList.length - 1)].id);
const id = nth(dList, Math.min(newIndex + 1, dList.length - 1))?.id;
if (!isUndefined(id)) {
changeActiveId(id);
}
}
} else {
changeActiveId(dList[newIndex].id);
const id = nth(dList, newIndex)?.id;
if (!isUndefined(id)) {
changeActiveId(id);
}
}
}
}
Expand Down Expand Up @@ -363,7 +378,10 @@ export function DSlides<ID extends DId, T extends DSlideItem<ID>>(props: DSlides
tabIndex={-1}
disabled={activeIndex === 0}
onClick={() => {
changeActiveId(dList[activeIndex - 1].id);
const id = nth(dList, activeIndex - 1)?.id;
if (!isUndefined(id)) {
changeActiveId(id);
}
}}
>
<LeftOutlined />
Expand All @@ -375,7 +393,10 @@ export function DSlides<ID extends DId, T extends DSlideItem<ID>>(props: DSlides
tabIndex={-1}
disabled={activeIndex === dList.length - 1}
onClick={() => {
changeActiveId(dList[activeIndex + 1].id);
const id = nth(dList, activeIndex + 1)?.id;
if (!isUndefined(id)) {
changeActiveId(id);
}
}}
>
<RightOutlined />
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/src/components/stepper/Stepper.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNumber, isUndefined } from 'lodash';
import { isNumber, isUndefined, nth } from 'lodash';

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

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

return (
<div
Expand Down

0 comments on commit d25d315

Please sign in to comment.