Skip to content

Commit 05eeced

Browse files
committed
fix: active bar always offset 1px
1 parent d02ddec commit 05eeced

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/PickerInput/Selector/RangeSelector.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import useRootProps from './hooks/useRootProps';
99
import Icon, { ClearIcon } from './Icon';
1010
import Input, { type InputRef } from './Input';
1111
import { getoffsetUnit, getRealPlacement } from '../../utils/uiUtil';
12+
import { getWin } from './util';
1213

1314
export type SelectorIdType =
1415
| string
@@ -184,13 +185,26 @@ function RangeSelector<DateType extends object = any>(
184185
const syncActiveOffset = useEvent(() => {
185186
const input = getInput(activeIndex);
186187
if (input) {
187-
const { offsetWidth, offsetLeft, offsetParent } = input.nativeElement;
188-
const parentWidth = (offsetParent as HTMLElement)?.offsetWidth || 0;
189-
const activeOffset = placementRight ? parentWidth - offsetWidth - offsetLeft : offsetLeft;
188+
const { offsetParent } = input.nativeElement;
189+
// offsetLeft is an integer, which will cause incorrect reulst.
190+
const { x = 0, width: inputWidth = 0 } = input.nativeElement.getBoundingClientRect() || {};
191+
const { x: pX = 0, width: parentWidth = 0 } = offsetParent?.getBoundingClientRect() || {};
192+
const parentStyles =
193+
offsetParent && getWin(offsetParent as HTMLElement).getComputedStyle(offsetParent);
194+
const parentBorderRightWidth = Number(
195+
(placementRight ? parentStyles?.borderRightWidth : parentStyles?.borderLeftWidth)?.replace(
196+
'px',
197+
'',
198+
) || 0,
199+
);
200+
const offsetLeft = x - pX;
201+
202+
const activeOffset = placementRight ? parentWidth - inputWidth - offsetLeft : offsetLeft;
190203
setActiveBarStyle(({ position }) => ({
191204
position,
192-
width: offsetWidth,
193-
[offsetUnit]: activeOffset,
205+
width: inputWidth,
206+
// parent will have border while focus, so need to cut `parentBorderWidth` on opposite side.
207+
[offsetUnit]: activeOffset - parentBorderRightWidth,
194208
}));
195209
onActiveOffset(activeOffset);
196210
}

src/PickerInput/Selector/util.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ export function getMaskRange(key: string): [startVal: number, endVal: number, de
1212
};
1313

1414
return PresetRange[key];
15-
}
15+
}
16+
17+
export function getWin(ele: HTMLElement) {
18+
return ele.ownerDocument.defaultView;
19+
}

0 commit comments

Comments
 (0)