fix: table locale not work (#21772)

* fix: Table locale not work

* add test case
This commit is contained in:
二货机器人 2020-03-02 17:15:39 +08:00 committed by GitHub
parent 99e6fb9981
commit 60f77cef23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 10 deletions

View File

@ -117,7 +117,7 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
ConfigContext,
);
const mergedSize = customizeSize || size;
const tableLocale = locale || contextLocale.Table;
const tableLocale = { ...contextLocale.Table, ...locale } as TableLocale;
const rawData: RecordType[] = dataSource || EMPTY_LIST;
const { getPrefixCls } = React.useContext(ConfigContext);
@ -241,6 +241,7 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
const [transformFilterColumns, filterStates, getFilters] = useFilter<RecordType>({
prefixCls,
locale: tableLocale,
dropdownPrefixCls,
columns: columns || [],
onFilterChange,
@ -308,6 +309,7 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
getRecordByKey,
expandType,
childrenColumnName,
locale: tableLocale,
});
const internalRowClassName = (record: RecordType, index: number, indent: number) => {

View File

@ -1171,4 +1171,31 @@ describe('Table.filter', () => {
expect.anything(),
);
});
it('locale should work', () => {
const wrapper = mount(
createTable({
locale: { filterConfirm: 'Bamboo' },
columns: [
{
...column,
filterDropdownVisible: true,
},
],
}),
);
expect(
wrapper
.find('.ant-table-filter-dropdown-link')
.first()
.text(),
).toEqual('Bamboo');
expect(
wrapper
.find('.ant-table-filter-dropdown-link')
.last()
.text(),
).toEqual('Reset');
});
});

View File

@ -11,8 +11,6 @@ import {
} from '../../interface';
import { getColumnPos, renderColumnTitle, getColumnKey } from '../../util';
import FilterDropdown from './FilterDropdown';
import { ConfigContext } from '../../../config-provider';
import defaultLocale from '../../../locale/en_US';
export interface FilterState<RecordType> {
column: ColumnType<RecordType>;
@ -161,6 +159,7 @@ interface FilterConfig<RecordType> {
prefixCls: string;
dropdownPrefixCls?: string;
columns: ColumnsType<RecordType>;
locale: TableLocale;
onFilterChange: (
filters: Record<string, Key[] | null>,
filterStates: FilterState<RecordType>[],
@ -174,14 +173,12 @@ function useFilter<RecordType>({
columns,
onFilterChange,
getPopupContainer,
locale: tableLocale,
}: FilterConfig<RecordType>): [
TransformColumns<RecordType>,
FilterState<RecordType>[],
() => Record<string, Key[] | null>,
] {
const { locale = defaultLocale } = React.useContext(ConfigContext);
const tableLocale = (locale.Table || {}) as TableLocale;
const [filterStates, setFilterStates] = React.useState<FilterState<RecordType>[]>(
collectFilterStates(columns, true),
);

View File

@ -16,8 +16,6 @@ import {
TransformColumns,
ExpandType,
} from '../interface';
import { ConfigContext } from '../../config-provider';
import defaultLocale from '../../locale/en_US';
const EMPTY_LIST: any[] = [];
@ -37,6 +35,7 @@ interface UseSelectionConfig<RecordType> {
getRecordByKey: (key: Key) => RecordType;
expandType: ExpandType;
childrenColumnName: string;
locale: TableLocale;
}
type INTERNAL_SELECTION_ITEM = SelectionItem | typeof SELECTION_ALL | typeof SELECTION_INVERT;
@ -78,8 +77,6 @@ export default function useSelection<RecordType>(
fixed,
} = rowSelection || {};
const { locale = defaultLocale } = React.useContext(ConfigContext);
const tableLocale = (locale.Table || {}) as TableLocale;
const {
prefixCls,
data,
@ -88,6 +85,7 @@ export default function useSelection<RecordType>(
getRowKey,
expandType,
childrenColumnName,
locale: tableLocale,
} = config;
const [innerSelectedKeys, setInnerSelectedKeys] = React.useState<Key[]>();