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

Add support for nested object fields in DataTable #602

Merged
merged 8 commits into from
Apr 24, 2021
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- ## Unreleased -->

## [0.32.2](https://github.com/IBM/carbon-components-svelte/releases/tag/v0.32.2) - 2021-04-23

**Fixes**

- prevent multiple Dropdown items from being selected by updating the `selectedId`
- prevent the default keydown behavior so that pressing "Enter" should open the Dropdown menu
- resolve Carbon icon imports to Svelte files in `DataTable`, `HamburgerMenu` components

**Housekeeping**

- patch `carbon-components` to version 10.32.1

## [0.32.1](https://github.com/IBM/carbon-components-svelte/releases/tag/v0.32.1) - 2021-04-02

**Fixes**
Expand Down
2 changes: 1 addition & 1 deletion COMPONENT_INDEX.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Component Index

> 167 components exported from carbon-components-svelte@0.32.1.
> 167 components exported from carbon-components-svelte@0.32.2.

## Components

Expand Down
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"devDependencies": {
"@sveltech/routify": "^1.9.9",
"autoprefixer": "^10.2.3",
"carbon-components": "10.32.0",
"carbon-components": "10.32.1",
"carbon-components-svelte": "../",
"mdsvex": "^0.8.8",
"npm-run-all": "^4.1.5",
Expand Down
12 changes: 0 additions & 12 deletions docs/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,4 @@

// Import all component styles
@import "carbon-components/scss/globals/scss/styles";

.bx--list-box:not(.bx--list-box--light) input[role="combobox"],
.bx--list-box:not(.bx--list-box--light) input[type="text"],
.bx--dropdown:not(.bx--dropdown--light),
.bx--list-box:not(.bx--list-box--light),
.bx--number:not(.bx--number--light) input[type="number"],
.bx--number:not(.bx--number--light) .bx--number__control-btn::before,
.bx--number:not(.bx--number--light) .bx--number__control-btn::after,
.bx--text-input:not(.bx--text-input--light),
.bx--select-input {
background-color: $field-01;
}
</style>
79 changes: 79 additions & 0 deletions docs/src/pages/components/DataTable.svx
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,85 @@ In the example below, the "Protocol" column is not sortable.
]}"
/>

### Sortable with nested object values

<DataTable sortable title="Load balancers" description="Your organization's active load balancers."
headers="{[
{ key: "name", value: "Name" },
{ key: "network.protocol", value: "Protocol" },
{ key: "network.port", value: "Port" },
{ key: "cost", value: "Cost", display: (cost) => cost + " €" },
{
key: "expireDate",
value: "Expire date",
display: (date) => new Date(date).toLocaleString(),
sort: (a, b) => new Date(a) - new Date(b),
},
]}"
rows="{[
{
id: "a",
name: "Load Balancer 3",
network: {
protocol: "HTTP",
port: 3000,
},
cost: 100,
expireDate: "2020-10-21",
},
{
id: "b",
name: "Load Balancer 1",
network: {
protocol: "HTTP",
port: 443,
},
cost: 200,
expireDate: "2020-09-10",
},
{
id: "c",
name: "Load Balancer 2",
network: {
protocol: "HTTP",
port: 80,
},
cost: 150,
expireDate: "2020-11-24",
},
{
id: "d",
name: "Load Balancer 6",
network: {
protocol: "HTTP",
port: 3000,
},
cost: 250,
expireDate: "2020-12-01",
},
{
id: "e",
name: "Load Balancer 4",
network: {
protocol: "HTTP",
port: 443,
},
cost: 550,
expireDate: "2021-03-21",
},
{
id: "f",
name: "Load Balancer 5",
network: {
protocol: "HTTP",
port: 80,
},
cost: 400,
expireDate: "2020-11-14",
},
]}"
/>

### Empty column with overflow menu

Some use cases require an empty column in the table body without a corresponding table header.
Expand Down
10 changes: 5 additions & 5 deletions docs/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -837,16 +837,16 @@ caniuse-lite@^1.0.30001173, caniuse-lite@^1.0.30001178:
integrity sha512-n8JVqXuZMVSPKiPiypjFtDTXc4jWIdjxull0f92WLo7e1MSi3uJ3NvveakSh/aCl1QKFAvIz3vIj0v+0K+FrXw==

carbon-components-svelte@../:
version "0.31.1"
version "0.32.1"
dependencies:
carbon-icons-svelte "^10.27.0"
clipboard-copy "3.2.0"
flatpickr "4.6.9"

