Skip to content

Commit 7d94005

Browse files
authored
feat: support prefix (#1060)
1 parent 7925a25 commit 7d94005

File tree

9 files changed

+33
-16
lines changed

9 files changed

+33
-16
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export default () => (
121121
| showAction | actions trigger the dropdown to show | String[]? | - |
122122
| autoFocus | focus select after mount | boolean | - |
123123
| autoClearSearchValue | auto clear search input value when multiple select is selected/deselected | boolean | true |
124+
| prefix | specify the select prefix icon or text | ReactNode | - |
124125
| suffixIcon | specify the select arrow icon | ReactNode | - |
125126
| clearIcon | specify the clear icon | ReactNode | - |
126127
| removeIcon | specify the remove icon | ReactNode | - |

assets/index.less

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272

7373
.@{select-prefix}-selection-search {
7474
width: 100%;
75+
position: relative;
7576

7677
&-input {
7778
width: 100%;
@@ -102,7 +103,6 @@
102103
// -------------- Multiple ---------------
103104
&-multiple .@{select-prefix}-selector {
104105
display: flex;
105-
flex-wrap: wrap;
106106
padding: 1px;
107107
border: 1px solid #000;
108108

@@ -122,7 +122,6 @@
122122
.@{select-prefix}-selection-overflow {
123123
display: flex;
124124
flex-wrap: wrap;
125-
width: 100%;
126125

127126
&-item {
128127
flex: none;

docs/examples/custom-icon.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class CustomIconComponent extends React.Component {
103103
value={value}
104104
mode="combobox"
105105
backfill
106+
prefix="Foobar"
106107
suffixIcon={({ searchValue }) => {
107108
if (searchValue) {
108109
return '😺';
@@ -193,6 +194,7 @@ class Test extends React.Component {
193194
onChange={this.onChange}
194195
onFocus={() => console.log('focus')}
195196
tokenSeparators={[' ', ',']}
197+
prefix="Foobar"
196198
suffixIcon={getSvg(arrowPath)}
197199
clearIcon={getSvg(clearPath)}
198200
removeIcon={getSvg(clearPath)}

src/BaseSelect/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export interface BaseSelectProps extends BaseSelectPrivateProps, React.AriaAttri
173173

174174
// >>> Icons
175175
allowClear?: boolean | { clearIcon?: RenderNode };
176+
prefix?: React.ReactNode;
176177
suffixIcon?: RenderNode;
177178
/**
178179
* Clear all icon
@@ -259,6 +260,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
259260

260261
// Icons
261262
allowClear,
263+
prefix,
262264
suffixIcon,
263265
clearIcon,
264266

@@ -795,6 +797,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
795797
inputElement={customizeInputElement}
796798
ref={selectorRef}
797799
id={id}
800+
prefix={prefix}
798801
showSearch={mergedShowSearch}
799802
autoClearSearchValue={autoClearSearchValue}
800803
mode={mode}

src/Selector/SingleSelector.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ const SingleSelector: React.FC<SelectorProps> = (props) => {
104104
attrs={pickAttrs(props, true)}
105105
maxLength={combobox ? maxLength : undefined}
106106
/>
107+
{/* Display placeholder */}
108+
{placeholderNode}
107109
</span>
108110

109111
{/* Display value */}
@@ -119,8 +121,6 @@ const SingleSelector: React.FC<SelectorProps> = (props) => {
119121
{item.label}
120122
</span>
121123
) : null}
122-
{/* Display placeholder */}
123-
{placeholderNode}
124124
</>
125125
);
126126
};

src/Selector/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export interface SelectorProps {
7272
disabled?: boolean;
7373
placeholder?: React.ReactNode;
7474
removeIcon?: RenderNode;
75+
prefix?: React.ReactNode;
7576

7677
// Tags
7778
maxTagCount?: number | 'responsive';
@@ -110,6 +111,7 @@ const Selector: React.ForwardRefRenderFunction<RefSelectorProps, SelectorProps>
110111
showSearch,
111112
tokenWithEnter,
112113
disabled,
114+
prefix,
113115

114116
autoClearSearchValue,
115117

@@ -284,6 +286,7 @@ const Selector: React.ForwardRefRenderFunction<RefSelectorProps, SelectorProps>
284286
onClick={onClick}
285287
onMouseDown={onMouseDown}
286288
>
289+
{prefix && <div className={`${prefixCls}-prefix`}>{prefix}</div>}
287290
{selectNode}
288291
</div>
289292
);

tests/Multiple.test.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
keyUp,
2020
} from './utils/common';
2121
import allowClearTest from './shared/allowClearTest';
22-
import { fireEvent, render } from '@testing-library/react';
22+
import { fireEvent, render, screen } from '@testing-library/react';
2323

2424
describe('Select.Multiple', () => {
2525
injectRunAllTimers(jest);
@@ -374,6 +374,15 @@ describe('Select.Multiple', () => {
374374
expect(container.querySelector('.rc-select-arrow')).toBeTruthy();
375375
});
376376

377+
it('show static prefix', () => {
378+
render(<Select mode="multiple" value={['']} prefix="Foobar">
379+
<Option value={1}>1</Option>
380+
<Option value={2}>2</Option>
381+
</Select>);
382+
383+
expect(screen.findByText('Foobar')).toBeTruthy();
384+
});
385+
377386
it('block input when fast backspace', () => {
378387
jest.useFakeTimers();
379388
const onChange = jest.fn();

tests/__snapshots__/Combobox.test.tsx.snap

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ exports[`Select.Combobox renders controlled correctly 1`] = `
2323
type="search"
2424
value=""
2525
/>
26-
</span>
27-
<span
28-
class="rc-select-selection-placeholder"
29-
>
30-
Search
26+
<span
27+
class="rc-select-selection-placeholder"
28+
>
29+
Search
30+
</span>
3131
</span>
3232
</div>
3333
</div>
@@ -56,11 +56,11 @@ exports[`Select.Combobox renders correctly 1`] = `
5656
type="search"
5757
value=""
5858
/>
59-
</span>
60-
<span
61-
class="rc-select-selection-placeholder"
62-
>
63-
Search
59+
<span
60+
class="rc-select-selection-placeholder"
61+
>
62+
Search
63+
</span>
6464
</span>
6565
</div>
6666
</div>

tests/__snapshots__/ssr.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Select.SSR should work 1`] = `"<div class="rc-select rc-select-single"><div class="rc-select-selector"><span class="rc-select-selection-search"><input type="search" autoComplete="off" class="rc-select-selection-search-input" role="combobox" aria-expanded="false" aria-haspopup="listbox" aria-owns="undefined_list" aria-autocomplete="list" aria-controls="undefined_list" readonly="" unselectable="on" style="opacity:0" value=""/></span><span class="rc-select-selection-placeholder"></span></div></div>"`;
3+
exports[`Select.SSR should work 1`] = `"<div class="rc-select rc-select-single"><div class="rc-select-selector"><span class="rc-select-selection-search"><input type="search" autoComplete="off" class="rc-select-selection-search-input" role="combobox" aria-expanded="false" aria-haspopup="listbox" aria-owns="undefined_list" aria-autocomplete="list" aria-controls="undefined_list" readonly="" unselectable="on" style="opacity:0" value=""/><span class="rc-select-selection-placeholder"></span></span></div></div>"`;

0 commit comments

Comments
 (0)