Various features/fixes (#727)

* fix(data-table): export useStaticWidth prop

* docs(data-table): add static width example

* fix(data-table): do not render table header if title/description not provided

* feat(modal): dispatch "click:button--primary" as an alias to "submit"

* test: update DataTable types test

* test(modal): update modal type tests

* docs(data-table): add clear reminder to key headers/rows
This commit is contained in:
Eric Liu 2021-07-05 13:22:56 -07:00 committed by GitHub
commit 0a69f8ec74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 128 additions and 28 deletions

View file

@ -686,7 +686,7 @@ None.
### Events ### Events
| Event name | Type | Detail | | Event name | Type | Detail |
| :------------ | :--------- | :------------------------------ | | :-------------------- | :--------- | :------------------------------ |
| transitionend | dispatched | <code>{ open: boolean; }</code> | | transitionend | dispatched | <code>{ open: boolean; }</code> |
| keydown | forwarded | -- | | keydown | forwarded | -- |
| click | forwarded | -- | | click | forwarded | -- |
@ -694,6 +694,7 @@ None.
| mouseenter | forwarded | -- | | mouseenter | forwarded | -- |
| mouseleave | forwarded | -- | | mouseleave | forwarded | -- |
| submit | dispatched | -- | | submit | dispatched | -- |
| click:button--primary | dispatched | -- |
| close | dispatched | -- | | close | dispatched | -- |
| open | dispatched | -- | | open | dispatched | -- |
@ -958,6 +959,7 @@ export interface DataTableCell {
| radio | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` for the radio selection variant | | radio | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` for the radio selection variant |
| batchSelection | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to enable batch selection | | batchSelection | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to enable batch selection |
| stickyHeader | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to enable a sticky header | | stickyHeader | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to enable a sticky header |
| useStaticWidth | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to use static width |
### Slots ### Slots
@ -2269,6 +2271,7 @@ None.
| mouseenter | forwarded | -- | | mouseenter | forwarded | -- |
| mouseleave | forwarded | -- | | mouseleave | forwarded | -- |
| submit | dispatched | -- | | submit | dispatched | -- |
| click:button--primary | dispatched | -- |
| close | dispatched | -- | | close | dispatched | -- |
| open | dispatched | -- | | open | dispatched | -- |

View file

@ -1530,6 +1530,7 @@
{ "type": "forwarded", "name": "mouseenter", "element": "div" }, { "type": "forwarded", "name": "mouseenter", "element": "div" },
{ "type": "forwarded", "name": "mouseleave", "element": "div" }, { "type": "forwarded", "name": "mouseleave", "element": "div" },
{ "type": "dispatched", "name": "submit" }, { "type": "dispatched", "name": "submit" },
{ "type": "dispatched", "name": "click:button--primary" },
{ "type": "dispatched", "name": "close" }, { "type": "dispatched", "name": "close" },
{ "type": "dispatched", "name": "open" } { "type": "dispatched", "name": "open" }
], ],
@ -2095,6 +2096,16 @@
"isFunction": false, "isFunction": false,
"constant": false, "constant": false,
"reactive": false "reactive": false
},
{
"name": "useStaticWidth",
"kind": "let",
"description": "Set to `true` to use static width",
"type": "boolean",
"value": "false",
"isFunction": false,
"constant": false,
"reactive": false
} }
], ],
"slots": [ "slots": [
@ -5500,6 +5511,7 @@
{ "type": "forwarded", "name": "mouseenter", "element": "div" }, { "type": "forwarded", "name": "mouseenter", "element": "div" },
{ "type": "forwarded", "name": "mouseleave", "element": "div" }, { "type": "forwarded", "name": "mouseleave", "element": "div" },
{ "type": "dispatched", "name": "submit" }, { "type": "dispatched", "name": "submit" },
{ "type": "dispatched", "name": "click:button--primary" },
{ "type": "dispatched", "name": "close" }, { "type": "dispatched", "name": "close" },
{ "type": "dispatched", "name": "open" } { "type": "dispatched", "name": "open" }
], ],

View file

