Skip to content

Commit fd9bd4b

Browse files
Refactoring
1 parent 8feb7e8 commit fd9bd4b

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

src/core/componentBuilderHelper.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function getComponentAttributes({ $attrs, componentData }) {
2424
return { ...attributes, ...attrs, ...props };
2525
}
2626

27-
function getSortableOption({ $attrs, callBackBuilder }) {
27+
function createSortableOption({ $attrs, callBackBuilder }) {
2828
const options = {
2929
draggable: ">*"
3030
};
@@ -43,4 +43,14 @@ function getSortableOption({ $attrs, callBackBuilder }) {
4343
return options;
4444
}
4545

46-
export { getComponentAttributes, getSortableOption };
46+
function getValidSortableEntries(value) {
47+
return Object.entries(value)
48+
.map(([key, value]) => [camelize(key), value])
49+
.filter(([key, _]) => !isReadOnlyEvent(key));
50+
}
51+
52+
export {
53+
getComponentAttributes,
54+
createSortableOption,
55+
getValidSortableEntries
56+
};

src/vuedraggable.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import Sortable from "sortablejs";
22
import { insertNodeAt, removeNode } from "./util/htmlHelper";
33
import { console } from "./util/console";
4-
import { camelize } from "./util/string";
54
import { isHtmlTag, isTransition as isTransitionName } from "./util/tags";
65
import {
76
getComponentAttributes,
8-
getSortableOption
7+
createSortableOption,
8+
getValidSortableEntries
99
} from "./core/componentBuilderHelper";
10-
import { isReadOnlyEvent } from "./core/sortableEvents";
11-
1210
import { h, defineComponent, nextTick, resolveComponent } from "vue";
1311

1412
function computeVmIndex(vNodes, element) {
@@ -172,7 +170,7 @@ const draggableComponent = defineComponent({
172170
`Transition-group inside component is not supported. Please alter tag value or remove transition-group. Current tag value: ${tag}`
173171
);
174172
}
175-
const sortableOptions = getSortableOption({
173+
const sortableOptions = createSortableOption({
176174
$attrs,
177175
callBackBuilder: {
178176
manageAndEmit: event => manageAndEmit.call(this, event),
@@ -231,11 +229,8 @@ const draggableComponent = defineComponent({
231229

232230
updateOptions(newOptionValue) {
233231
const { _sortable } = this;
234-
Object.entries(newOptionValue).forEach(([key, value]) => {
235-
const normalizedKey = camelize(key);
236-
if (!isReadOnlyEvent(normalizedKey)) {
237-
_sortable.option(normalizedKey, value);
238-
}
232+
getValidSortableEntries(newOptionValue).forEach(([key, value]) => {
233+
_sortable.option(key, value);
239234
});
240235
},
241236

tests/unit/core/componentBuilderHelper.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
getComponentAttributes,
3-
getSortableOption,
3+
createSortableOption,
44
} from "@/core/componentBuilderHelper";
55

66
describe("getComponentAttributes", () => {
@@ -64,7 +64,7 @@ describe("getComponentAttributes", () => {
6464
});
6565
});
6666

67-
describe("getSortableOption", () => {
67+
describe("createSortableOption", () => {
6868
test.each([
6969
[{ $attrs: {}, callBackBuilder: {} }, { draggable: ">*" }],
7070
[{ $attrs: { onStart: 23 }, callBackBuilder: {} }, { draggable: ">*" }],
@@ -146,7 +146,7 @@ describe("getSortableOption", () => {
146146
},
147147
],
148148
])("for %o returns %o", (value, expected) => {
149-
const actual = getSortableOption(value);
149+
const actual = createSortableOption(value);
150150
expect(actual).toEqual(expected);
151151
});
152152
});

0 commit comments

Comments
 (0)