Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Select width became 0px when search after select something for antd 5 #936

Merged
merged 3 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/examples/single.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,13 @@ class Test extends React.Component {

<h2>Single Select</h2>

<div style={{ width: 300 }}>
<div>
<Select
autoFocus
id="my-select"
value={value}
placeholder="placeholder"
showSearch
style={{ width: 500 }}
onBlur={this.onBlur}
onFocus={this.onFocus}
onSearch={this.onSearch}
Expand Down
20 changes: 15 additions & 5 deletions src/Selector/SingleSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ const SingleSelector: React.FC<SelectorProps> = (props) => {
if (item) {
return null;
}
const hiddenStyle = hasTextInput ? { visibility: 'hidden' as const } : undefined;
const hiddenStyle = hasTextInput ? { visibility: 'hidden' } as React.CSSProperties : undefined;
return (
<span className={`${prefixCls}-selection-placeholder`} style={hiddenStyle}>
<span
className={`${prefixCls}-selection-placeholder`}
style={hiddenStyle}
>
{placeholder}
</span>
);
Expand Down Expand Up @@ -105,11 +108,18 @@ const SingleSelector: React.FC<SelectorProps> = (props) => {
</span>

{/* Display value */}
{!combobox && item && !hasTextInput && (
<span className={`${prefixCls}-selection-item`} title={selectionTitle}>
{(!combobox && item) ? (
<span
className={`${prefixCls}-selection-item`}
title={selectionTitle}
// 当 Select 已经选中选项时,还需 selection 隐藏但留在原地占位
// https://github.com/ant-design/ant-design/issues/27688
// https://github.com/ant-design/ant-design/issues/41530
style={hasTextInput ? { visibility: 'hidden' } as React.CSSProperties : undefined}
>
{item.label}
</span>
)}
) : null}

{/* Display placeholder */}
{renderPlaceholder()}
Expand Down
2 changes: 1 addition & 1 deletion tests/Combobox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('Select.Combobox', () => {
expect(wrapper.find('input').props().value).toBe('');
expect(wrapper.find('.rc-select-selection-placeholder').text()).toEqual('placeholder');
wrapper.find('input').simulate('change', { target: { value: '1' } });
expect(wrapper.find('.rc-select-selection-placeholder').length).toBeFalsy();
expect(wrapper.find('.rc-select-selection-placeholder').length).toBe(0);
expect(wrapper.find('input').props().value).toBe('1');
});

Expand Down
61 changes: 5 additions & 56 deletions tests/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -595,18 +595,16 @@ describe('Select.Basic', () => {
expect(wrapper.find('.rc-select').getDOMNode().className).toContain('-focus');
});

it('click placeholder should trigger onFocus', () => {
const wrapper2 = mount(
it('focus input when placeholder is clicked', () => {
const wrapper = mount(
<Select placeholder="xxxx">
<Option value="1">1</Option>
<Option value="2">2</Option>
</Select>,
);

const inputSpy = jest.spyOn(wrapper2.find('input').instance(), 'focus' as any);

wrapper2.find('.rc-select-selection-placeholder').simulate('mousedown');
wrapper2.find('.rc-select-selection-placeholder').simulate('click');
const inputSpy = jest.spyOn(wrapper.find('input').instance(), 'focus' as any);
wrapper.find('.rc-select-selection-placeholder').simulate('mousedown');
wrapper.find('.rc-select-selection-placeholder').simulate('click');
expect(inputSpy).toHaveBeenCalled();
});
});
Expand Down Expand Up @@ -816,19 +814,6 @@ describe('Select.Basic', () => {
expectOpen(wrapper, false);
});

it('focus input when placeholder is clicked', () => {
const wrapper = mount(
<Select placeholder="select">
<Option value="1">1</Option>
</Select>,
);

const focusSpy = jest.spyOn(wrapper.find('input').instance(), 'focus' as any);
wrapper.find('.rc-select-selection-placeholder').simulate('mousedown');
wrapper.find('.rc-select-selection-placeholder').simulate('click');
expect(focusSpy).toHaveBeenCalled();
});

describe('combobox could customize input element', () => {
it('work', () => {
const onKeyDown = jest.fn();
Expand Down Expand Up @@ -1736,32 +1721,6 @@ describe('Select.Basic', () => {
});
});

describe('show placeholder', () => {
it('when searchValue is controlled', () => {
const wrapper = mount(<Select searchValue="light" placeholder="bamboo" />);
expect(
wrapper.find('.rc-select-selection-placeholder').getDOMNode().hasAttribute('style'),
).toBe(false);
toggleOpen(wrapper);
expect(
(wrapper.find('.rc-select-selection-placeholder').getDOMNode() as HTMLSpanElement).style
.visibility,
).toBe('hidden');
});

it('when value is null', () => {
const wrapper = mount(<Select value={null} placeholder="bamboo" />);
expect(wrapper.find('.rc-select-selection-placeholder').length).toBeTruthy();
});

it('not when value is null but it is an Option', () => {
const wrapper = mount(
<Select value={null} placeholder="bamboo" options={[{ value: null, label: 'light' }]} />,
);
expect(wrapper.find('.rc-select-selection-placeholder').length).toBeFalsy();
});
});

it('Remove options can keep the cache', () => {
const wrapper = mount(<Select value={903} options={[{ value: 903, label: 'Bamboo' }]} />);
expect(findSelection(wrapper).text()).toEqual('Bamboo');
Expand Down Expand Up @@ -1894,16 +1853,6 @@ describe('Select.Basic', () => {
expect(onClick).toHaveBeenCalled();
});

it('should hide placeholder if force closed and showSearch with searchValue', () => {
const wrapper = mount(
<Select showSearch searchValue="search" open={false} placeholder="placeholder" />,
);
expect(
(wrapper.find('.rc-select-selection-placeholder').getDOMNode() as HTMLSpanElement).style
.visibility,
).toBe('hidden');
});

it('no warning for non-dom attr', () => {
const wrapper = mount(
<Select open>
Expand Down
45 changes: 45 additions & 0 deletions tests/placeholder.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { mount } from 'enzyme';
import React from 'react';
import Select from '../src';
import { toggleOpen } from './utils/common';

describe('Select placeholder', () => {
it('when searchValue is controlled', () => {
const wrapper = mount(<Select searchValue="light" placeholder="bamboo" />);
expect(
wrapper.find('.rc-select-selection-placeholder').getDOMNode().hasAttribute('style'),
).toBe(false);
toggleOpen(wrapper);
expect(
(wrapper.find('.rc-select-selection-placeholder').getDOMNode() as HTMLSpanElement).style
.visibility,
).toBe('hidden');
});

it('when value is null', () => {
const wrapper = mount(<Select value={null} placeholder="bamboo" />);
expect(wrapper.find('.rc-select-selection-placeholder').length).toBe(1);
expect(wrapper.find('.rc-select-selection-placeholder').text()).toBe('bamboo');
});

it('not when value is null but it is an Option', () => {
const wrapper = mount(
<Select value={null} placeholder="bamboo" options={[{ value: null, label: 'light' }]} />,
);
expect(wrapper.find('.rc-select-selection-placeholder').length).toBe(0);
expect(wrapper.find('.rc-select-selection-item').text()).toBe('light');
toggleOpen(wrapper);
expect(wrapper.find('.rc-select-selection-item').text()).toBe('light');
});

it('should hide placeholder if force closed and showSearch with searchValue', () => {
const wrapper = mount(
<Select showSearch searchValue="search" open={false} placeholder="placeholder" />,
);
expect(
(wrapper.find('.rc-select-selection-placeholder').getDOMNode() as HTMLSpanElement).style
.visibility,
).toBe('hidden');
expect(wrapper.find('.rc-select-selection-placeholder').text()).toBe('placeholder');
});
});