From 3b58a3c55db317a3e9fd10e0a376e42f3c4a2859 Mon Sep 17 00:00:00 2001 From: Cameron Hessler Date: Mon, 9 Mar 2020 17:23:23 -0500 Subject: [PATCH 1/2] Auto-adjust x when dropdownMatchSelectWidth=false --- examples/auto-adjust-dropdown.tsx | 127 ++++++++++++++++++++++++++++++ src/SelectTrigger.tsx | 37 +++++---- tests/Select.test.tsx | 26 ++++++ 3 files changed, 174 insertions(+), 16 deletions(-) create mode 100644 examples/auto-adjust-dropdown.tsx diff --git a/examples/auto-adjust-dropdown.tsx b/examples/auto-adjust-dropdown.tsx new file mode 100644 index 000000000..4c5d43740 --- /dev/null +++ b/examples/auto-adjust-dropdown.tsx @@ -0,0 +1,127 @@ +/* eslint-disable no-console */ +import React from 'react'; +import Select, { Option } from '../src'; +import '../assets/index.less'; + +class Test extends React.Component { + state = { + value: '3', + }; + + onChange = e => { + let value; + if (e && e.target) { + ({ value } = e.target); + } else { + value = e; + } + this.setState({ + value, + }); + }; + + render() { + const { value } = this.state; + + return ( +
+
+
+ +
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+ +
+
+
+ ); + } +} + +export default Test; +/* eslint-enable */ diff --git a/src/SelectTrigger.tsx b/src/SelectTrigger.tsx index 5bee5b957..4fc6044c7 100644 --- a/src/SelectTrigger.tsx +++ b/src/SelectTrigger.tsx @@ -3,23 +3,28 @@ import Trigger from 'rc-trigger'; import classNames from 'classnames'; import { RenderDOMFunc } from './interface'; -const BUILT_IN_PLACEMENTS = { - bottomLeft: { - points: ['tl', 'bl'], - offset: [0, 4], - overflow: { - adjustX: 0, - adjustY: 1, +const getBuiltInPlacements = (dropdownMatchSelectWidth: number | true) => { + // Enable horizontal overflow auto-adjustment when a custom dropdown width is provided + const adjustX = typeof dropdownMatchSelectWidth !== 'number' ? 0 : 1; + + return { + bottomLeft: { + points: ['tl', 'bl'], + offset: [0, 4], + overflow: { + adjustX, + adjustY: 1, + }, }, - }, - topLeft: { - points: ['bl', 'tl'], - offset: [0, -4], - overflow: { - adjustX: 0, - adjustY: 1, + topLeft: { + points: ['bl', 'tl'], + offset: [0, -4], + overflow: { + adjustX, + adjustY: 1, + }, }, - }, + }; }; export interface RefTriggerProps { @@ -96,7 +101,7 @@ const SelectTrigger: React.RefForwardingComponent< showAction={[]} hideAction={[]} popupPlacement="bottomLeft" - builtinPlacements={BUILT_IN_PLACEMENTS} + builtinPlacements={getBuiltInPlacements(dropdownMatchSelectWidth)} prefixCls={dropdownPrefixCls} popupTransitionName={mergedTransitionName} popup={
{popupNode}
} diff --git a/tests/Select.test.tsx b/tests/Select.test.tsx index f2d4bcdd5..47f1ef2a2 100644 --- a/tests/Select.test.tsx +++ b/tests/Select.test.tsx @@ -1182,6 +1182,32 @@ describe('Select.Basic', () => { ).toBe(233); }); + it('dropdown should auto-adjust horizontally when dropdownMatchSelectWidth is false', () => { + const wrapper = mount( + , + ); + expect( + wrapper.find('Trigger').props().builtinPlacements.bottomLeft.overflow + .adjustX, + ).toBe(1); + }); + + it('dropdown should not auto-adjust horizontally when dropdownMatchSelectWidth is true', () => { + const wrapper = mount( + , + ); + expect( + wrapper.find('Trigger').props().builtinPlacements.bottomLeft.overflow + .adjustX, + ).toBe(0); + }); + it('if loading, arrow should show loading icon', () => { const wrapper = mount(