refactor: solve circular reference of Table (#42813)

* refactor: solve circular reference of Table

* Update index.tsx

* chore: add export
This commit is contained in:
Zack Chang 2023-06-25 12:17:44 +08:00 committed by GitHub
parent 875a221c62
commit 7dc74b1da9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 18 deletions

View File

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

View File

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