Fix Table column.filteredValue null problem, ref #4550

This commit is contained in:
afc163 2017-01-10 20:52:53 +08:00
parent dc00b9ee96
commit 3f5aebe85b
4 changed files with 49 additions and 29 deletions

View File

@ -294,7 +294,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
} }
getFilteredValueColumns(columns?) { getFilteredValueColumns(columns?) {
return (columns || this.columns || []).filter(column => column.filteredValue); return (columns || this.columns || []).filter(column => typeof column.filteredValue !== 'undefined');
} }
getFiltersFromColumns(columns?) { getFiltersFromColumns(columns?) {

View File

@ -128,6 +128,22 @@ describe('Table.filter', () => {
expect(wrapper.find('tbody tr').length).toBe(4); expect(wrapper.find('tbody tr').length).toBe(4);
}); });
it('can be controlled by filteredValue null', () => {
const wrapper = mount(createTable({
columns: [{
...column,
filteredValue: ['girl'],
}],
}));
expect(wrapper.find('tbody tr').length).toBe(1);
wrapper.setProps({ columns: [{
...column,
filteredValue: null,
}] });
expect(wrapper.find('tbody tr').length).toBe(4);
});
it('fires change event', () => { it('fires change event', () => {
const handleChange = jest.fn(); const handleChange = jest.fn();
const wrapper = mount(createTable({ onChange: handleChange })); const wrapper = mount(createTable({ onChange: handleChange }));

View File

@ -6908,18 +6908,27 @@ exports[`test renders ./components/table/demo/reset-filter.md correctly 1`] = `
<div> <div>
<div <div
class="table-operations"> class="table-operations">
<a <button
href="#"> class="ant-btn ant-btn-ghost"
Age descending order type="button">
</a> <span>
<a Sort age
href="#"> </span>
Clear filters </button>
</a> <button
<a class="ant-btn ant-btn-ghost"
href="#"> type="button">
Clear filters and sorting <span>
</a> Clear filters
</span>
</button>
<button
class="ant-btn ant-btn-ghost"
type="button">
<span>
Clear filters and sorters
</span>
</button>
</div> </div>
<div <div
class=" clearfix"> class=" clearfix">

View File

@ -22,7 +22,7 @@ Control filters and sorters by `filteredValue` and `sortOrder`.
> 3. `column.key` is required. > 3. `column.key` is required.
````jsx ````jsx
import { Table } from 'antd'; import { Table, Button } from 'antd';
const data = [{ const data = [{
key: '1', key: '1',
@ -60,19 +60,16 @@ const App = React.createClass({
sortedInfo: sorter, sortedInfo: sorter,
}); });
}, },
clearFilters(e) { clearFilters() {
e.preventDefault();
this.setState({ filteredInfo: null }); this.setState({ filteredInfo: null });
}, },
clearAll(e) { clearAll() {
e.preventDefault();
this.setState({ this.setState({
filteredInfo: null, filteredInfo: null,
sortedInfo: null, sortedInfo: null,
}); });
}, },
setAgeSort(e) { setAgeSort() {
e.preventDefault();
this.setState({ this.setState({
sortedInfo: { sortedInfo: {
order: 'descend', order: 'descend',
@ -92,7 +89,7 @@ const App = React.createClass({
{ text: 'Joe', value: 'Joe' }, { text: 'Joe', value: 'Joe' },
{ text: 'Jim', value: 'Jim' }, { text: 'Jim', value: 'Jim' },
], ],
filteredValue: filteredInfo.name, filteredValue: filteredInfo.name || null,
onFilter: (value, record) => record.name.includes(value), onFilter: (value, record) => record.name.includes(value),
sorter: (a, b) => a.name.length - b.name.length, sorter: (a, b) => a.name.length - b.name.length,
sortOrder: sortedInfo.columnKey === 'name' && sortedInfo.order, sortOrder: sortedInfo.columnKey === 'name' && sortedInfo.order,
@ -110,7 +107,7 @@ const App = React.createClass({
{ text: 'London', value: 'London' }, { text: 'London', value: 'London' },
{ text: 'New York', value: 'New York' }, { text: 'New York', value: 'New York' },
], ],
filteredValue: filteredInfo.address, filteredValue: filteredInfo.address || null,
onFilter: (value, record) => record.address.includes(value), onFilter: (value, record) => record.address.includes(value),
sorter: (a, b) => a.address.length - b.address.length, sorter: (a, b) => a.address.length - b.address.length,
sortOrder: sortedInfo.columnKey === 'address' && sortedInfo.order, sortOrder: sortedInfo.columnKey === 'address' && sortedInfo.order,
@ -118,9 +115,9 @@ const App = React.createClass({
return ( return (
<div> <div>
<div className="table-operations"> <div className="table-operations">
<a href="#" onClick={this.setAgeSort}>Age descending order</a> <Button type="ghost" onClick={this.setAgeSort}>Sort age</Button>
<a href="#" onClick={this.clearFilters}>Clear filters</a> <Button type="ghost" onClick={this.clearFilters}>Clear filters</Button>
<a href="#" onClick={this.clearAll}>Clear filters and sorting</a> <Button type="ghost" onClick={this.clearAll}>Clear filters and sorters</Button>
</div> </div>
<Table columns={columns} dataSource={data} onChange={this.handleChange} /> <Table columns={columns} dataSource={data} onChange={this.handleChange} />
</div> </div>
@ -133,12 +130,10 @@ ReactDOM.render(<App />, mountNode);
````css ````css
.table-operations { .table-operations {
font-size: 12px;
text-align: right;
margin-bottom: 16px; margin-bottom: 16px;
} }
.table-operations a { .table-operations > button {
margin-left: 8px; margin-right: 8px;
} }
```` ````