@ -3,15 +3,23 @@ components: ["DataTable", "Toolbar", "ToolbarContent", "ToolbarSearch", "Toolbar
--- ---
<script> <script>
import { DataTable, DataTableSkeleton, Toolbar, ToolbarContent, ToolbarSearch, ToolbarMenu, ToolbarMenuItem, Button, Link } from "carbon-components-svelte"; import { InlineNotification, DataTable, DataTableSkeleton, Toolbar, ToolbarContent, ToolbarSearch, ToolbarMenu, ToolbarMenuItem, Button, Link } from "carbon-components-svelte";
import Launch16 from "carbon-icons-svelte/lib/Launch16"; import Launch16 from "carbon-icons-svelte/lib/Launch16";
import Preview from "../../components/Preview.svelte"; import Preview from "../../components/Preview.svelte";
</script> </script>
`DataTable` is keyed for both rendering and sorting purposes.
<InlineNotification svx-ignore title="Note:" kind="info" hideCloseButton>
<div class="body-short-01">Every <code>headers</code> object must have a unique "key" property.</div>
</InlineNotification>
<InlineNotification svx-ignore title="Note:" kind="info" hideCloseButton>
<div class="body-short-01">Every <code>rows</code> object must have a unique "id" property.</div>
</InlineNotification>
### Default ### Default
The `DataTable` is keyed for both rendering and sorting. You must define a "key" value per object in the `headers` property and an "id" value in `rows`.
<DataTable <DataTable
headers="{[ headers="{[
{ key: "name", value: "Name" }, { key: "name", value: "Name" },
@ -65,6 +73,63 @@ The `DataTable` is keyed for both rendering and sorting. You must define a "key"
]}" ]}"
/> />
### Static width
Set `useStaticWidth` to `true` to render the table with a width of "auto" instead of "100%".
<DataTable useStaticWidth
headers="{[
{ key: "name", value: "Name" },
{ key: "protocol", value: "Protocol" },
{ key: "port", value: "Port" },
{ key: "rule", value: "Rule" }
]}"
rows="{[
{
id: "a",
name: "Load Balancer 3",
protocol: "HTTP",
port: 3000,
rule: "Round robin"
},
{
id: "b",
name: "Load Balancer 1",
protocol: "HTTP",
port: 443,
rule: "Round robin"
},
{
id: "c",
name: "Load Balancer 2",
protocol: "HTTP",
port: 80,
rule: "DNS delegation"
},
{
id: "d",
name: "Load Balancer 6",
protocol: "HTTP",
port: 3000,
rule: "Round robin"
},
{
id: "e",
name: "Load Balancer 4",
protocol: "HTTP",
port: 443,
rule: "Round robin"
},
{
id: "f",
name: "Load Balancer 5",
protocol: "HTTP",
port: 80,
rule: "DNS delegation"
},
]}"
/>
### Slotted cells ### Slotted cells
Use the `"cell"` slot to control the display value per table cell. Individual row and cell data are provided through the `let:row` and `let:cell` directives. Use the `"cell"` slot to control the display value per table cell. Individual row and cell data are provided through the `let:row` and `let:cell` directives.

View file

