Skip to content

Commit b5fd01f

Browse files
Use same convention as vue for componentdata
1 parent 28a3c4a commit b5fd01f

File tree

6 files changed

+79
-131
lines changed

6 files changed

+79
-131
lines changed

README.md

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
[![MIT License](https://img.shields.io/github/license/SortableJS/Vue.Draggable.svg)](https://github.com/SortableJS/Vue.Draggable/blob/master/LICENSE)
1212

1313

14-
Vue component (Vue.js 2.0) or directive (Vue.js 1.0) allowing drag-and-drop and synchronization with view model array.
14+
Vue component (Vue.js 2.0 and vue 3) or directive (Vue.js 1.0) allowing drag-and-drop and synchronization with view model array.
1515

1616
Based on and offering all features of [Sortable.js](https://github.com/RubaXa/Sortable)
1717

@@ -121,7 +121,9 @@ Draggable component should directly wrap the draggable elements, or a `transitio
121121
<div v-for="element in myArray" :key="element.id" class="item">
122122
{{element.name}}
123123
</div>
124-
<button slot="footer" @click="addPeople">Add</button>
124+
<template slot="footer">
125+
<button @click="addPeople">Add</button>
126+
</template>
125127
</draggable>
126128
```
127129
### With header slot:
@@ -130,7 +132,9 @@ Draggable component should directly wrap the draggable elements, or a `transitio
130132
<div v-for="element in myArray" :key="element.id" class="item">
131133
{{element.name}}
132134
</div>
133-
<button slot="header" @click="addPeople">Add</button>
135+
<template slot="header">
136+
<button @click="addPeople">Add</button>
137+
</template>
134138
</draggable>
135139
```
136140
@@ -155,7 +159,7 @@ computed: {
155159
156160
157161
### Props
158-
#### value
162+
#### modelValue
159163
Type: `Array`<br>
160164
Required: `false`<br>
161165
Default: `null`
@@ -172,9 +176,9 @@ Type: `Array`<br>
172176
Required: `false`<br>
173177
Default: `null`
174178
175-
Alternative to the `value` prop, list is an array to be synchronized with drag-and-drop.<br>
176-
The main difference is that `list` prop is updated by draggable component using splice method, whereas `value` is immutable.<br>
177-
**Do not use in conjunction with value prop.**
179+
Alternative to the `modelValue` prop, list is an array to be synchronized with drag-and-drop.<br>
180+
The main difference is that `list` prop is updated by draggable component using splice method, whereas `modelValue` is immutable.<br>
181+
**Do not use in conjunction with modelValue prop.**
178182
179183
#### All sortable options
180184
New in version 2.19
@@ -258,10 +262,7 @@ Required: `false`<br>
258262
Default: `null`<br>
259263
260264
This props is used to pass additional information to child component declared by [tag props](#tag).<br>
261-
Value:
262-
* `props`: props to be passed to the child component
263-
* `attrs`: attrs to be passed to the child component
264-
* `on`: events to be subscribe in the child component
265+
Value: an object corresponding to the attributes, props and events we would pass to the component.
265266
266267
Example (using [element UI library](http://element.eleme.io/#/en-US)):
267268
```HTML
@@ -281,16 +282,10 @@ methods: {
281282
},
282283
getComponentData() {
283284
return {
284-
on: {
285-
change: this.handleChange,
286-
input: this.inputChanged
287-
},
288-
attrs:{
289-
wrap: true
290-
},
291-
props: {
292-
value: this.activeNames
293-
}
285+
onChange: this.handleChange,
286+
onInput: this.inputChanged,
287+
wrap: true,
288+
value: this.activeNames
294289
};
295290
}
296291
}
@@ -341,7 +336,9 @@ Ex:
341336
<div v-for="element in myArray" :key="element.id" class="item">
342337
{{element.name}}
343338
</div>
344-
<button slot="header" @click="addPeople">Add</button>
339+
<template slot="header">
340+
<button @click="addPeople">Add</button>
341+
</template>
345342
</draggable>
346343
```
347344
@@ -356,15 +353,17 @@ Ex:
356353
<div v-for="element in myArray" :key="element.id" class="item">
357354
{{element.name}}
358355
</div>
359-
<button slot="footer" @click="addPeople">Add</button>
356+
<template slot="footer">
357+
<button @click="addPeople">Add</button>
358+
</template>
360359
</draggable>
361360
```
362361
### Gotchas
363362
364-
- Vue.draggable children should always map the list or value prop using a v-for directive
363+
- Vue.draggable children should always map the list or modelValue prop using a v-for directive
365364
* You may use [header](https://github.com/SortableJS/Vue.Draggable#header) and [footer](https://github.com/SortableJS/Vue.Draggable#footer) slot to by-pass this limitation.
366365
367-
- Children elements inside v-for should be keyed as any element in Vue.js. Be carefull to provide revelant key values in particular:
366+
- Children elements inside v-for should be keyed as any element in Vue.js. Be careful to provide relevant key values in particular:
368367
* typically providing array index as keys won't work as key should be linked to the items content
369368
* cloned elements should provide updated keys, it is doable using the [clone props](#clone) for example
370369

example/components/third-party.vue

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,8 @@ export default {
8080
],
8181
activeNames,
8282
collapseComponentData: {
83-
on: {
84-
"update:modelValue": this.inputChanged
85-
},
86-
props: {
87-
modelValue: activeNames
88-
}
83+
"onUpdate:modelValue": this.inputChanged,
84+
modelValue: activeNames
8985
}
9086
};
9187
},

src/core/componentBuilderHelper.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,13 @@ function project(entries) {
99
}, {});
1010
}
1111

12-
function getComponentAttributes({ $attrs, componentData }) {
12+
function getComponentAttributes({ $attrs, componentData = {} }) {
1313
const attributes = project(
1414
Object.entries($attrs).filter(([key, _]) => isHtmlAttribute(key))
1515
);
16-
if (!componentData) {
17-
return attributes;
18-
}
19-
const { on, props, attrs } = componentData;
20-
Object.entries(on || {}).forEach(([key, value]) => {
21-
attributes[`on${capitalize(key)}`] = value;
22-
});
2316
return {
2417
...attributes,
25-
...attrs,
26-
...props
18+
...componentData
2719
};
2820
}
2921

tests/unit/core/componentBuilderHelper.spec.js

Lines changed: 43 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,36 @@ describe("getComponentAttributes", () => {
1212
{
1313
$attrs: {},
1414
componentData: {
15-
attrs: {
16-
value: 89,
17-
},
18-
},
15+
value: 89
16+
}
1917
},
2018
{
21-
value: 89,
22-
},
19+
value: 89
20+
}
2321
],
2422
[
2523
{
2624
$attrs: {},
2725
componentData: {
28-
props: {
29-
prop1: "value",
30-
},
31-
attrs: {
32-
value: 89,
33-
},
34-
},
26+
prop1: "value",
27+
value: 89
28+
}
3529
},
3630
{
3731
value: 89,
38-
prop1: "value",
39-
},
32+
prop1: "value"
33+
}
4034
],
4135
[
4236
{
4337
$attrs: {
4438
id: "value",
45-
value: 89,
39+
value: 89
4640
}
4741
},
4842
{
49-
id: "value",
50-
},
43+
id: "value"
44+
}
5145
],
5246
[
5347
{
@@ -56,37 +50,19 @@ describe("getComponentAttributes", () => {
5650
id: 68,
5751
"data-application": "app",
5852
class: "my-class",
59-
other: "will be filtered",
53+
other: "will be filtered"
6054
},
6155
componentData: {
62-
attrs: {
63-
value: 89,
64-
},
65-
},
56+
value: 89
57+
}
6658
},
6759
{
6860
id: 68,
6961
class: "my-class",
7062
"data-application": "app",
71-
value: 89,
72-
},
73-
],
74-
[
75-
{
76-
$attrs: {
77-
},
78-
componentData: {
79-
on: {
80-
value: 89,
81-
input: 66
82-
},
83-
},
84-
},
85-
{
86-
onValue: 89,
87-
onInput: 66
88-
},
89-
],
63+
value: 89
64+
}
65+
]
9066
])("for %o returns %o", (value, expected) => {
9167
const actual = getComponentAttributes(value);
9268
expect(actual).toEqual(expected);
@@ -101,41 +77,41 @@ describe("createSortableOption", () => {
10177
[
10278
{
10379
$attrs: { id: "id", class: "class", "data-app": "app" },
104-
callBackBuilder: {},
80+
callBackBuilder: {}
10581
},
106-
{ draggable: ">*" },
82+
{ draggable: ">*" }
10783
],
10884
[
10985
{
11086
$attrs: { value: "43" },
111-
callBackBuilder: {},
87+
callBackBuilder: {}
11288
},
113-
{ value: "43", draggable: ">*" },
89+
{ value: "43", draggable: ">*" }
11490
],
11591
[
11692
{
11793
$attrs: {
11894
value: "43",
11995
"ghost-class": "phantom",
120-
draggable: ".draggable",
96+
draggable: ".draggable"
12197
},
122-
callBackBuilder: {},
98+
callBackBuilder: {}
12399
},
124100
{
125101
value: "43",
126102
ghostClass: "phantom",
127-
draggable: ".draggable",
128-
},
103+
draggable: ".draggable"
104+
}
129105
],
130106
[
131107
{
132108
$attrs: {
133109
value: "7",
134-
draggable: ".draggable",
110+
draggable: ".draggable"
135111
},
136112
callBackBuilder: {
137113
emit: eventName => eventName
138-
},
114+
}
139115
},
140116
{
141117
value: "7",
@@ -144,19 +120,19 @@ describe("createSortableOption", () => {
144120
onClone: "Clone",
145121
onFilter: "Filter",
146122
onSort: "Sort",
147-
onUnchoose: "Unchoose",
148-
},
123+
onUnchoose: "Unchoose"
124+
}
149125
],
150126
[
151127
{
152128
$attrs: {
153-
property: "property",
129+
property: "property"
154130
},
155131
callBackBuilder: {
156132
emit: eventName => `emit-${eventName}`,
157133
manage: eventName => `manage-${eventName}`,
158134
manageAndEmit: eventName => `manageAndEmit-${eventName}`
159-
},
135+
}
160136
},
161137
{
162138
property: "property",
@@ -171,25 +147,24 @@ describe("createSortableOption", () => {
171147
onEnd: "manageAndEmit-End",
172148
onStart: "manageAndEmit-Start",
173149
onRemove: "manageAndEmit-Remove",
174-
onUpdate: "manageAndEmit-Update",
175-
},
176-
],
150+
onUpdate: "manageAndEmit-Update"
151+
}
152+
]
177153
])("for %o returns %o", (value, expected) => {
178154
const actual = createSortableOption(value);
179155
expect(actual).toEqual(expected);
180156
});
181157
});
182158

183-
describe("getValidSortableEntries", () =>{
159+
describe("getValidSortableEntries", () => {
184160
test.each([
185-
[{newValue: 1}, [["newValue", 1]]],
186-
[{onStart: 1}, []],
187-
[{onStart: 1, newValue: 11}, [["newValue", 11]]],
188-
[{onStart: 1, id: "newId", attribute: "yes"}, [["attribute", "yes"]]],
189-
[{onStart: 1, "data-bind": "value", boolean: true}, [["boolean", true]]]
190-
])
191-
("for %o returns %o", (value, expected) => {
161+
[{ newValue: 1 }, [["newValue", 1]]],
162+
[{ onStart: 1 }, []],
163+
[{ onStart: 1, newValue: 11 }, [["newValue", 11]]],
164+
[{ onStart: 1, id: "newId", attribute: "yes" }, [["attribute", "yes"]]],
165+
[{ onStart: 1, "data-bind": "value", boolean: true }, [["boolean", true]]]
166+
])("for %o returns %o", (value, expected) => {
192167
const actual = getValidSortableEntries(value);
193168
expect(actual).toEqual(expected);
194169
});
195-
})
170+
});

tests/unit/helper/DraggableWithComponent.vue

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
<template>
2-
<draggable
3-
:list="array"
4-
tag="fake"
5-
:component-data="{ props: { prop1: 'my-id' } }"
6-
>
2+
<draggable :list="array" tag="fake" :component-data="{ prop1: 'my-id' }">
73
<div v-for="item in array" :key="item">{{ item }}</div>
84
</draggable>
95
</template>

0 commit comments

Comments
 (0)