-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathREADME.md
197 lines (142 loc) · 8.46 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# vue-nestable
Drag & drop hierarchical list made as a vue component.
[![NPM Version][npm-image]][npm-url]
[![Build Status][travis-image]][travis-url]
[npm-image]: https://img.shields.io/npm/v/vue-nestable.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/vue-nestable
[travis-image]: https://img.shields.io/travis/rhwilr/vue-nestable/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/rhwilr/vue-nestable
> This package is currently only compatible with Vue 2. Vue 3 compatibility
> may be added in the future, though there are currently no plans for it.
<hr />
## Goals
- A simple vue component to create a draggable list to customizable items
- Reorder items by dragging them above another item
- Intuitively nest items by dragging right
- Fully customizable, ships with no CSS
- Everything is configurable: item identifier, max nesting level, threshold
for nesting
## Table of contents
* [Demo](#Demo)
* [Installation](#installation)
* [Usage](#usage)
* [Example](#example)
* [Styling](#styling)
* [Props](#props)
* [Slots](#slots)
* [Events](#events)
* [Hooks](#hooks)
## Demo
[Live Demo](https://rhwilr.github.io/vue-nestable/)
## Installation
Install the plugin:
```sh
npm install --save vue-nestable
```
Use the plugin in your app:
```js
import Vue from 'vue'
import VueNestable from 'vue-nestable'
Vue.use(VueNestable)
```
You can also import the components on-demand, if you wish to do so:
```js
import { VueNestable, VueNestableHandle } from 'vue-nestable'
export default {
components: {
VueNestable,
VueNestableHandle
}
...
}
```
## Example
You only need two components: `vue-nestable` which renders the list and
`vue-nestable-handle` which indicates the area the user can drag the item by.
**Important Note:** Each item must have a unique `id` property and it must be a
valid css class name. It can not contain a `:`, `,`, `.`, `;` or other special
characters that are invalid in a css class name.
```html
<template>
<vue-nestable v-model="nestableItems">
<vue-nestable-handle
slot-scope="{ item }"
:item="item">
{{ item.text }}
</vue-nestable-handle>
</vue-nestable>
</template>
<script type="text/babel">
export default {
data () {
return {
nestableItems: [
{
id: 0,
text: 'Andy'
}, {
id: 1,
text: 'Harry',
children: [{
id: 2,
text: 'David'
}]
}, {
id: 3,
text: 'Lisa'
}
]
}
}
}
</script>
```
## Styling
By default, vue-nestable comes without any styling. Which means you can
customize the appearance completely to your needs. However, if you want you can
take a look at the style used in the demo:
[example/assets/vue-nestable.css](example/assets/vue-nestable.css)
## Props
The following props can be passed to the `<VueNestable>` Component:
| Property | Type | Default | Description |
| :----------- | :------------------ | :------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| value | Array | [ ] | Array of objects to be used in the list. **Important:** Each item must have a unique key by which it can be identified. By default the key is assumed to be named `id` but you can change it by setting the `keyProp` property. |
| threshold | Number | 30 | Amount of pixels by which the mouse must be move horizontally before increasing/decreasing level (nesting) of current element. |
| maxDepth | Number | 10 | Maximum available level of nesting. Setting this to 0 will prevent dragging altogether. |
| group | String or Number | random String | Different group numbers may be passed if you have more than one nestable component on a page and want some extra styles for portal instances. |
| keyProp | String *(Optional)* | 'id' | Name of the property that uniquely identifies an item. |
| childrenProp | String *(Optional)* | 'children' | Name of the property that holds an array of children. |
| class | String *(Optional)* | null | Name of the property for classes to add to the item. |
| hooks | Object *(Optional)* | {} | Allows you to register hooks that fire whenever vue-nestable performs some action |
| rtl | Boolean *(Optional)* | false | Add rtl support to vue-nestable |
## Slots
The `<VueNestable>` Component has two slots that can be used to render items and
a placeholder. See [Example](example/components/NoItems.vue) for an example on
how to use them.
| Slot Name | Props | Description |
| :---------- | :------------------------- | :------------------------------------------------------------------------------------------------------------ |
| default | `item`, `index`, `isChild` | This slot is used to render the items in the list, use the scoped-slot property `item` to render the element. |
| placeholder | | Lets you define a custom template that is used when no elements are in the list |
## Events
Events are triggered when an item was moved or when a drag operation was
completed. When you use `v-model` to bind your data, the `@input` event will
automatically be handled.
| Event | Parameters | Description |
| :----- | :----------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| input | `value` | triggered whenever the list changes |
| change | `value`, `options` | triggered when the user dropped the item. `options` is passed as the second parameter in the event and contains the following properties: `{ items, pathTo }` |
## Hooks
Hooks allow you to get finer controll over which items can be moved or take
action when a specific item is moved.
Hooks are passed as an Object to the `:hooks` prop. The object defines a key
with the hook name and a function that will be called when the hook fires.
```js
{
'beforeMove': this.myHookFunction
}
```
Look [here](example/components/Advanced.vue) of an example on how to prevent one
item from being moved.
| Hook Name | Parameters | Description |
| :------------ | :------------------------------- | :------------------------------------------------------------------------------------------- |
| beforeMove | `{ dragItem, pathFrom, pathTo }` | Fires when an item is about to be moved. Returning `false` will cancel that action. |