Skip to content

Commit 224b33d

Browse files
authored
Add support for header and footer slots (#54)
* Add footer and header slots * update readme
1 parent 065e3ac commit 224b33d

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { Sortable } from "sortablejs-vue3";
3636

3737
3. Use the component:
3838

39-
```typescript
39+
```vue
4040
<template>
4141
<main>
4242
<Sortable
@@ -45,18 +45,24 @@ import { Sortable } from "sortablejs-vue3";
4545
tag="div"
4646
:options="options"
4747
>
48+
<-- The Header and Footer templates below are optional -->
49+
<template #header>
50+
<h1>SortableJS Vue3 Demo</h1>
51+
</template>
4852
<template #item="{element, index}">
4953
<div class="draggable" :key="element.id">
5054
{{ element.name }}
5155
</div>
5256
</template>
57+
<template #footer>
58+
<div class="draggable">A footer</div>
59+
</template>
5360
</Sortable>
5461
</template>
5562
```
5663

5764
4. The `list` and `item-key` props are necessary. The `options` prop is an object that can contain any SortableJS option. You can find a full list of them here: https://github.com/SortableJS/Sortable#options
58-
59-
5. The `tag` prop is an optional prop, it's the HTML node type of the element that creates an outer element for the included slot. the default value is `div`
65+
- The `tag` prop is optional and defaults to `div`. It's the HTML node type for the outer element of the included template/slot.
6066

6167
### Props
6268

@@ -88,9 +94,9 @@ You can listen to Sortable events by adding the listeners to the `Sortable` comp
8894
>
8995
```
9096

91-
### Vuex
97+
### Use with a Store
9298

93-
No changes are necessary to work with Vuex. Just pass `store.state.items` as your list. To modify your data you need to manually listen to the events and calculate the new position with `event.oldIndex` and `event.newIndex` with something like the following:
99+
No changes are necessary to work with Vuex or another store. Just pass `store.state.items` as your list. To modify your data you need to manually listen to the events and calculate the new position with `event.oldIndex` and `event.newIndex` with something like the following:
94100

95101
```typescript
96102
const moveItemInArray = <T>(array: T[], from: number, to: number) => {
@@ -101,6 +107,8 @@ const moveItemInArray = <T>(array: T[], from: number, to: number) => {
101107
onEnd(event) { moveItemInArray(store.state.items, event.oldIndex, event.newIndex) }
102108
```
103109

110+
You may also want to see the SortableJS store documentation [here](https://github.com/SortableJS/Sortable#store).
111+
104112
## Development
105113

106114
1. Run `yarn` to install dependencies

src/components/Sortable.vue

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,17 @@ onUnmounted(() => {
111111
sortable.value = null;
112112
}
113113
});
114+
114115
</script>
115116

116117
<template>
117118
<component ref="containerRef" :is="$props.tag" :class="$props.class">
118-
<slot
119-
v-for="(item, index) of list"
120-
:key="getKey(item)"
121-
:element="item"
122-
:index="index"
123-
name="item"
124-
></slot>
119+
<header v-if="$slots['header']">
120+
<slot name="header"></slot>
121+
</header>
122+
<slot v-for="(item, index) of list" :key="getKey(item)" :element="item" :index="index" name="item"></slot>
123+
<footer v-if="$slots['footer']">
124+
<slot name="footer"></slot>
125+
</footer>
125126
</component>
126127
</template>

0 commit comments

Comments
 (0)