Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/PickerInput/Selector/RangeSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import classNames from 'classnames';
import ResizeObserver from 'rc-resize-observer';
import { useEvent } from 'rc-util';
import omit from 'rc-util/lib/omit';
import * as React from 'react';
import type { RangePickerRef, SelectorProps } from '../../interface';
import PickerContext from '../context';
Expand All @@ -9,6 +10,7 @@ import useRootProps from './hooks/useRootProps';
import Icon, { ClearIcon } from './Icon';
import Input, { type InputRef } from './Input';
import { getoffsetUnit, getRealPlacement } from '../../utils/uiUtil';
import usePrevious from './hooks/usePrevious';

export type SelectorIdType =
| string
Expand Down Expand Up @@ -175,6 +177,7 @@ function RangeSelector<DateType extends object = any>(
// ====================== ActiveBar =======================
const realPlacement = getRealPlacement(placement, rtl);
const offsetUnit = getoffsetUnit(realPlacement, rtl);
const prevOffsetUnit = usePrevious<any>(offsetUnit);
const placementRight = realPlacement?.toLowerCase().endsWith('right');
const [activeBarStyle, setActiveBarStyle] = React.useState<React.CSSProperties>({
position: 'absolute',
Expand All @@ -186,12 +189,17 @@ function RangeSelector<DateType extends object = any>(
if (input) {
const { offsetWidth, offsetLeft, offsetParent } = input.nativeElement;
const parentWidth = (offsetParent as HTMLElement)?.offsetWidth || 0;
const activeOffset = placementRight ? (parentWidth - offsetWidth - offsetLeft) : offsetLeft;
setActiveBarStyle((ori) => ({
...ori,
width: offsetWidth,
[offsetUnit]: activeOffset,
}));
const activeOffset = placementRight ? parentWidth - offsetWidth - offsetLeft : offsetLeft;
setActiveBarStyle((ori) =>
omit(
{
...ori,
width: offsetWidth,
[offsetUnit]: activeOffset,
},
[prevOffsetUnit],
),
);
onActiveOffset(activeOffset);
}
});
Expand Down
15 changes: 15 additions & 0 deletions src/PickerInput/Selector/hooks/usePrevious.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useRef } from 'react';

function usePrevious<T>(state: T): T | undefined {
const prevRef = useRef<T>();
const curRef = useRef<T>();

if (Object.is(curRef.current, state)) {
prevRef.current = curRef.current;
curRef.current = state;

Check warning on line 9 in src/PickerInput/Selector/hooks/usePrevious.ts

View check run for this annotation

Codecov / codecov/patch

src/PickerInput/Selector/hooks/usePrevious.ts#L8-L9

Added lines #L8 - L9 were not covered by tests
}

return prevRef.current;
}

export default usePrevious;