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

Make detailscard table columns filterable #580

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
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,6 @@ exports[`<DdgNodeContent> renders the number of operations if there are multiple
<Popover
content={
<FilteredList
cancel={[Function]}
options={
Array [
"op0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,7 @@ export class UnconnectedDdgNodeContent extends React.PureComponent<TProps, TStat
>
{Array.isArray(operation) ? (
<Popover
content={
<FilteredList
cancel={() => {}}
options={operation}
value={null}
setValue={this.setOperation}
/>
}
content={<FilteredList options={operation} value={null} setValue={this.setOperation} />}
placement="bottom"
title="Select Operation to Filter Graph"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,20 @@

import React from 'react';
import { shallow } from 'enzyme';
import { Icon } from 'antd';
import FaFilter from 'react-icons/lib/fa/filter.js';

import ExamplesLink from '../ExamplesLink';
import DetailTable, { _onCell, _makeColumns, _renderCell, _rowKey, _sort } from './DetailTable';
import DetailTableDropdown from './DetailTableDropdown';
import DetailTable, {
_makeColumns,
_makeFilterDropdown,
_onCell,
_onFilter,
_renderCell,
_rowKey,
_sort,
} from './DetailTable';

describe('DetailTable', () => {
describe('render', () => {
Expand Down Expand Up @@ -92,13 +103,16 @@ describe('DetailTable', () => {
const stringColumn = 'stringCol';

describe('static props', () => {
const makeColumn = def => _makeColumns({ defs: [def] })[0];
const makeColumn = (def, rows = []) => _makeColumns({ defs: [def], rows })[0];

it('renders string column', () => {
expect(makeColumn(stringColumn)).toEqual({
dataIndex: stringColumn,
key: stringColumn,
title: stringColumn,
filterDropdown: false,
filterIcon: expect.any(Function),
onFilter: expect.any(Function),
onCell: expect.any(Function),
onHeaderCell: expect.any(Function),
render: expect.any(Function),
Expand All @@ -111,6 +125,9 @@ describe('DetailTable', () => {
dataIndex: stringColumn,
key: stringColumn,
title: stringColumn,
filterDropdown: false,
filterIcon: expect.any(Function),
onFilter: expect.any(Function),
onCell: expect.any(Function),
onHeaderCell: expect.any(Function),
render: expect.any(Function),
Expand All @@ -129,6 +146,9 @@ describe('DetailTable', () => {
dataIndex: stringColumn,
key: stringColumn,
title: label,
filterDropdown: false,
filterIcon: expect.any(Function),
onFilter: expect.any(Function),
onCell: expect.any(Function),
onHeaderCell: expect.any(Function),
render: expect.any(Function),
Expand All @@ -149,6 +169,36 @@ describe('DetailTable', () => {
).toBe(styling);
});

it('renders filter icon without filter set', () => {
const icon = makeColumn(stringColumn).filterIcon();
expect(icon.type).toBe(Icon);
expect(icon.props.type).toBe('filter');
});

it('renders filter icon with filter set', () => {
const icon = makeColumn(stringColumn).filterIcon(true);
expect(icon.type).toBe(FaFilter);
});

it('renders filterable column if there are filterable values', () => {
const filterableValues = ['foo', 'bar', { value: 'obj foo' }, { value: 'obj baz' }];
const expected = new Set(filterableValues.map(v => v.value || v));
const values = [
...filterableValues,
<ExamplesLink examples={[]} />,
<ExamplesLink key="fookey" examples={[]} />,
undefined,
];
const rows = values.map(value => ({
[stringColumn]: value,
}));
const column = makeColumn(stringColumn, rows);
const dropdown = column.filterDropdown();

expect(dropdown.props.options).toEqual(expected);
expect(dropdown.type).toBe(DetailTableDropdown);
});

it('renders object column without sort', () => {
expect(
makeColumn({
Expand All @@ -159,6 +209,9 @@ describe('DetailTable', () => {
dataIndex: stringColumn,
key: stringColumn,
title: stringColumn,
filterDropdown: false,
filterIcon: expect.any(Function),
onFilter: expect.any(Function),
onCell: expect.any(Function),
onHeaderCell: expect.any(Function),
render: expect.any(Function),
Expand All @@ -175,6 +228,25 @@ describe('DetailTable', () => {
}))
);

describe('_makeFilterDropdown', () => {
it('returns DetailsTableDropdown with correct props', () => {
const options = ['foo', 'bar'];
const mockAntdDropdownProps = {
foo: 'bar',
bar: 'baz',
baz: 'foo',
};
const filterDropdown = _makeFilterDropdown(stringColumn, options)(mockAntdDropdownProps);

expect(filterDropdown.type).toBe(DetailTableDropdown);
expect(filterDropdown.props).toEqual({
...mockAntdDropdownProps,
options,
});
expect(filterDropdown.key).toBe(stringColumn);
});
});

describe('_onCell', () => {
const onCell = makeTestFn(_onCell);

Expand Down Expand Up @@ -204,6 +276,36 @@ describe('DetailTable', () => {
});
});

describe('_onFilter', () => {
const onFilter = (filterValue, testValue) =>
_onFilter(stringColumn)(filterValue, { [stringColumn]: testValue });
const value = 'test-value';

it('returns true if string value is filter', () => {
expect(onFilter(value, value)).toBe(true);
});

it('returns true if object value is filter', () => {
expect(onFilter(value, { value })).toBe(true);
});

it('returns false if string value is not filter', () => {
expect(onFilter(value, `not-${value}`)).toBe(false);
});

it('returns false if object value is not filter', () => {
expect(onFilter(value, { value: `not-${value}` })).toBe(false);
});

it('returns false for array value', () => {
expect(onFilter(value, [value])).toBe(false);
});

it('returns false for undefined value', () => {
expect(onFilter(value)).toBe(false);
});
});

describe('_renderCell', () => {
it('renders a string', () => {
expect(_renderCell('a')).toBe('a');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@
// limitations under the License.

import * as React from 'react';
import { Table } from 'antd';
import { Icon, Table } from 'antd';
import FaFilter from 'react-icons/lib/fa/filter.js';
import _isEmpty from 'lodash/isEmpty';

import ExamplesLink, { TExample } from '../ExamplesLink';
import DetailTableDropdown from './DetailTableDropdown';

import { TColumnDef, TColumnDefs, TRow, TStyledValue } from './types';
import { TColumnDef, TColumnDefs, TFilterDropdownProps, TRow, TStyledValue } from './types';

// exported for tests
export const _makeFilterDropdown = (dataIndex: string, options: Set<string>) => (
props: TFilterDropdownProps
) => {
return <DetailTableDropdown {...props} key={dataIndex} options={options} />;
};

// exported for tests
export const _onCell = (dataIndex: string) => (row: TRow) => {
Expand All @@ -31,6 +40,15 @@ export const _onCell = (dataIndex: string) => (row: TRow) => {
};
};

// exported for tests
export const _onFilter = (dataIndex: string) => (value: string, row: TRow) => {
const data = row[dataIndex];
if (typeof data === 'object' && !Array.isArray(data) && typeof data.value === 'string') {
return data.value === value;
}
return data === value;
};

// exported for tests
export const _renderCell = (cellData: undefined | string | TStyledValue) => {
if (!cellData || typeof cellData !== 'object') return cellData;
Expand Down Expand Up @@ -62,7 +80,7 @@ export const _sort = (dataIndex: string) => (a: TRow, b: TRow) => {
};

// exported for tests
export const _makeColumns = ({ defs }: { defs: TColumnDefs }) =>
export const _makeColumns = ({ defs, rows }: { defs: TColumnDefs; rows: TRow[] }) =>
defs.map((def: TColumnDef | string) => {
let dataIndex: string;
let key: string;
Expand All @@ -80,14 +98,29 @@ export const _makeColumns = ({ defs }: { defs: TColumnDefs }) =>
if (def.preventSort) sortable = false;
}

const options = new Set<string>();
rows.forEach(row => {
const value = row[dataIndex];
if (typeof value === 'string' && value) options.add(value);
else if (typeof value === 'object' && !Array.isArray(value) && typeof value.value === 'string') {
options.add(value.value);
}
});

return {
dataIndex,
key,
title,
filterDropdown: Boolean(options.size) && _makeFilterDropdown(dataIndex, options),
filterIcon: (filtered: boolean) => {
if (filtered) return <FaFilter />;
return <Icon type="filter" />;
},
onCell: _onCell(dataIndex),
onHeaderCell: () => ({
style,
}),
onFilter: _onFilter(dataIndex),
render: _renderCell,
sorter: sortable && _sort(dataIndex),
};
Expand Down Expand Up @@ -138,7 +171,7 @@ export default function DetailTable({
<Table
key="table"
size="middle"
columns={_makeColumns({ defs: columnDefs })}
columns={_makeColumns({ defs: columnDefs, rows: details })}
dataSource={details}
pagination={false}
rowKey={_rowKey}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright (c) 2019 Uber Technologies, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

.DetailTableDropdown--Footer {
background: white;
border: 1px solid #ccc;
display: flex;
justify-content: space-between;
padding: 0.3em;
}

.DetailTableDropdown--Footer--CancelConfirm {
display: flex;
}

.DetailTableDropdown--Btn {
align-items: center;
border-radius: 0;
color: #ddd;
display: flex;
padding: 0.3em 0.7em 0.3em 0.6em;
}

.DetailTableDropdown--Btn ~ .DetailTableDropdown--Btn {
margin-left: 0.3em;
}

.DetailTableDropdown--Btn > *:not(:first-child) {
margin-left: 0.3em;
}

.DetailTableDropdown--Btn.Apply {
background: #4c21ce;
}

.DetailTableDropdown--Btn.Cancel {
background: #007272;
}

.DetailTableDropdown--Btn.Clear {
background: #ff2626;
}

.DetailTableDropdown--Tooltip {
max-width: unset;
white-space: nowrap;
}

.DetailTableDropdown--Tooltip--Body {
display: flex;
flex-direction: column;
}
Loading