Skip to content

Commit

Permalink
fix: Arrow logic adjust (#753)
Browse files Browse the repository at this point in the history
* fix: arrow offset

* chore: fix offset logic
  • Loading branch information
zombieJ authored Feb 7, 2024
1 parent e43d866 commit 277bf75
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
6 changes: 4 additions & 2 deletions docs/examples/debug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ export default () => {

return (
<div>
<RangePicker {...sharedLocale} open picker="time" style={{ width: 400 }} />

<div style={{ display: 'flex', flexWrap: 'wrap', gap: 16 }}>
<PickerPanel
{/* <PickerPanel
generateConfig={dayjsGenerateConfig}
locale={zhCN}
value={value}
Expand All @@ -156,7 +158,7 @@ export default () => {
console.error('1');
console.log('🎲 PanelValue Change:', panelValue, mode);
}}
/>
/> */}
</div>
</div>
);
Expand Down
12 changes: 9 additions & 3 deletions src/PickerInput/Popup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default function Popup<DateType extends object = any>(props: PopupProps<D
const rtl = direction === 'rtl';

// ========================= Refs =========================
const arrowRef = React.useRef<HTMLDivElement>(null);
const wrapperRef = React.useRef<HTMLDivElement>(null);

// ======================== Offset ========================
Expand All @@ -90,10 +91,14 @@ export default function Popup<DateType extends object = any>(props: PopupProps<D
// `activeOffset` is always align with the active input element
// So we need only check container contains the `activeOffset`
if (range) {
if (activeOffset + containerWidth < wrapperRef.current?.offsetWidth) {
setContainerOffset(activeOffset);
} else {
// Offset in case container has border radius
const arrowWidth = arrowRef.current?.offsetWidth || 0;

const maxOffset = containerWidth - arrowWidth;
if (activeOffset <= maxOffset) {
setContainerOffset(0);
} else {
setContainerOffset(activeOffset + arrowWidth - containerWidth);
}
}
}, [containerWidth, activeOffset, range]);
Expand Down Expand Up @@ -197,6 +202,7 @@ export default function Popup<DateType extends object = any>(props: PopupProps<D
className={classNames(`${prefixCls}-range-wrapper`, `${prefixCls}-${picker}-range-wrapper`)}
>
<div
ref={arrowRef}
className={`${prefixCls}-range-arrow`}
style={{ [rtl ? 'right' : 'left']: activeOffset }}
/>
Expand Down
4 changes: 3 additions & 1 deletion src/PickerInput/Selector/RangeSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ function RangeSelector<DateType extends object = any>(

const syncActiveOffset = useEvent(() => {
const input = getInput(activeIndex);
if (input) {
if (activeIndex === 0) {
onActiveOffset(0);
} else if (input) {
const { offsetWidth, offsetLeft, offsetParent } = input.nativeElement;

let offset = offsetLeft;
Expand Down
5 changes: 3 additions & 2 deletions tests/range.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,7 @@ describe('Picker.Range', () => {
offsetWidth: {
get() {
if (this.className.includes('range-arrow')) {
return 14;
return 0;
} else if (this.className.includes('panel-container')) {
return 312;
} else if (this.className.includes('input')) {
Expand Down Expand Up @@ -1695,7 +1695,7 @@ describe('Picker.Range', () => {
offsetWidth: {
get() {
if (this.className.includes('range-arrow')) {
return 14;
return 0;
} else if (this.className.includes('panel-container')) {
return 312;
} else if (this.className.includes('input')) {
Expand All @@ -1722,6 +1722,7 @@ describe('Picker.Range', () => {
/>,
);
openPicker(container, 1);
console.log(document.querySelector<HTMLElement>('.rc-picker-panel-container').style.cssText);
expect(document.querySelector('.rc-picker-panel-container')).toHaveStyle({ marginLeft: 0 });
mock.mockRestore();
});
Expand Down

0 comments on commit 277bf75

Please sign in to comment.