Skip to content

Commit

Permalink
Filter Icon of Table (ant-design#5293)
Browse files Browse the repository at this point in the history
*  Filter Icon of Table

+ close ant-design#5287
+ add `isFiltered` props for mark dataSource is filterd
+ improve CustomFilter Demo

* fix doc

* update

* custome filter icon

* custome filter icon
  • Loading branch information
RaoHai authored and afc163 committed Mar 27, 2017
1 parent 8409cff commit 15e6133
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions components/table/Column.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface ColumnProps<T> {
width?: string | number;
className?: string;
fixed?: boolean | ('left' | 'right');
filterIcon?: React.ReactNode;
filteredValue?: any[];
sortOrder?: boolean | ('ascend' | 'descend');
children?: ColumnProps<T>[];
Expand Down
3 changes: 2 additions & 1 deletion components/table/__tests__/__snapshots__/demo.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,8 @@ exports[`renders ./components/table/demo/custom-filter-panel.md correctly 1`] =
<span>
Name
<i
class="anticon anticon-filter ant-dropdown-trigger"
class="anticon anticon-smile-o ant-table-filter-icon ant-dropdown-trigger"
style="color:#aaa;"
title="筛选"
/>
</span>
Expand Down
8 changes: 6 additions & 2 deletions components/table/demo/custom-filter-panel.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ title:
Implement a customized column search example via `filterDropdown`, `filterDropdownVisible` and `filterDropdownVisibleChange`.

````jsx
import { Table, Input, Button } from 'antd';
import { Table, Input, Button, Icon } from 'antd';

const data = [{
key: '1',
Expand Down Expand Up @@ -43,6 +43,7 @@ class App extends React.Component {
filterDropdownVisible: false,
data,
searchText: '',
filtered: false,
};
onInputChange = (e) => {
this.setState({ searchText: e.target.value });
Expand All @@ -52,6 +53,7 @@ class App extends React.Component {
const reg = new RegExp(searchText, 'gi');
this.setState({
filterDropdownVisible: false,
filtered: !!searchText,
data: data.map((record) => {
const match = record.name.match(reg);
if (!match) {
Expand All @@ -78,6 +80,7 @@ class App extends React.Component {
filterDropdown: (
<div className="custom-filter-dropdown">
<Input
ref={ele => this.searchInput = ele}
placeholder="Search name"
value={this.state.searchText}
onChange={this.onInputChange}
Expand All @@ -86,8 +89,9 @@ class App extends React.Component {
<Button type="primary" onClick={this.onSearch}>Search</Button>
</div>
),
filterIcon: <Icon type="smile-o" style={{ color: this.state.filtered ? '#108ee9' : '#aaa' }} />,
filterDropdownVisible: this.state.filterDropdownVisible,
onFilterDropdownVisibleChange: visible => this.setState({ filterDropdownVisible: visible }),
onFilterDropdownVisibleChange: visible => this.setState({ filterDropdownVisible: visible }, () => this.searchInput.focus()),
}, {
title: 'Age',
dataIndex: 'age',
Expand Down
18 changes: 14 additions & 4 deletions components/table/filterDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface FilterMenuProps {
filterDropdownVisible?: boolean,
onFilterDropdownVisibleChange?: (visible: boolean) => any,
fixed?: boolean | string,
filterIcon?: React.ReactNode;
};
confirmFilter: (column: Object, selectedKeys: string[]) => any;
prefixCls: string;
Expand Down Expand Up @@ -168,6 +169,18 @@ export default class FilterMenu extends React.Component<FilterMenuProps, any> {
this.setState({ keyPathOfSelectedItem });
}

renderFilterIcon = () => {
const { column, locale, prefixCls } = this.props;
const filterIcon = column.filterIcon as any;
const dropdownSelectedClass = this.props.selectedKeys.length > 0 ? `${prefixCls}-selected` : '';

return filterIcon ? React.cloneElement(filterIcon as any, {
title: locale.filterTitle,
className: classNames(filterIcon.className, {
[`${prefixCls}-icon`]: true,
}),
}) : <Icon title={locale.filterTitle} type="filter" className={dropdownSelectedClass} />;
}
render() {
const { column, locale, prefixCls, dropdownPrefixCls } = this.props;
// default multiple selection in filter dropdown
Expand Down Expand Up @@ -209,17 +222,14 @@ export default class FilterMenu extends React.Component<FilterMenuProps, any> {
</FilterDropdownMenuWrapper>
);

const dropdownSelectedClass = (this.props.selectedKeys.length > 0)
? `${prefixCls}-selected` : '';

return (
<Dropdown
trigger={['click']}
overlay={menus}
visible={this.neverShown ? false : this.state.visible}
onVisibleChange={this.onVisibleChange}
>
<Icon title={locale.filterTitle} type="filter" className={dropdownSelectedClass} />
{this.renderFilterIcon()}
</Dropdown>
);
}
Expand Down
4 changes: 3 additions & 1 deletion components/table/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ One of Property `columns` for descriping column, Column has the same API.
| filterDropdown | customized filter overlay | ReactNode | - |
| filterDropdownVisible | whether filterDropdown is visible | boolean | - |
| onFilterDropdownVisibleChange | called when filterDropdownVisible is changed | function(visible) {} | - |
| filteredValue | controlled filtered value | string[] | - |
| filteredValue | controlled filtered value, filter icon will highlight. | string[] | - |
| filtered | whether the dataSource is filterd | boolean | false |
| filterIcon | customized filter icon | ReactNode | false |
| sorter | sort function for local sort, see [Array.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)'s compareFunction. If you need sort buttons only, set it `true` | Function\|boolean | - |
| colSpan | span of this column's title | number | |
| width | width of this column | string\|number | - |
Expand Down
2 changes: 2 additions & 0 deletions components/table/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ const columns = [{
| filterDropdownVisible | 用于控制自定义筛选菜单是否可见 | boolean | - |
| onFilterDropdownVisibleChange | 自定义筛选菜单可见变化时调用 | function(visible) {} | - |
| filteredValue | 筛选的受控属性,外界可用此控制列的筛选状态,值为已筛选的 value 数组 | string[] | - |
| filtered | 标识数据是否经过过滤,筛选图标会高亮 | boolean | false |
| filterIcon | 自定义 fiter 图标。| ReactNode | false |
| sorter | 排序函数,本地排序使用一个函数(参考 [Array.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) 的 compareFunction),需要服务端排序可设为 true | Function\|boolean | - |
| colSpan | 表头列合并,设置为 0 时,不渲染 | number | |
| width | 列宽度 | string\|number | - |
Expand Down
2 changes: 1 addition & 1 deletion components/table/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
text-align: center;
}

.@{iconfont-css-prefix}-filter {
.@{iconfont-css-prefix}-filter, .@{table-prefix-cls}-filter-icon {
margin-left: 4px;
font-size: @font-size-base;
cursor: pointer;
Expand Down

0 comments on commit 15e6133

Please sign in to comment.