Skip to content

Commit

Permalink
fix: TreeSelect removeIcon and clearIcon not working (#18949)
Browse files Browse the repository at this point in the history
* add tree-select async demo

* TreeSelect支持clearIcon和removeIcon

* remove docs and demo

* run test

* clear code

* add ui test
  • Loading branch information
sosohime authored and afc163 committed Sep 25, 2019
1 parent df7e366 commit e511e50
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 10 deletions.
93 changes: 93 additions & 0 deletions components/tree-select/__tests__/__snapshots__/index.test.js.snap
@@ -0,0 +1,93 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`TreeSelect TreeSelect Custom Icons should support customized icons 1`] = `
<span
aria-haspopup="listbox"
class="ant-select ant-select-enabled ant-select-allow-clear"
role="combobox"
tabindex="-1"
>
<span
class="ant-select-selection ant-select-selection--multiple"
>
<ul
class="ant-select-selection__rendered"
role="menubar"
>
<li
class="ant-select-selection__choice"
role="menuitem"
style="user-select: none;"
unselectable="unselectable"
>
<span
class="ant-select-selection__choice__remove"
>
<span>
remove
</span>
</span>
<span
class="ant-select-selection__choice__content"
>
leaf1
</span>
</li>
<li
class="ant-select-selection__choice"
role="menuitem"
style="user-select: none;"
unselectable="unselectable"
>
<span
class="ant-select-selection__choice__remove"
>
<span>
remove
</span>
</span>
<span
class="ant-select-selection__choice__content"
>
leaf2
</span>
</li>
<li
class="ant-select-search ant-select-search--inline"
>
<span
class="ant-select-search__field__wrap"
>
<input
aria-autocomplete="list"
aria-label="filter select"
aria-multiline="false"
class="ant-select-search__field"
style="width: 0px;"
type="text"
value=""
/>
<span
class="ant-select-search__field__mirror"
>
 
</span>
</span>
</li>
</ul>
<span
class="ant-select-selection__clear"
>
<span>
clear
</span>
</span>
<span
class="ant-select-search__field__placeholder"
style="display: none;"
>
Please select
</span>
</span>
</span>
`;
28 changes: 27 additions & 1 deletion components/tree-select/__tests__/index.test.js
@@ -1,6 +1,6 @@
import React from 'react';
import { mount } from 'enzyme';
import TreeSelect from '..';
import TreeSelect, { TreeNode } from '..';
import focusTest from '../../../tests/shared/focusTest';
import mountTest from '../../../tests/shared/mountTest';

Expand All @@ -17,4 +17,30 @@ describe('TreeSelect', () => {
expect(multiple.find('.ant-select-search__field').length).toBeTruthy();
});
});

describe('TreeSelect Custom Icons', () => {
it('should support customized icons', () => {
const wrapper = mount(
<TreeSelect
showSearch
clearIcon={<span>clear</span>}
removeIcon={<span>remove</span>}
value={['leaf1', 'leaf2']}
placeholder="Please select"
multiple
allowClear
treeDefaultExpandAll
>
<TreeNode value="parent 1" title="parent 1" key="0-1">
<TreeNode value="parent 1-0" title="parent 1-0" key="0-1-1">
<TreeNode value="leaf1" title="my leaf" key="random" />
<TreeNode value="leaf2" title="your leaf" key="random1" />
</TreeNode>
</TreeNode>
</TreeSelect>,
);

expect(wrapper.render()).toMatchSnapshot();
});
});
});
28 changes: 19 additions & 9 deletions components/tree-select/index.tsx
Expand Up @@ -5,6 +5,7 @@ import omit from 'omit.js';
import { TreeSelectProps, TreeNodeValue } from './interface';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import warning from '../_util/warning';
import { cloneElement } from '../_util/reactNode';
import Icon from '../icon';
import { AntTreeNodeProps } from '../tree';

Expand Down Expand Up @@ -74,6 +75,8 @@ export default class TreeSelect<T extends TreeNodeValue> extends React.Component
dropdownStyle,
dropdownClassName,
suffixIcon,
removeIcon,
clearIcon,
getPopupContainer,
...restProps
} = this.props;
Expand All @@ -99,15 +102,22 @@ export default class TreeSelect<T extends TreeNodeValue> extends React.Component
checkable = <span className={`${prefixCls}-tree-checkbox-inner`} />;
}

const inputIcon = (suffixIcon &&
(React.isValidElement<{ className?: string }>(suffixIcon)
? React.cloneElement(suffixIcon)
: suffixIcon)) || <Icon type="down" className={`${prefixCls}-arrow-icon`} />;
const inputIcon = suffixIcon ? (
cloneElement(suffixIcon)
) : (
<Icon type="down" className={`${prefixCls}-arrow-icon`} />
);

const removeIcon = <Icon type="close" className={`${prefixCls}-remove-icon`} />;
const finalRemoveIcon = removeIcon ? (
cloneElement(removeIcon)
) : (
<Icon type="close" className={`${prefixCls}-remove-icon`} />
);

const clearIcon = (
<Icon type="close-circle" className={`${prefixCls}-clear-icon`} theme="filled" />
const finalClearIcon = clearIcon ? (
cloneElement(clearIcon)
) : (
<Icon type="close-circle" theme="filled" className={`${prefixCls}-clear-icon`} />
);

return (
Expand All @@ -116,8 +126,8 @@ export default class TreeSelect<T extends TreeNodeValue> extends React.Component
this.renderSwitcherIcon(prefixCls, nodeProps)
}
inputIcon={inputIcon}
removeIcon={removeIcon}
clearIcon={clearIcon}
removeIcon={finalRemoveIcon}
clearIcon={finalClearIcon}
{...rest}
showSearch={showSearch}
getPopupContainer={getPopupContainer || getContextPopupContainer}
Expand Down
2 changes: 2 additions & 0 deletions components/tree-select/interface.tsx
Expand Up @@ -52,6 +52,8 @@ export interface TreeSelectProps<T extends TreeNodeValue> extends AbstractSelect
searchValue?: string;
showCheckedStrategy?: 'SHOW_ALL' | 'SHOW_PARENT' | 'SHOW_CHILD';
suffixIcon?: React.ReactNode;
removeIcon?: React.ReactNode;
clearIcon?: React.ReactNode;
treeCheckable?: boolean | React.ReactNode;
treeCheckStrictly?: boolean;
treeData?: Array<TreeNode>;
Expand Down

0 comments on commit e511e50

Please sign in to comment.