Skip to content

Commit 901bd49

Browse files
authored
feat: support suffix (#711)
1 parent 408db11 commit 901bd49

File tree

8 files changed

+54
-2
lines changed

8 files changed

+54
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export default () => (
128128
| loading | show loading icon in arrow | Boolean | false |
129129
| virtual | Disable virtual scroll | Boolean | true |
130130
| direction | direction of dropdown | 'ltr' \| 'rtl' | 'ltr' |
131+
| suffix | suffix of select input | ReactNode | - |
131132

132133
### Methods
133134

assets/index.less

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,18 @@
253253
color: #999;
254254
}
255255
}
256+
257+
&-has-suffix {
258+
.@{select-prefix}-arrow {
259+
right: 16px;
260+
}
261+
}
262+
263+
&-suffix {
264+
position: absolute;
265+
right: 4px;
266+
top: 0;
267+
}
256268
}
257269

258270
.@{select-prefix}-selection__choice-zoom {

docs/demo/suffix.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## suffix
2+
<code src="../examples/suffix.tsx">

docs/examples/suffix.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React, { FC } from 'react';
2+
import Select from 'rc-select';
3+
4+
const SuffixDemo: FC = () => {
5+
return (
6+
<Select suffix=":)">
7+
{Array(10).fill(1).map((item, index) => (
8+
// eslint-disable-next-line react/no-array-index-key
9+
<Select.Option key={`pick me ${index}`} value={index}>{`pick me ${index}`}</Select.Option>
10+
))}
11+
</Select>
12+
)
13+
}
14+
15+
export default SuffixDemo;

src/BaseSelect.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import useDelayReset from './hooks/useDelayReset';
1616
import TransBtn from './TransBtn';
1717
import useLock from './hooks/useLock';
1818
import { BaseSelectContext } from './hooks/useBaseProps';
19+
import type { ReactNode } from 'react';
1920

2021
const DEFAULT_OMIT_PROPS = [
2122
'value',
@@ -169,6 +170,7 @@ export interface BaseSelectProps extends BaseSelectPrivateProps, React.AriaAttri
169170
clearIcon?: RenderNode;
170171
/** Selector remove icon */
171172
removeIcon?: RenderNode;
173+
suffix?: ReactNode;
172174

173175
// >>> Dropdown
174176
animation?: string;
@@ -273,6 +275,8 @@ const BaseSelect = React.forwardRef((props: BaseSelectProps, ref: React.Ref<Base
273275
onKeyDown,
274276
onMouseDown,
275277

278+
suffix,
279+
276280
// Rest Props
277281
...restProps
278282
} = props;
@@ -718,6 +722,7 @@ const BaseSelect = React.forwardRef((props: BaseSelectProps, ref: React.Ref<Base
718722
[`${prefixCls}-open`]: mergedOpen,
719723
[`${prefixCls}-customize-input`]: customizeInputElement,
720724
[`${prefixCls}-show-search`]: mergedShowSearch,
725+
[`${prefixCls}-has-suffix`]: suffix,
721726
});
722727

723728
// >>> Selector
@@ -815,6 +820,7 @@ const BaseSelect = React.forwardRef((props: BaseSelectProps, ref: React.Ref<Base
815820

816821
{arrowNode}
817822
{clearNode}
823+
{suffix && <span className={`${prefixCls}-suffix`}>{suffix}</span>}
818824
</div>
819825
);
820826
}

src/Select.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,17 @@
3232
import * as React from 'react';
3333
import warning from 'rc-util/lib/warning';
3434
import useMergedState from 'rc-util/lib/hooks/useMergedState';
35+
import type {
36+
BaseSelectProps,
37+
BaseSelectPropsWithoutPrivate,
38+
BaseSelectRef,
39+
DisplayValueType,
40+
RenderNode,
41+
} from './BaseSelect';
3542
import BaseSelect, { isMultiple } from './BaseSelect';
36-
import type { DisplayValueType, RenderNode } from './BaseSelect';
3743
import OptionList from './OptionList';
3844
import Option from './Option';
3945
import OptGroup from './OptGroup';
40-
import type { BaseSelectRef, BaseSelectPropsWithoutPrivate, BaseSelectProps } from './BaseSelect';
4146
import useOptions from './hooks/useOptions';
4247
import SelectContext from './SelectContext';
4348
import useId from './hooks/useId';

src/Selector/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
import * as React from 'react';
12+
import type { ReactNode } from 'react';
1213
import { useRef } from 'react';
1314
import KeyCode from 'rc-util/lib/KeyCode';
1415
import type { ScrollTo } from 'rc-virtual-list/lib/List';
@@ -93,6 +94,8 @@ export interface SelectorProps {
9394
* This may be removed after React provides replacement of `findDOMNode`
9495
*/
9596
domRef: React.Ref<HTMLDivElement>;
97+
98+
suffix?: ReactNode;
9699
}
97100

98101
const Selector: React.RefForwardingComponent<RefSelectorProps, SelectorProps> = (props, ref) => {

tests/Select.test.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,4 +1789,12 @@ describe('Select.Basic', () => {
17891789
.visibility,
17901790
).toBe('hidden');
17911791
});
1792+
1793+
it('should support suffix', () => {
1794+
const wrapper = mount(<Select suffix=":)" />);
1795+
1796+
expect(wrapper.find('.rc-select-has-suffix')).toBeTruthy();
1797+
expect(wrapper.find('.rc-select-suffix')).toBeTruthy();
1798+
expect(wrapper.find('.rc-select-suffix').text()).toEqual(':)');
1799+
});
17921800
});

0 commit comments

Comments
 (0)