carbon-components@10.32.0:
version "10.32.0"
resolved "https://registry.yarnpkg.com/carbon-components/-/carbon-components-10.32.0.tgz#0fbf60ea8fc12cea43ae7860bec844b3c2c0880f"
integrity sha512-LM44X6VhUc+yrZBSz+IW1aT/OGxAS0oiiy/kV7CiDpsUDF5zlPOwgokZbeYLo2mhB1wBZ1q9jwvItZ4zfaZuMw==
carbon-components@10.32.1:
version "10.32.1"
resolved "https://registry.yarnpkg.com/carbon-components/-/carbon-components-10.32.1.tgz#064d4504daafafa379ee2fbc5b2997204fb2e5eb"
integrity sha512-m9Q9y1NSsXcLINSYmRmOOuxwvSNrXqC2FIN3ykg+WO3+WrmozbPsgFSyMs0gd/RUgNXP6edt8k0Op//oks01gA==
dependencies:
"@carbon/telemetry" "0.0.0-alpha.6"
flatpickr "4.6.1"
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "carbon-components-svelte",
"version": "0.32.1",
"version": "0.32.2",
"license": "Apache-2.0",
"author": "IBM",
"description": "Svelte implementation of the Carbon Design System",
Expand Down Expand Up @@ -32,7 +32,7 @@
"@rollup/plugin-node-resolve": "^11.1.1",
"@tsconfig/svelte": "^1.0.10",
"autoprefixer": "^10.2.4",
"carbon-components": "10.32.0",
"carbon-components": "10.32.1",
"gh-pages": "^3.1.0",
"husky": "^4.3.8",
"lint-staged": "^10.5.3",
Expand Down
2 changes: 1 addition & 1 deletion preprocess/api.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.32.1",
"version": "0.32.2",
"components": {
"Accordion": {
"path": "carbon-components-svelte/src/Accordion/Accordion.svelte"
Expand Down
18 changes: 15 additions & 3 deletions src/DataTable/DataTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@
.map(({ key }, i) => ({ key, id: $headerItems[i] }))
.reduce((a, c) => ({ ...a, [c.key]: c.id }), {})
);
const resolvePath = (object, path, defaultValue) =>
path
.split(/[\.\[\]\'\"]/)
.filter((p) => p)
.reduce(
(o, p) => (o && typeof o === "object" && o[p] ? o[p] : defaultValue),
object
);

setContext("DataTable", {
sortHeader,
Expand Down Expand Up @@ -159,7 +167,7 @@
$: headerKeys = headers.map(({ key }) => key);
$: rows = rows.map((row) => ({
...row,
cells: headerKeys.map((key) => ({ key, value: row[key] })),
cells: headerKeys.map((key) => ({ key, value: resolvePath(row, key, "") })),
}));
$: sortedRows = rows;
$: ascending = $sortHeader.sortDirection === "ascending";
Expand All @@ -170,8 +178,12 @@
sortedRows = rows;
} else {
sortedRows = [...rows].sort((a, b) => {
const itemA = ascending ? a[sortKey] : b[sortKey];
const itemB = ascending ? b[sortKey] : a[sortKey];
const itemA = ascending
? resolvePath(a, sortKey, "")
: resolvePath(b, sortKey, "");
const itemB = ascending
? resolvePath(b, sortKey, "")
: resolvePath(a, sortKey, "");

if ($sortHeader.sort) return $sortHeader.sort(itemA, itemB);

Expand Down
7 changes: 6 additions & 1 deletion src/Dropdown/Dropdown.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
}
$: inline = type === "inline";
$: selectedItem = items[selectedIndex];
$: selectedId = items[selectedIndex] ? items[selectedIndex].id : undefined;
$: if (!open) {
highlightedIndex = -1;
}
Expand Down Expand Up @@ -203,7 +204,11 @@
tabindex="0"
role="button"
aria-expanded="{open}"
on:keydown="{({ key }) => {
on:keydown="{(e) => {
const { key } = e;
if (['Enter', 'ArrowDown', 'ArrowUp'].includes(key)) {
e.preventDefault();
}
if (key === 'Enter') {
open = !open;
if (highlightedIndex > -1 && highlightedIndex !== selectedIndex) {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,10 @@ caniuse-lite@^1.0.30001181:
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001183.tgz#7a57ba9d6584119bb5f2bc76d3cc47ba9356b3e2"
integrity sha512-7JkwTEE1hlRKETbCFd8HDZeLiQIUcl8rC6JgNjvHCNaxOeNmQ9V4LvQXRUsKIV2CC73qKxljwVhToaA3kLRqTw==

carbon-components@10.32.0:
version "10.32.0"
resolved "https://registry.yarnpkg.com/carbon-components/-/carbon-components-10.32.0.tgz#0fbf60ea8fc12cea43ae7860bec844b3c2c0880f"
integrity sha512-LM44X6VhUc+yrZBSz+IW1aT/OGxAS0oiiy/kV7CiDpsUDF5zlPOwgokZbeYLo2mhB1wBZ1q9jwvItZ4zfaZuMw==
carbon-components@10.32.1:
version "10.32.1"
resolved "https://registry.yarnpkg.com/carbon-components/-/carbon-components-10.32.1.tgz#064d4504daafafa379ee2fbc5b2997204fb2e5eb"
integrity sha512-m9Q9y1NSsXcLINSYmRmOOuxwvSNrXqC2FIN3ykg+WO3+WrmozbPsgFSyMs0gd/RUgNXP6edt8k0Op//oks01gA==
dependencies:
"@carbon/telemetry" "0.0.0-alpha.6"
flatpickr "4.6.1"
Expand Down