@ -53,6 +53,7 @@
}, },
submit: () => { submit: () => {
dispatch("submit"); dispatch("submit");
dispatch("click:button--primary");
}, },
declareRef: (ref) => { declareRef: (ref) => {
buttonRef = ref; buttonRef = ref;

View file

@ -91,6 +91,9 @@
/** Set to `true` to enable a sticky header */ /** Set to `true` to enable a sticky header */
export let stickyHeader = false; export let stickyHeader = false;
/** Set to `true` to use static width */
export let useStaticWidth = false;
import { createEventDispatcher, setContext } from "svelte"; import { createEventDispatcher, setContext } from "svelte";
import { writable, derived } from "svelte/store"; import { writable, derived } from "svelte/store";
import ChevronRight16 from "carbon-icons-svelte/lib/ChevronRight16/ChevronRight16.svelte"; import ChevronRight16 from "carbon-icons-svelte/lib/ChevronRight16/ChevronRight16.svelte";
@ -203,6 +206,7 @@
</script> </script>
<TableContainer {...$$restProps}> <TableContainer {...$$restProps}>
{#if title || $$slots.title || description || $$slots.description}
<div class:bx--data-table-header="{true}"> <div class:bx--data-table-header="{true}">
{#if title || $$slots.title} {#if title || $$slots.title}
<h4 class:bx--data-table-header__title="{true}"> <h4 class:bx--data-table-header__title="{true}">
@ -215,12 +219,14 @@
</p> </p>
{/if} {/if}
</div> </div>
{/if}
<slot /> <slot />
<Table <Table
zebra="{zebra}" zebra="{zebra}"
size="{size}" size="{size}"
stickyHeader="{stickyHeader}" stickyHeader="{stickyHeader}"
sortable="{sortable}" sortable="{sortable}"
useStaticWidth="{useStaticWidth}"
> >
<TableHead> <TableHead>
<TableRow> <TableRow>

View file

@ -297,6 +297,7 @@
disabled="{primaryButtonDisabled}" disabled="{primaryButtonDisabled}"
on:click="{() => { on:click="{() => {
dispatch('submit'); dispatch('submit');
dispatch('click:button--primary');
}}" }}"
> >
{primaryButtonText} {primaryButtonText}

View file

@ -10,7 +10,7 @@
let checked = false; let checked = false;
</script> </script>
<ComposedModal open> <ComposedModal open on:close on:click:button--primary>
<ModalHeader title="Confirm changes" /> <ModalHeader title="Confirm changes" />
<ModalBody hasForm> <ModalBody hasForm>
<Checkbox labelText="I have reviewed the changes" bind:checked /> <Checkbox labelText="I have reviewed the changes" bind:checked />
@ -19,6 +19,8 @@
primaryButtonText="Proceed" primaryButtonText="Proceed"
primaryButtonDisabled="{!checked}" primaryButtonDisabled="{!checked}"
secondaryButtons="{[{ text: 'Cancel' }, { text: 'Duplicate' }]}" secondaryButtons="{[{ text: 'Cancel' }, { text: 'Duplicate' }]}"
on:click:button--secondary="{({ detail }) => {}}" on:click:button--secondary="{({ detail }) => {
console.log(detail);
}}"
/> />
</ComposedModal> </ComposedModal>

View file

@ -98,6 +98,7 @@
description="Your organization's active load balancers." description="Your organization's active load balancers."
headers="{headers}" headers="{headers}"
rows="{rows}" rows="{rows}"
useStaticWidth
/> />
<DataTable <DataTable

View file

@ -16,6 +16,7 @@
on:open on:open
on:close on:close
on:submit on:submit
on:click:button--primary
> >
<p>Create a new Cloudant database in the US South region.</p> <p>Create a new Cloudant database in the US South region.</p>
</Modal> </Modal>

View file

@ -55,6 +55,7 @@ export default class ComposedModal extends SvelteComponentTyped<
mouseenter: WindowEventMap["mouseenter"]; mouseenter: WindowEventMap["mouseenter"];
mouseleave: WindowEventMap["mouseleave"]; mouseleave: WindowEventMap["mouseleave"];
submit: CustomEvent<any>; submit: CustomEvent<any>;
["click:button--primary"]: CustomEvent<any>;
close: CustomEvent<any>; close: CustomEvent<any>;
open: CustomEvent<any>; open: CustomEvent<any>;
}, },

View file

@ -129,6 +129,12 @@ export interface DataTableProps
* @default false * @default false
*/ */
stickyHeader?: boolean; stickyHeader?: boolean;
/**
* Set to `true` to use static width
* @default false
*/
useStaticWidth?: boolean;
} }
export default class DataTable extends SvelteComponentTyped< export default class DataTable extends SvelteComponentTyped<

View file

@ -132,6 +132,7 @@ export default class Modal extends SvelteComponentTyped<
mouseenter: WindowEventMap["mouseenter"]; mouseenter: WindowEventMap["mouseenter"];
mouseleave: WindowEventMap["mouseleave"]; mouseleave: WindowEventMap["mouseleave"];
submit: CustomEvent<any>; submit: CustomEvent<any>;
["click:button--primary"]: CustomEvent<any>;
close: CustomEvent<any>; close: CustomEvent<any>;
open: CustomEvent<any>; open: CustomEvent<any>;
}, },