Skip to content

Commit ff0ea10

Browse files
authored
fix: auto dropdown placement (#710)
* chore: auto dropdown placement * fix: dropdownMatchWidth logic
1 parent 14353d8 commit ff0ea10

File tree

5 files changed

+22
-22
lines changed

5 files changed

+22
-22
lines changed

assets/index.less

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,15 @@
301301
}
302302

303303
&-slide-up-enter&-slide-up-enter-active&-placement-bottomLeft,
304-
&-slide-up-appear&-slide-up-appear-active&-placement-bottomLeft {
304+
&-slide-up-appear&-slide-up-appear-active&-placement-bottomLeft,
305+
&-slide-up-enter&-slide-up-enter-active&-placement-bottomRight,
306+
&-slide-up-appear&-slide-up-appear-active&-placement-bottomRight {
305307
animation-name: rcSelectDropdownSlideUpIn;
306308
animation-play-state: running;
307309
}
308310

309-
&-slide-up-leave&-slide-up-leave-active&-placement-bottomLeft {
311+
&-slide-up-leave&-slide-up-leave-active&-placement-bottomLeft,
312+
&-slide-up-leave&-slide-up-leave-active&-placement-bottomRight {
310313
animation-name: rcSelectDropdownSlideUpOut;
311314
animation-play-state: running;
312315
}

docs/examples/single-animation.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ const Test = () => (
1717
allowClear
1818
placeholder="placeholder"
1919
defaultValue="lucy"
20-
style={{ width: 500 }}
20+
style={{ width: '100%' }}
2121
animation="slide-up"
2222
showSearch
2323
onChange={onChange}
24+
dropdownStyle={{
25+
width: 'auto',
26+
}}
2427
>
2528
<Option value="jack">
2629
<b

src/Select.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ const Select = React.forwardRef(
165165
// Select
166166
onSelect,
167167
onDeselect,
168-
dropdownMatchSelectWidth,
168+
dropdownMatchSelectWidth = true,
169169

170170
// Options
171171
filterOption,

src/SelectTrigger.tsx

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import Trigger from 'rc-trigger';
33
import classNames from 'classnames';
44
import type { Placement, RenderDOMFunc } from './BaseSelect';
55

6-
const getBuiltInPlacements = (adjustX: number) => {
6+
const getBuiltInPlacements = (dropdownMatchSelectWidth: number | boolean) => {
7+
// Enable horizontal overflow auto-adjustment when a custom dropdown width is provided
8+
const adjustX = dropdownMatchSelectWidth === true ? 0 : 1;
79
return {
810
bottomLeft: {
911
points: ['tl', 'bl'],
@@ -40,13 +42,6 @@ const getBuiltInPlacements = (adjustX: number) => {
4042
};
4143
};
4244

43-
const getAdjustX = (adjustXDependencies: Pick<SelectTriggerProps, 'autoAdjustOverflow' | 'dropdownMatchSelectWidth'>) => {
44-
const { autoAdjustOverflow, dropdownMatchSelectWidth } = adjustXDependencies;
45-
if(!!autoAdjustOverflow) return 1;
46-
// Enable horizontal overflow auto-adjustment when a custom dropdown width is provided
47-
return typeof dropdownMatchSelectWidth !== 'number' ? 0 : 1
48-
}
49-
5045
export interface RefTriggerProps {
5146
getPopupElement: () => HTMLDivElement;
5247
}
@@ -70,7 +65,6 @@ export interface SelectTriggerProps {
7065
getPopupContainer?: RenderDOMFunc;
7166
dropdownAlign: object;
7267
empty: boolean;
73-
autoAdjustOverflow?: boolean;
7468

7569
getTriggerDOMNode: () => HTMLElement;
7670
onPopupVisibleChange?: (visible: boolean) => void;
@@ -95,15 +89,14 @@ const SelectTrigger: React.RefForwardingComponent<RefTriggerProps, SelectTrigger
9589
dropdownClassName,
9690
direction = 'ltr',
9791
placement,
98-
dropdownMatchSelectWidth = true,
92+
dropdownMatchSelectWidth,
9993
dropdownRender,
10094
dropdownAlign,
10195
getPopupContainer,
10296
empty,
10397
getTriggerDOMNode,
10498
onPopupVisibleChange,
10599
onPopupMouseEnter,
106-
autoAdjustOverflow,
107100
...restProps
108101
} = props;
109102

@@ -115,11 +108,8 @@ const SelectTrigger: React.RefForwardingComponent<RefTriggerProps, SelectTrigger
115108
}
116109

117110
const builtInPlacements = React.useMemo(
118-
() => getBuiltInPlacements(getAdjustX({
119-
autoAdjustOverflow,
120-
dropdownMatchSelectWidth,
121-
})),
122-
[dropdownMatchSelectWidth, autoAdjustOverflow],
111+
() => getBuiltInPlacements(dropdownMatchSelectWidth),
112+
[dropdownMatchSelectWidth],
123113
);
124114

125115
// ===================== Motion ======================

tests/Select.test.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,9 @@ describe('Select.Basic', () => {
13001300
<Option value={1}>1</Option>
13011301
</Select>,
13021302
);
1303-
expect(wrapper.find('Trigger').props().builtinPlacements.bottomLeft.overflow.adjustX).toBe(1);
1303+
expect(
1304+
(wrapper.find('Trigger').prop('builtinPlacements') as any).bottomLeft.overflow.adjustX,
1305+
).toBe(1);
13041306
});
13051307

13061308
it('dropdown should not auto-adjust horizontally when dropdownMatchSelectWidth is true', () => {
@@ -1310,7 +1312,9 @@ describe('Select.Basic', () => {
13101312
<Option value={1}>1</Option>
13111313
</Select>,
13121314
);
1313-
expect(wrapper.find('Trigger').props().builtinPlacements.bottomLeft.overflow.adjustX).toBe(0);
1315+
expect(
1316+
(wrapper.find('Trigger').prop('builtinPlacements') as any).bottomLeft.overflow.adjustX,
1317+
).toBe(0);
13141318
});
13151319

13161320
it('if loading, arrow should show loading icon', () => {

0 commit comments

Comments
 (0)