Skip to content

Commit 9c14cac

Browse files
Refactoring
1 parent fec76e5 commit 9c14cac

File tree

4 files changed

+66
-55
lines changed

4 files changed

+66
-55
lines changed

src/core/componentStructure.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { isTransition as isTransitionName } from "../util/tags";
2+
3+
function isTransition(nodes) {
4+
if (nodes.length !== 1) {
5+
return false;
6+
}
7+
const [{ type }] = nodes;
8+
return !!type && (isTransitionName(type) || isTransitionName(type.name));
9+
}
10+
11+
class ComponentStructure {
12+
constructor({ nodes: { header, default: defaultNodes, footer }, root }) {
13+
this.nodes = { header, default: defaultNodes, footer };
14+
this.children = [...header, ...defaultNodes, ...footer];
15+
this.offsets = {
16+
header: header.length,
17+
footer: footer.length
18+
};
19+
this.transitionMode = isTransition(defaultNodes);
20+
this.externalComponent = root.externalComponent;
21+
this.tag = root.tag;
22+
this.noneFunctional =
23+
this.externalComponent && typeof this.tag !== "function";
24+
}
25+
26+
checkCoherence() {
27+
if (this.noneFunctional && this.transitionMode) {
28+
throw new Error(
29+
`Transition-group inside component is not supported. Please alter tag value or remove transition-group. Current tag value: ${this.tag}`
30+
);
31+
}
32+
return this;
33+
}
34+
}
35+
36+
export { ComponentStructure };

src/core/renderHelper.js

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,37 @@
1-
import { isHtmlTag, isTransition as isTransitionName } from "../util/tags";
1+
import { ComponentStructure } from "./componentStructure";
2+
import { isHtmlTag, isTransition } from "../util/tags";
23
import { resolveComponent } from "vue";
34

45
function getSlot(slots, key) {
56
const slotValue = slots[key];
67
return slotValue ? slotValue() : [];
78
}
89

9-
function isTransition(nodes) {
10-
if (nodes.length !== 1) {
11-
return false;
12-
}
13-
const [{ type }] = nodes;
14-
return !!type && (isTransitionName(type) || isTransitionName(type.name));
15-
}
16-
17-
function computeChildrenAndNodes(slots) {
10+
function computeNodes(slots) {
1811
const [header, defaultNodes, footer] = [
1912
"header",
2013
"default",
21-
"footer"
22-
].map(name => getSlot(slots, name));
23-
const transitionMode = isTransition(defaultNodes);
14+
"footer",
15+
].map((name) => getSlot(slots, name));
2416
return {
25-
children: [...header, ...defaultNodes, ...footer],
26-
transitionMode,
27-
nodes: {
28-
header,
29-
footer,
30-
default: defaultNodes
31-
},
32-
offsets: {
33-
header: header.length,
34-
footer: footer.length
35-
}
17+
header,
18+
footer,
19+
default: defaultNodes,
3620
};
3721
}
3822

