Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Table getPopupContainer #17806

Merged
merged 4 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 71 additions & 28 deletions components/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
locale: PropTypes.object,
dropdownPrefixCls: PropTypes.string,
sortDirections: PropTypes.array,
getPopupContainer: PropTypes.func,
};

static defaultProps = {
Expand Down Expand Up @@ -762,18 +763,24 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
return recordKey === undefined ? index : recordKey;
};

getPopupContainer = () => {
return ReactDOM.findDOMNode(this) as HTMLElement;
};

generatePopupContainerFunc = () => {
generatePopupContainerFunc = (getPopupContainer: TableProps<T>['getPopupContainer']) => {
const { scroll } = this.props;

if (getPopupContainer) {
return getPopupContainer;
}
// Use undefined to let rc component use default logic.
return scroll ? this.getPopupContainer : undefined;
return scroll ? () => ReactDOM.findDOMNode(this) as HTMLElement : undefined;
};

renderRowSelection(prefixCls: string, locale: TableLocale) {
renderRowSelection({
prefixCls,
locale,
getPopupContainer,
}: {
prefixCls: string;
locale: TableLocale;
getPopupContainer: TableProps<T>['getPopupContainer'];
}) {
const { rowSelection } = this.props;
const columns = this.columns.concat();
if (rowSelection) {
Expand Down Expand Up @@ -813,7 +820,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
onSelect={this.handleSelectRow}
selections={rowSelection.selections}
hideDefaultSelections={rowSelection.hideDefaultSelections}
getPopupContainer={this.generatePopupContainerFunc()}
getPopupContainer={this.generatePopupContainerFunc(getPopupContainer)}
/>
);
}
Expand Down Expand Up @@ -853,12 +860,19 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
return this.getColumnKey(sortColumn) === this.getColumnKey(column);
}

renderColumnsDropdown(
prefixCls: string,
dropdownPrefixCls: string,
columns: ColumnProps<T>[],
locale: TableLocale,
) {
renderColumnsDropdown({
prefixCls,
dropdownPrefixCls,
columns,
locale,
getPopupContainer,
}: {
prefixCls: string;
dropdownPrefixCls: string;
columns: ColumnProps<T>[];
locale: TableLocale;
getPopupContainer: TableProps<T>['getPopupContainer'];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成 object 参数声明形式,增减参数比较方便。

}) {
const { sortOrder, filters } = this.state;
return treeMap(columns, (column, i) => {
const key = this.getColumnKey(column, i) as string;
Expand All @@ -876,7 +890,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
confirmFilter={this.handleFilter}
prefixCls={`${prefixCls}-filter`}
dropdownPrefixCls={dropdownPrefixCls || 'ant-dropdown'}
getPopupContainer={this.generatePopupContainerFunc()}
getPopupContainer={this.generatePopupContainerFunc(getPopupContainer)}
key="filter-dropdown"
/>
);
Expand Down Expand Up @@ -1182,16 +1196,27 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
return null;
};

renderTable = (
prefixCls: string,
renderEmpty: RenderEmptyHandler,
dropdownPrefixCls: string,
contextLocale: TableLocale,
) => {
const { style, className, showHeader, locale, ...restProps } = this.props;
renderTable = ({
prefixCls,
renderEmpty,
dropdownPrefixCls,
contextLocale,
getPopupContainer: contextGetPopupContainer,
}: {
prefixCls: string;
renderEmpty: RenderEmptyHandler;
dropdownPrefixCls: string;
contextLocale: TableLocale;
getPopupContainer: TableProps<T>['getPopupContainer'];
}) => {
const { style, className, showHeader, locale, getPopupContainer, ...restProps } = this.props;
const data = this.getCurrentPageData();
const expandIconAsCell = this.props.expandedRowRender && this.props.expandIconAsCell !== false;

// use props.getPopupContainer first
const realGetPopupContainer = getPopupContainer || contextGetPopupContainer;

// Merge too locales
const mergedLocale = { ...contextLocale, ...locale };
if (!locale || !locale.emptyText) {
mergedLocale.emptyText = renderEmpty('Table');
Expand All @@ -1204,13 +1229,23 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
[`${prefixCls}-without-column-header`]: !showHeader,
});

let columns = this.renderRowSelection(prefixCls, mergedLocale);
columns = this.renderColumnsDropdown(prefixCls, dropdownPrefixCls, columns, mergedLocale);
columns = columns.map((column, i) => {
const columnsWithRowSelection = this.renderRowSelection({
prefixCls,
locale: mergedLocale,
getPopupContainer: realGetPopupContainer,
});
const columns = this.renderColumnsDropdown({
columns: columnsWithRowSelection,
prefixCls,
dropdownPrefixCls,
locale: mergedLocale,
getPopupContainer: realGetPopupContainer,
}).map((column, i) => {
const newColumn = { ...column };
newColumn.key = this.getColumnKey(newColumn, i);
return newColumn;
});

let expandIconColumnIndex = columns[0] && columns[0].key === 'selection-column' ? 1 : 0;
if ('expandIconColumnIndex' in restProps) {
expandIconColumnIndex = restProps.expandIconColumnIndex as number;
Expand All @@ -1235,7 +1270,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
);
};

renderComponent = ({ getPrefixCls, renderEmpty }: ConfigConsumerProps) => {
renderComponent = ({ getPrefixCls, renderEmpty, getPopupContainer }: ConfigConsumerProps) => {
const {
prefixCls: customizePrefixCls,
dropdownPrefixCls: customizeDropdownPrefixCls,
Expand All @@ -1255,7 +1290,15 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
const dropdownPrefixCls = getPrefixCls('dropdown', customizeDropdownPrefixCls);
const table = (
<LocaleReceiver componentName="Table" defaultLocale={defaultLocale.Table}>
{locale => this.renderTable(prefixCls, renderEmpty, dropdownPrefixCls, locale)}
{locale =>
this.renderTable({
prefixCls,
renderEmpty,
dropdownPrefixCls,
contextLocale: locale,
getPopupContainer,
})
}
</LocaleReceiver>
);

Expand Down
32 changes: 32 additions & 0 deletions components/table/__tests__/Table.filter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { render, mount } from 'enzyme';
import Table from '..';
import Input from '../../input';
import Button from '../../button';
import ConfigProvider from '../../config-provider';

function getDropdownWrapper(wrapper) {
return mount(
Expand Down Expand Up @@ -686,4 +687,35 @@ describe('Table.filter', () => {
dropdownWrapper2.find('.confirm').simulate('click');
expect(onChange).toHaveBeenCalled();
});

it('should support getPopupContainer', () => {
const wrapper = mount(
createTable({
columns: [
{
...column,
filterDropdownVisible: true,
},
],
getPopupContainer: node => node.parentNode,
}),
);
expect(wrapper.render()).toMatchSnapshot();
});

it('should support getPopupContainer from ConfigProvider', () => {
const wrapper = mount(
<ConfigProvider getPopupContainer={node => node.parentNode}>
{createTable({
columns: [
{
...column,
filterDropdownVisible: true,
},
],
})}
</ConfigProvider>
);
expect(wrapper.render()).toMatchSnapshot();
});
});
Loading