Skip to content

Commit

Permalink
refactor: no closable when disabled (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Oct 8, 2020
1 parent f05d794 commit 05d19a9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Selector/MultipleSelector.tsx
Expand Up @@ -132,7 +132,7 @@ const SelectSelector: React.FC<SelectorProps> = props => {
>
{({ key, label, value, disabled: itemDisabled, className, style }) => {
const mergedKey = key || value;
const closable = key !== REST_TAG_KEY && !itemDisabled;
const closable = !disabled && key !== REST_TAG_KEY && !itemDisabled;
const onMouseDown = (event: React.MouseEvent) => {
event.preventDefault();
event.stopPropagation();
Expand Down
73 changes: 54 additions & 19 deletions tests/Tags.test.tsx
@@ -1,3 +1,4 @@
/* eslint-disable import/no-named-as-default-member */
import { mount } from 'enzyme';
import KeyCode from 'rc-util/lib/KeyCode';
import classNames from 'classnames';
Expand Down Expand Up @@ -268,26 +269,58 @@ describe('Select.Tags', () => {
expect(onChange).toHaveBeenCalledWith(['a'], expect.anything());
});

it('can render custom tags', () => {
const onTagRender = jest.fn();
const tagRender = (props: any) => {
const { label } = props;
onTagRender(label);
return (
<span className={classNames(label, 'customize-tag')}>
{label}
{label}
</span>
describe('tagRender', () => {
it('can render custom tags', () => {
const onTagRender = jest.fn();
const tagRender = (props: any) => {
const { label } = props;
onTagRender(label);
return (
<span className={classNames(label, 'customize-tag')}>
{label}
{label}
</span>
);
};
const wrapper = mount(<Select mode="tags" tokenSeparators={[',']} tagRender={tagRender} />);

wrapper.find('input').simulate('change', { target: { value: '1,A,42' } });

expect(wrapper.find('span.A').length).toBe(1);
expect(wrapper.find('span.A').text()).toBe('AA');
expect(onTagRender).toHaveBeenCalled();
expect(wrapper.find('.customize-tag')).toHaveLength(3);
});

it('disabled', () => {
const tagRender = jest.fn();
mount(
<Select
mode="tags"
disabled
value={['light']}
tagRender={tagRender}
options={[{ value: 'light' }]}
/>,
);
};
const wrapper = mount(<Select mode="tags" tokenSeparators={[',']} tagRender={tagRender} />);

wrapper.find('input').simulate('change', { target: { value: '1,A,42' } });
expect(tagRender).toHaveBeenCalledWith(expect.objectContaining({ closable: false }));
});

expect(wrapper.find('span.A').length).toBe(1);
expect(wrapper.find('span.A').text()).toBe('AA');
expect(onTagRender).toHaveBeenCalledTimes(3);
expect(wrapper.find('.customize-tag')).toHaveLength(3);
it('option disabled', () => {
const tagRender = jest.fn();
mount(
<Select
mode="tags"
disabled
value={['light']}
tagRender={tagRender}
options={[{ value: 'light', disabled: true }]}
/>,
);

expect(tagRender).toHaveBeenCalledWith(expect.objectContaining({ closable: false }));
});
});

describe('OptGroup', () => {
Expand Down Expand Up @@ -326,12 +359,14 @@ describe('Select.Tags', () => {
});

it('should work fine when filterOption function exists', () => {
const LegacyOption = Select.Option as any; // Compatible to legacy usage

const children = [];
for (let i = 10; i < 36; i += 1) {
children.push(
<Option key={i.toString(36) + i} disabled={!(i % 3)}>
<LegacyOption key={i.toString(36) + i} disabled={!(i % 3)}>
{i.toString(36) + i}
</Option>,
</LegacyOption>,
);
}
const wrapper = mount(
Expand Down
8 changes: 3 additions & 5 deletions tests/shared/removeSelectedTest.tsx
Expand Up @@ -34,17 +34,15 @@ export default function removeSelectedTest(mode: any) {
value={['1']}
onChange={handleChange}
onDeselect={handleDeselect}
disabled={true}
disabled
mode={mode}
>
<Option value="1">1</Option>
<Option value="2">2</Option>
</Select>,
);
removeSelection(wrapper);

expect(handleDeselect).not.toHaveBeenCalled();
expect(handleChange).not.toHaveBeenCalled();
expect(wrapper.find('.rc-select-selection-item-remove')).toHaveLength(0);
});

it('wrap value when labelInValue', () => {
Expand All @@ -55,7 +53,7 @@ export default function removeSelectedTest(mode: any) {
value={[{ key: '1' }, { key: '2' }]}
onChange={handleChange}
onDeselect={handleDeselect}
labelInValue={true}
labelInValue
mode={mode}
>
<Option value="1">1</Option>
Expand Down

1 comment on commit 05d19a9

@vercel
Copy link

@vercel vercel bot commented on 05d19a9 Oct 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.