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

fix(data-table): handle add/remove columns #4017

Merged
merged 3 commits into from
Jan 8, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/data-table/__tests__/data-table-add-remove-columns.scenario.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright (c) 2018-2020 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow

import * as React from 'react';

import BooleanColumn from '../column-boolean.js';
import {StatefulDataTable} from '../stateful-data-table.js';

function getColumn(index) {
return BooleanColumn({
title: index.toString(),
mapDataToValue: () => true,
});
}

const rows = new Array(5).fill().map((_, i) => ({id: i, data: {}}));

export default function Scenario() {
const [columns, setColumns] = React.useState([getColumn(0), getColumn(1)]);
return (
<div>
<button
data-testid="add"
onClick={() => setColumns(prev => [...prev, getColumn(prev.length)])}
>
Add Column
</button>
<button
data-testid="remove"
onClick={() => setColumns(prev => prev.slice(0, -1))}
>
Remove Column
</button>

<div style={{height: '400px', width: '900px'}}>
<StatefulDataTable
resizableColumnWidths
columns={columns || []}
rows={rows}
/>
</div>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright (c) 2018-2020 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow

import * as React from 'react';

import {Tag, KIND as TAG_KIND} from '../../tag/index.js';

import CategoricalColumn from '../column-categorical.js';
import CustomColumn from '../column-custom.js';
import StringColumn from '../column-string.js';
import {StatefulDataTable} from '../stateful-data-table.js';

import AnimalData from './animal-data.js';

type RowDataT = {
Name: string,
Kingdom: string,
Phylum: string,
Class: string,
Order: string,
Family: string,
};

const columns = [
StringColumn({
title: 'Name',
minWidth: 300,
mapDataToValue: (data: RowDataT) => data.Name,
}),
CategoricalColumn({
title: 'Kingdom',
mapDataToValue: (data: RowDataT) => data.Kingdom,
}),
CustomColumn({
title: 'Phylum',
minWidth: 90,
mapDataToValue: (data: RowDataT) => data.Phylum,
textQueryFilter: function(textQuery, data) {
return data.toLowerCase().includes(textQuery.toLowerCase());
},
renderCell: function PhylumnCell(props) {
return (
<Tag
closeable={false}
overrides={{
Root: {
style: {
marginTop: 0,
marginRight: 0,
marginBottom: 0,
marginLeft: 0,
},
},
}}
kind={TAG_KIND.accent}
>
{props.value}
</Tag>
);
},
}),
CategoricalColumn({
title: 'Class',
minWidth: 120,
mapDataToValue: (data: RowDataT) => data.Class,
}),
CategoricalColumn({
title: 'Order',
mapDataToValue: (data: RowDataT) => data.Order,
}),
CategoricalColumn({
title: 'Family',
mapDataToValue: (data: RowDataT) => data.Family,
}),
];

const rows = AnimalData.map(row => {
return {
id: row.Name,
data: row,
};
});

export default function Scenario() {
return (
<div style={{height: '600px', width: '700px'}}>
<StatefulDataTable resizableColumnWidths columns={columns} rows={rows} />
</div>
);
}