Browse Source

refactor: solve circular reference of Table (#42813)

* refactor: solve circular reference of Table

* Update index.tsx

* chore: add export
pull/43176/head
Zack Chang 1 year ago
committed by GitHub
parent
commit
7dc74b1da9
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      components/table/hooks/useFilter/FilterDropdown.tsx
  2. 16
      components/table/hooks/useFilter/index.tsx

21
components/table/hooks/useFilter/FilterDropdown.tsx

@ -1,34 +1,34 @@
import FilterFilled from '@ant-design/icons/FilterFilled';
import classNames from 'classnames';
import isEqual from 'rc-util/lib/isEqual';
import type { FieldDataNode } from 'rc-tree';
import isEqual from 'rc-util/lib/isEqual';
import * as React from 'react';
import type { MenuProps } from '../../../menu';
import type { FilterState } from '.';
import { flattenKeys } from '.';
import useSyncState from '../../../_util/hooks/useSyncState';
import warning from '../../../_util/warning';
import Button from '../../../button';
import type { CheckboxChangeEvent } from '../../../checkbox';
import Checkbox from '../../../checkbox';
import { ConfigContext } from '../../../config-provider/context';
import Dropdown from '../../../dropdown';
import Empty from '../../../empty';
import type { MenuProps } from '../../../menu';
import Menu from '../../../menu';
import { OverrideProvider } from '../../../menu/OverrideContext';
import Radio from '../../../radio';
import type { EventDataNode } from '../../../tree';
import Tree from '../../../tree';
import useSyncState from '../../../_util/hooks/useSyncState';
import type {
ColumnFilterItem,
ColumnType,
FilterSearchType,
FilterValue,
GetPopupContainer,
Key,
TableLocale,
} from '../../interface';
import FilterSearch from './FilterSearch';
import FilterDropdownMenuWrapper from './FilterWrapper';
import warning from '../../../_util/warning';
type FilterTreeDataNode = FieldDataNode<{ title: React.ReactNode; key: React.Key }>;
@ -37,6 +37,17 @@ interface FilterRestProps {
closeDropdown?: Boolean;
}
export function flattenKeys(filters?: ColumnFilterItem[]) {
let keys: FilterValue = [];
(filters || []).forEach(({ value, children }) => {
keys.push(value);
if (children) {
keys = [...keys, ...flattenKeys(children)];
}
});
return keys;
}
function hasSubMenu(filters: ColumnFilterItem[]) {
return filters.some(({ children }) => children);
}

16
components/table/hooks/useFilter/index.tsx

@ -1,7 +1,6 @@
import * as React from 'react';
import warning from '../../../_util/warning';
import type {
ColumnFilterItem,
ColumnsType,
ColumnTitleProps,
ColumnType,
@ -13,7 +12,7 @@ import type {
TransformColumns,
} from '../../interface';
import { getColumnKey, getColumnPos, renderColumnTitle } from '../../util';
import FilterDropdown from './FilterDropdown';
import FilterDropdown, { flattenKeys } from './FilterDropdown';
export interface FilterState<RecordType> {
column: ColumnType<RecordType>;
@ -129,17 +128,6 @@ function injectFilter<RecordType>(
});
}
export function flattenKeys(filters?: ColumnFilterItem[]) {
let keys: FilterValue = [];
(filters || []).forEach(({ value, children }) => {
keys.push(value);
if (children) {
keys = [...keys, ...flattenKeys(children)];
}
});
return keys;
}
function generateFilterInfo<RecordType>(filterStates: FilterState<RecordType>[]) {
const currentFilters: Record<string, FilterValue | null> = {};
@ -292,4 +280,6 @@ function useFilter<RecordType>({
return [transformColumns, mergedFilterStates, filters];
}
export { flattenKeys };
export default useFilter;

Loading…
Cancel
Save