Skip to content

Commit b923cdb

Browse files
committed
fix: activeBar offset not right when start from the opposite direction
Co-authored-by: Dmaziyo <[email protected]> close #889 ref: ant-design/ant-design#51480
1 parent 6db0043 commit b923cdb

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/PickerInput/Selector/RangeSelector.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import classNames from 'classnames';
22
import ResizeObserver from 'rc-resize-observer';
33
import { useEvent } from 'rc-util';
4+
import omit from 'rc-util/lib/omit';
45
import * as React from 'react';
56
import type { RangePickerRef, SelectorProps } from '../../interface';
67
import PickerContext from '../context';
@@ -9,6 +10,7 @@ import useRootProps from './hooks/useRootProps';
910
import Icon, { ClearIcon } from './Icon';
1011
import Input, { type InputRef } from './Input';
1112
import { getoffsetUnit, getRealPlacement } from '../../utils/uiUtil';
13+
import usePrevious from './hooks/usePrevious';
1214

1315
export type SelectorIdType =
1416
| string
@@ -175,6 +177,7 @@ function RangeSelector<DateType extends object = any>(
175177
// ====================== ActiveBar =======================
176178
const realPlacement = getRealPlacement(placement, rtl);
177179
const offsetUnit = getoffsetUnit(realPlacement, rtl);
180+
const prevOffsetUnit = usePrevious<any>(offsetUnit);
178181
const placementRight = realPlacement?.toLowerCase().endsWith('right');
179182
const [activeBarStyle, setActiveBarStyle] = React.useState<React.CSSProperties>({
180183
position: 'absolute',
@@ -186,12 +189,17 @@ function RangeSelector<DateType extends object = any>(
186189
if (input) {
187190
const { offsetWidth, offsetLeft, offsetParent } = input.nativeElement;
188191
const parentWidth = (offsetParent as HTMLElement)?.offsetWidth || 0;
189-
const activeOffset = placementRight ? (parentWidth - offsetWidth - offsetLeft) : offsetLeft;
190-
setActiveBarStyle((ori) => ({
191-
...ori,
192-
width: offsetWidth,
193-
[offsetUnit]: activeOffset,
194-
}));
192+
const activeOffset = placementRight ? parentWidth - offsetWidth - offsetLeft : offsetLeft;
193+
setActiveBarStyle((ori) =>
194+
omit(
195+
{
196+
...ori,
197+
width: offsetWidth,
198+
[offsetUnit]: activeOffset,
199+
},
200+
[prevOffsetUnit],
201+
),
202+
);
195203
onActiveOffset(activeOffset);
196204
}
197205
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useRef } from 'react';
2+
3+
function usePrevious<T>(state: T): T | undefined {
4+
const prevRef = useRef<T>();
5+
const curRef = useRef<T>();
6+
7+
if (Object.is(curRef.current, state)) {
8+
prevRef.current = curRef.current;
9+
curRef.current = state;
10+
}
11+
12+
return prevRef.current;
13+
}
14+
15+
export default usePrevious;

0 commit comments

Comments
 (0)