mirror of
https://github.com/ant-design/ant-design.git
synced 2025-01-18 22:36:31 +08:00
fix(table): optimize type declaration about filter (#29385)
This commit is contained in:
parent
0fae19ca17
commit
dc100b17c5
@ -18,7 +18,6 @@ import {
|
||||
ColumnsType,
|
||||
TableCurrentDataSource,
|
||||
SorterResult,
|
||||
Key,
|
||||
GetPopupContainer,
|
||||
ExpandableConfig,
|
||||
ExpandType,
|
||||
@ -26,6 +25,7 @@ import {
|
||||
SortOrder,
|
||||
TableLocale,
|
||||
TableAction,
|
||||
FilterValue,
|
||||
} from './interface';
|
||||
import useSelection, {
|
||||
SELECTION_ALL,
|
||||
@ -54,7 +54,7 @@ interface ChangeEventInfo<RecordType> {
|
||||
pageSize?: number;
|
||||
total?: number;
|
||||
};
|
||||
filters: Record<string, (Key | boolean)[] | null>;
|
||||
filters: Record<string, FilterValue | null>;
|
||||
sorter: SorterResult<RecordType> | SorterResult<RecordType>[];
|
||||
|
||||
filterStates: FilterState<RecordType>[];
|
||||
@ -85,7 +85,7 @@ export interface TableProps<RecordType>
|
||||
|
||||
onChange?: (
|
||||
pagination: TablePaginationConfig,
|
||||
filters: Record<string, (Key | boolean)[] | null>,
|
||||
filters: Record<string, FilterValue | null>,
|
||||
sorter: SorterResult<RecordType> | SorterResult<RecordType>[],
|
||||
extra: TableCurrentDataSource<RecordType>,
|
||||
) => void;
|
||||
@ -236,9 +236,10 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Controlled state in `columns` is not a good idea that makes too many code (1000+ line?) to read
|
||||
* state out and then put it back to title render. Move these code into `hooks` but still too
|
||||
* complex. We should provides Table props like `sorter` & `filter` to handle control in next big version.
|
||||
* Controlled state in `columns` is not a good idea that makes too many code (1000+ line?) to
|
||||
* read state out and then put it back to title render. Move these code into `hooks` but still
|
||||
* too complex. We should provides Table props like `sorter` & `filter` to handle control in next
|
||||
* big version.
|
||||
*/
|
||||
|
||||
// ============================ Sorter =============================
|
||||
@ -273,7 +274,7 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
|
||||
|
||||
// ============================ Filter ============================
|
||||
const onFilterChange = (
|
||||
filters: Record<string, (Key | boolean)[]>,
|
||||
filters: Record<string, FilterValue>,
|
||||
filterStates: FilterState<RecordType>[],
|
||||
) => {
|
||||
triggerOnChange(
|
||||
|
@ -134,7 +134,7 @@ function FilterDropdown<RecordType>(props: FilterDropdownProps<RecordType>) {
|
||||
typeof filterDropdownVisible === 'boolean' ? filterDropdownVisible : visible;
|
||||
|
||||
// ===================== Select Keys =====================
|
||||
const propFilteredKeys = filterState && filterState.filteredKeys;
|
||||
const propFilteredKeys = filterState?.filteredKeys;
|
||||
const [getFilteredKeysSync, setFilteredKeysSync] = useSyncState(propFilteredKeys || []);
|
||||
|
||||
const onSelectKeys = ({ selectedKeys }: { selectedKeys?: Key[] }) => {
|
||||
|
@ -6,6 +6,8 @@ import {
|
||||
ColumnTitleProps,
|
||||
Key,
|
||||
TableLocale,
|
||||
FilterValue,
|
||||
FilterKey,
|
||||
GetPopupContainer,
|
||||
ColumnFilterItem,
|
||||
} from '../../interface';
|
||||
@ -15,7 +17,7 @@ import FilterDropdown from './FilterDropdown';
|
||||
export interface FilterState<RecordType> {
|
||||
column: ColumnType<RecordType>;
|
||||
key: Key;
|
||||
filteredKeys?: Key[] | null;
|
||||
filteredKeys?: FilterKey;
|
||||
forceFiltered?: boolean;
|
||||
}
|
||||
|
||||
@ -41,7 +43,7 @@ function collectFilterStates<RecordType>(
|
||||
filterStates.push({
|
||||
column,
|
||||
key: getColumnKey(column, columnPos),
|
||||
filteredKeys: filteredValues,
|
||||
filteredKeys: filteredValues as FilterKey,
|
||||
forceFiltered: column.filtered,
|
||||
});
|
||||
} else {
|
||||
@ -49,8 +51,9 @@ function collectFilterStates<RecordType>(
|
||||
filterStates.push({
|
||||
column,
|
||||
key: getColumnKey(column, columnPos),
|
||||
filteredKeys:
|
||||
init && column.defaultFilteredValue ? column.defaultFilteredValue! : undefined,
|
||||
filteredKeys: (init && column.defaultFilteredValue
|
||||
? column.defaultFilteredValue!
|
||||
: undefined) as FilterKey,
|
||||
forceFiltered: column.filtered,
|
||||
});
|
||||
}
|
||||
@ -121,7 +124,7 @@ function injectFilter<RecordType>(
|
||||
}
|
||||
|
||||
function flattenKeys(filters?: ColumnFilterItem[]) {
|
||||
let keys: (string | number | boolean)[] = [];
|
||||
let keys: FilterValue = [];
|
||||
(filters || []).forEach(({ value, children }) => {
|
||||
keys.push(value);
|
||||
if (children) {
|
||||
@ -132,7 +135,7 @@ function flattenKeys(filters?: ColumnFilterItem[]) {
|
||||
}
|
||||
|
||||
function generateFilterInfo<RecordType>(filterStates: FilterState<RecordType>[]) {
|
||||
const currentFilters: Record<string, (Key | boolean)[] | null> = {};
|
||||
const currentFilters: Record<string, FilterValue | null> = {};
|
||||
|
||||
filterStates.forEach(({ key, filteredKeys, column }) => {
|
||||
const { filters, filterDropdown } = column;
|
||||
@ -178,7 +181,7 @@ interface FilterConfig<RecordType> {
|
||||
mergedColumns: ColumnsType<RecordType>;
|
||||
locale: TableLocale;
|
||||
onFilterChange: (
|
||||
filters: Record<string, (Key | boolean)[] | null>,
|
||||
filters: Record<string, FilterValue | null>,
|
||||
filterStates: FilterState<RecordType>[],
|
||||
) => void;
|
||||
getPopupContainer?: GetPopupContainer;
|
||||
@ -194,7 +197,7 @@ function useFilter<RecordType>({
|
||||
}: FilterConfig<RecordType>): [
|
||||
TransformColumns<RecordType>,
|
||||
FilterState<RecordType>[],
|
||||
() => Record<string, (Key | boolean)[] | null>,
|
||||
() => Record<string, FilterValue | null>,
|
||||
] {
|
||||
const [filterStates, setFilterStates] = React.useState<FilterState<RecordType>[]>(
|
||||
collectFilterStates(mergedColumns, true),
|
||||
|
@ -68,6 +68,8 @@ export type ColumnTitle<RecordType> =
|
||||
| React.ReactNode
|
||||
| ((props: ColumnTitleProps<RecordType>) => React.ReactNode);
|
||||
|
||||
export type FilterValue = (Key | boolean)[];
|
||||
export type FilterKey = Key[] | null;
|
||||
export interface FilterConfirmProps {
|
||||
closeDropdown: boolean;
|
||||
}
|
||||
@ -103,8 +105,8 @@ export interface ColumnType<RecordType> extends RcColumnType<RecordType> {
|
||||
filters?: ColumnFilterItem[];
|
||||
filterDropdown?: React.ReactNode | ((props: FilterDropdownProps) => React.ReactNode);
|
||||
filterMultiple?: boolean;
|
||||
filteredValue?: Key[] | null;
|
||||
defaultFilteredValue?: Key[] | null;
|
||||
filteredValue?: FilterValue | null;
|
||||
defaultFilteredValue?: FilterValue | null;
|
||||
filterIcon?: React.ReactNode | ((filtered: boolean) => React.ReactNode);
|
||||
onFilter?: (value: string | number | boolean, record: RecordType) => boolean;
|
||||
filterDropdownVisible?: boolean;
|
||||
|
Loading…
Reference in New Issue
Block a user