mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 02:11:05 +00:00
feat(dropdown): make Dropdown
slottable (#1182)
This commit is contained in:
parent
2858776367
commit
f5594daba6
7 changed files with 57 additions and 5 deletions
|
@ -1197,7 +1197,9 @@ export interface DropdownItem {
|
||||||
|
|
||||||
### Slots
|
### Slots
|
||||||
|
|
||||||
None.
|
| Slot name | Default | Props | Fallback |
|
||||||
|
| :-------- | :------ | :--------------------------------------------------- | :-------------------------------- |
|
||||||
|
| -- | Yes | <code>{ item: DropdownItem; index: number; } </code> | <code>{itemToString(item)}</code> |
|
||||||
|
|
||||||
### Events
|
### Events
|
||||||
|
|
||||||
|
|
|
@ -3318,7 +3318,14 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"moduleExports": [],
|
"moduleExports": [],
|
||||||
"slots": [],
|
"slots": [
|
||||||
|
{
|
||||||
|
"name": "__default__",
|
||||||
|
"default": true,
|
||||||
|
"fallback": "{itemToString(item)}",
|
||||||
|
"slot_props": "{ item: DropdownItem; index: number; }"
|
||||||
|
}
|
||||||
|
],
|
||||||
"events": [
|
"events": [
|
||||||
{
|
{
|
||||||
"type": "dispatched",
|
"type": "dispatched",
|
||||||
|
|
|
@ -19,6 +19,12 @@ components: ["Dropdown", "DropdownSkeleton"]
|
||||||
{id: "1", text: "Email"},
|
{id: "1", text: "Email"},
|
||||||
{id: "2", text: "Fax"}]}" />
|
{id: "2", text: "Fax"}]}" />
|
||||||
|
|
||||||
|
### Custom slot
|
||||||
|
|
||||||
|
Override the default slot to customize the display of each item. Access the item and index through the `let:` directive.
|
||||||
|
|
||||||
|
<FileSource src="/framed/Dropdown/DropdownSlot" />
|
||||||
|
|
||||||
### Hidden label
|
### Hidden label
|
||||||
|
|
||||||
<Dropdown hideLabel titleText="Contact" selectedId="0" items="{[{id: "0", text: "Slack"},
|
<Dropdown hideLabel titleText="Contact" selectedId="0" items="{[{id: "0", text: "Slack"},
|
||||||
|
|
29
docs/src/pages/framed/Dropdown/DropdownSlot.svelte
Normal file
29
docs/src/pages/framed/Dropdown/DropdownSlot.svelte
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
<script>
|
||||||
|
import { Dropdown } from "carbon-components-svelte";
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<Dropdown
|
||||||
|
titleText="Contact"
|
||||||
|
selectedId="0"
|
||||||
|
items="{[
|
||||||
|
{ id: '0', text: 'Slack' },
|
||||||
|
{ id: '1', text: 'Email' },
|
||||||
|
{ id: '2', text: 'Fax' },
|
||||||
|
]}"
|
||||||
|
let:item
|
||||||
|
let:index
|
||||||
|
>
|
||||||
|
<div>
|
||||||
|
<strong>{item.text}</strong>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
id: {item.id} - index:
|
||||||
|
{index}
|
||||||
|
</div>
|
||||||
|
</Dropdown>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
:global(.bx--list-box__menu-item, .bx--list-box__menu-item__option) {
|
||||||
|
height: auto;
|
||||||
|
}
|
||||||
|
</style>
|
|
@ -4,6 +4,7 @@
|
||||||
* @typedef {string} DropdownItemText
|
* @typedef {string} DropdownItemText
|
||||||
* @typedef {{ id: DropdownItemId; text: DropdownItemText; }} DropdownItem
|
* @typedef {{ id: DropdownItemId; text: DropdownItemText; }} DropdownItem
|
||||||
* @event {{ selectedId: DropdownItemId, selectedItem: DropdownItem }} select
|
* @event {{ selectedId: DropdownItemId, selectedItem: DropdownItem }} select
|
||||||
|
* @slot {{ item: DropdownItem; index: number; }}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -256,7 +257,9 @@
|
||||||
highlightedIndex = i;
|
highlightedIndex = i;
|
||||||
}}"
|
}}"
|
||||||
>
|
>
|
||||||
{itemToString(item)}
|
<slot item="{item}" index="{i}">
|
||||||
|
{itemToString(item)}
|
||||||
|
</slot>
|
||||||
</ListBoxMenuItem>
|
</ListBoxMenuItem>
|
||||||
{/each}
|
{/each}
|
||||||
</ListBoxMenu>
|
</ListBoxMenu>
|
||||||
|
|
|
@ -14,7 +14,12 @@
|
||||||
on:select="{(e) => {
|
on:select="{(e) => {
|
||||||
console.log(e.detail.selectedId);
|
console.log(e.detail.selectedId);
|
||||||
}}"
|
}}"
|
||||||
/>
|
let:item
|
||||||
|
let:index
|
||||||
|
>
|
||||||
|
{item.id}
|
||||||
|
{index}
|
||||||
|
</Dropdown>
|
||||||
|
|
||||||
<Dropdown
|
<Dropdown
|
||||||
itemToString="{(item) => {
|
itemToString="{(item) => {
|
||||||
|
|
2
types/Dropdown/Dropdown.svelte.d.ts
vendored
2
types/Dropdown/Dropdown.svelte.d.ts
vendored
|
@ -153,5 +153,5 @@ export default class Dropdown extends SvelteComponentTyped<
|
||||||
selectedItem: DropdownItem;
|
selectedItem: DropdownItem;
|
||||||
}>;
|
}>;
|
||||||
},
|
},
|
||||||
{}
|
{ default: { item: DropdownItem; index: number } }
|
||||||
> {}
|
> {}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue