feat(toolbar-batch-actions): dispatch cancelable on:cancel event (#1441)

Closes #1438
This commit is contained in:
naegelin 2022-08-14 21:09:40 +02:00 committed by GitHub
commit 06777ba803
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 6 deletions

View file

@ -4436,7 +4436,9 @@ None.
### Events ### Events
None. | Event name | Type | Detail |
| :--------- | :--------- | :---------------- |
| cancel | dispatched | <code>null</code> |
## `ToolbarContent` ## `ToolbarContent`

View file

@ -13696,7 +13696,7 @@
"slot_props": "{}" "slot_props": "{}"
} }
], ],
"events": [], "events": [{ "type": "dispatched", "name": "cancel", "detail": "null" }],
"typedefs": [], "typedefs": [],
"rest_props": { "type": "Element", "name": "div" } "rest_props": { "type": "Element", "name": "div" }
}, },

View file

@ -1,4 +1,8 @@
<script> <script>
/**
* @event {null} cancel
*/
/** /**
* Override the total items selected text * Override the total items selected text
* @type {(totalSelected: number) => string} * @type {(totalSelected: number) => string}
@ -12,14 +16,26 @@
*/ */
export let active = false; export let active = false;
import { onMount, getContext } from "svelte"; import { onMount, getContext, createEventDispatcher } from "svelte";
import Button from "../Button/Button.svelte"; import Button from "../Button/Button.svelte";
let batchSelectedIds = []; let batchSelectedIds = [];
$: showActions = batchSelectedIds.length > 0 || active; const dispatch = createEventDispatcher();
const ctx = getContext("DataTable"); const ctx = getContext("DataTable");
function cancel() {
const shouldContinue = dispatch("cancel", null, { cancelable: true });
if (shouldContinue) {
ctx.resetSelectedRowIds();
}
}
$: showActions = batchSelectedIds.length > 0 || active;
const unsubscribe = ctx.batchSelectedIds.subscribe((value) => { const unsubscribe = ctx.batchSelectedIds.subscribe((value) => {
batchSelectedIds = value; batchSelectedIds = value;
}); });
@ -55,7 +71,7 @@
<Button <Button
class="bx--batch-summary__cancel" class="bx--batch-summary__cancel"
tabindex="{showActions ? '0' : '-1'}" tabindex="{showActions ? '0' : '-1'}"
on:click="{ctx.resetSelectedRowIds}" on:click="{cancel}"
> >
<slot name="cancel">Cancel</slot> <slot name="cancel">Cancel</slot>
</Button> </Button>

View file

@ -18,6 +18,6 @@ export interface ToolbarBatchActionsProps
export default class ToolbarBatchActions extends SvelteComponentTyped< export default class ToolbarBatchActions extends SvelteComponentTyped<
ToolbarBatchActionsProps, ToolbarBatchActionsProps,
{}, { cancel: CustomEvent<null> },
{ default: {}; cancel: {} } { default: {}; cancel: {} }
> {} > {}