39-
function resolveTag(tag) {
40-
const externalComponent = !isHtmlTag(tag) && !isTransitionName(tag);
41-
const realRoot = externalComponent ? resolveComponent(tag) : tag;
23+
function getRootInformation(tag) {
24+
const externalComponent = !isHtmlTag(tag) && !isTransition(tag);
4225
return {
43-
tag: realRoot,
4426
externalComponent,
45-
noneFunctional: externalComponent && typeof realRoot !== "function"
27+
tag: externalComponent ? resolveComponent(tag) : tag
4628
};
4729
}
4830

49-
function computeRenderContext({ $slots, tag }) {
50-
const childrenAndNodes = computeChildrenAndNodes($slots);
51-
const tagInformation = resolveTag(tag);
52-
53-
return {
54-
...tagInformation,
55-
...childrenAndNodes
56-
};
31+
function computeComponentStructure({ $slots, tag }) {
32+
const nodes = computeNodes($slots);
33+
const root = getRootInformation(tag);
34+
return new ComponentStructure({ nodes, root });
5735
}
5836

59-
export { computeRenderContext };
37+
export { computeComponentStructure };

src/vuedraggable.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
createSortableOption,
77
getValidSortableEntries
88
} from "./core/componentBuilderHelper";
9-
import { computeRenderContext } from "./core/renderHelper";
9+
import { computeComponentStructure } from "./core/renderHelper";
1010
import { h, defineComponent, nextTick } from "vue";
1111

1212
function computeVmIndex(vNodes, htmlElement) {
@@ -99,17 +99,14 @@ const draggableComponent = defineComponent({
9999

100100
render() {
101101
const { $slots, $attrs, tag, componentData } = this;
102-
const renderContext = computeRenderContext({ $slots, tag });
102+
const componentStructure = computeComponentStructure({
103+
$slots,
104+
tag
105+
}).checkCoherence();
106+
this.componentStructure = componentStructure;
103107
const attributes = getComponentAttributes({ $attrs, componentData });
104108

105-
if (renderContext.noneFunctional && renderContext.transitionMode) {
106-
throw new Error(
107-
`Transition-group inside component is not supported. Please alter tag value or remove transition-group. Current tag value: ${tag}`
108-
);
109-
}
110-
111-
this.renderContext = renderContext;
112-
return h(renderContext.tag, attributes, renderContext.children);
109+
return h(componentStructure.tag, attributes, componentStructure.children);
113110
},
114111

115112
created() {
@@ -145,7 +142,7 @@ const draggableComponent = defineComponent({
145142
rootContainer() {
146143
const {
147144
$el,
148-
renderContext: { transitionMode }
145+
componentStructure: { transitionMode }
149146
} = this;
150147
if (!transitionMode) {
151148
return $el;
@@ -190,7 +187,7 @@ const draggableComponent = defineComponent({
190187

191188
getChildrenNodes() {
192189
const {
193-
renderContext: {
190+
componentStructure: {
194191
noneFunctional,
195192
transitionMode,
196193
nodes: { default: defaultNodes }
@@ -222,7 +219,7 @@ const draggableComponent = defineComponent({
222219
this.visibleIndexes = computeIndexes(
223220
this.getChildrenNodes(),
224221
this.rootContainer.children,
225-
this.renderContext
222+
this.componentStructure
226223
);
227224
});
228225
},

tests/unit/vuedraggable.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ describe("draggable.vue when initialized with list", () => {
198198

199199
it("set noneFunctional to false ", () => {
200200
const {
201-
renderContext: { noneFunctional }
201+
componentStructure: { noneFunctional }
202202
} = vm;
203203
expect(noneFunctional).toBe(false);
204204
});
@@ -342,7 +342,7 @@ describe("draggable.vue when initialized with list", () => {
342342
});
343343
const {
344344
vm: {
345-
renderContext: { noneFunctional }
345+
componentStructure: { noneFunctional }
346346
}
347347
} = wrapper;
348348
expect(noneFunctional).toBe(expectedNoneFunctionalComponentMode);
@@ -971,7 +971,7 @@ describe("draggable.vue when initialized with modelValue", () => {
971971
});
972972

973973
it("transition mode should be false", () => {
974-
expect(vm.renderContext.transitionMode).toBe(false);
974+
expect(vm.componentStructure.transitionMode).toBe(false);
975975
});
976976

977977
describe("when initiating a drag operation", () => {
@@ -1126,7 +1126,7 @@ describe("draggable.vue when initialized with a transition group", () => {
11261126
});
11271127

11281128
it("transition mode should be true", () => {
1129-
expect(vm.renderContext.transitionMode).toBe(true);
1129+
expect(vm.componentStructure.transitionMode).toBe(true);
11301130
});
11311131

11321132
it("renders correctly", () => {

0 commit comments

Comments
 (0)