Skip to content

fix: activeBar offset not right when start from the opposite direction #890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion 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 @@ -188,7 +191,7 @@ function RangeSelector<DateType extends object = any>(
const parentWidth = (offsetParent as HTMLElement)?.offsetWidth || 0;
const activeOffset = placementRight ? (parentWidth - offsetWidth - offsetLeft) : offsetLeft;
setActiveBarStyle((ori) => ({
...ori,
...omit(ori, [prevOffsetUnit]),
width: offsetWidth,
[offsetUnit]: 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;
}

return prevRef.current;
}

export default usePrevious;