diff --git a/components/table/Column.tsx b/components/table/Column.tsx index 9114a41367..6767c2f733 100644 --- a/components/table/Column.tsx +++ b/components/table/Column.tsx @@ -15,6 +15,7 @@ export interface ColumnProps { width?: string | number; className?: string; fixed?: boolean | ('left' | 'right'); + filterIcon?: React.ReactNode; filteredValue?: any[]; sortOrder?: boolean | ('ascend' | 'descend'); children?: ColumnProps[]; diff --git a/components/table/__tests__/__snapshots__/demo.test.js.snap b/components/table/__tests__/__snapshots__/demo.test.js.snap index 624c261fad..e249aa4d79 100644 --- a/components/table/__tests__/__snapshots__/demo.test.js.snap +++ b/components/table/__tests__/__snapshots__/demo.test.js.snap @@ -862,7 +862,8 @@ exports[`renders ./components/table/demo/custom-filter-panel.md correctly 1`] = Name diff --git a/components/table/demo/custom-filter-panel.md b/components/table/demo/custom-filter-panel.md index 1fb0886a58..ea2f789e23 100644 --- a/components/table/demo/custom-filter-panel.md +++ b/components/table/demo/custom-filter-panel.md @@ -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', @@ -43,6 +43,7 @@ class App extends React.Component { filterDropdownVisible: false, data, searchText: '', + filtered: false, }; onInputChange = (e) => { this.setState({ searchText: e.target.value }); @@ -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) { @@ -78,6 +80,7 @@ class App extends React.Component { filterDropdown: (
this.searchInput = ele} placeholder="Search name" value={this.state.searchText} onChange={this.onInputChange} @@ -86,8 +89,9 @@ class App extends React.Component {
), + filterIcon: , filterDropdownVisible: this.state.filterDropdownVisible, - onFilterDropdownVisibleChange: visible => this.setState({ filterDropdownVisible: visible }), + onFilterDropdownVisibleChange: visible => this.setState({ filterDropdownVisible: visible }, () => this.searchInput.focus()), }, { title: 'Age', dataIndex: 'age', diff --git a/components/table/filterDropdown.tsx b/components/table/filterDropdown.tsx index b8de295369..67ffca526a 100755 --- a/components/table/filterDropdown.tsx +++ b/components/table/filterDropdown.tsx @@ -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; @@ -168,6 +169,18 @@ export default class FilterMenu extends React.Component { 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, + }), + }) : ; + } render() { const { column, locale, prefixCls, dropdownPrefixCls } = this.props; // default multiple selection in filter dropdown @@ -209,9 +222,6 @@ export default class FilterMenu extends React.Component { ); - const dropdownSelectedClass = (this.props.selectedKeys.length > 0) - ? `${prefixCls}-selected` : ''; - return ( { visible={this.neverShown ? false : this.state.visible} onVisibleChange={this.onVisibleChange} > - + {this.renderFilterIcon()} ); } diff --git a/components/table/index.en-US.md b/components/table/index.en-US.md index 239b2c02cd..03006b653c 100644 --- a/components/table/index.en-US.md +++ b/components/table/index.en-US.md @@ -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 | - | diff --git a/components/table/index.zh-CN.md b/components/table/index.zh-CN.md index f12524f7a3..1a0b344e6a 100644 --- a/components/table/index.zh-CN.md +++ b/components/table/index.zh-CN.md @@ -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 | - | diff --git a/components/table/style/index.less b/components/table/style/index.less index 6c7b5cc271..ff6c9c85fa 100644 --- a/components/table/style/index.less +++ b/components/table/style/index.less @@ -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;