Skip to content

Commit ac1ebb0

Browse files
committed
[2.5.0] Added FieldArray control
1 parent 3cf38e6 commit ac1ebb0

File tree

15 files changed

+386
-17
lines changed

15 files changed

+386
-17
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 2.5.0
2+
3+
## Added
4+
5+
- [`FieldArray`](/VueForm/components/ConnectedFieldArray.js) control
6+
- [`Array Field Form`](https://detools.github.io/vue-form/#/array-field-form) to explain how `FieldArray` works
7+
18
## 2.4.2
29

310
### Updated

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ See demo at [https://detools.github.io/vue-form](https://detools.github.io/vue-f
7474
- [Slider](/VueForm/components/ConnectedSlider.js)
7575
- [Switch](/VueForm/components/ConnectedSwitch.js)
7676
- [TimePicker](/VueForm/components/ConnectedTimePicker.js)
77+
- [FieldArray](/VueForm/components/ConnectedFieldArray.js)
7778
- [Form](/VueForm/components/Form/Form.vue)
7879

7980
## Changelog
8081

82+
- [2.5.0](/CHANGELOG.md#250)
8183
- [2.4.2](/CHANGELOG.md#242)
8284
- [2.4.1](/CHANGELOG.md#241)
8385
- [2.4.0](/CHANGELOG.md#240)
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
import without from 'lodash/without'
2+
import last from 'lodash/last'
3+
import ConnectedControlMixin from '../mixins/ConnectedControl'
4+
5+
export default {
6+
props: {
7+
name: {
8+
type: String,
9+
required: true,
10+
},
11+
12+
value: {
13+
type: Array,
14+
default: () => [],
15+
},
16+
17+
validators: Array,
18+
asyncValidators: Array,
19+
20+
renderField: {
21+
type: Function,
22+
required: true,
23+
},
24+
},
25+
26+
mixins: [ConnectedControlMixin],
27+
28+
computed: {
29+
controlValue() {
30+
return this.state[0]
31+
},
32+
33+
setValue() {
34+
return this.state[1]
35+
},
36+
37+
fields() {
38+
return {
39+
forEach: this.forEach,
40+
get: this.get,
41+
getAll: this.getAll,
42+
insert: this.insert,
43+
length: this.length,
44+
map: this.map,
45+
move: this.move,
46+
pop: this.pop,
47+
push: this.push,
48+
remove: this.remove,
49+
removeAll: this.removeAll,
50+
shift: this.shift,
51+
swap: this.swap,
52+
unshift: this.unshift,
53+
}
54+
},
55+
},
56+
57+
methods: {
58+
forEach(callback) {
59+
return this.controlValue.forEach(callback)
60+
},
61+
62+
get(index) {
63+
return this.controlValue[index]
64+
},
65+
66+
getAll() {
67+
return this.controlValue
68+
},
69+
70+
insert(index, value) {
71+
const nextArray = [
72+
...this.controlValue.slice(0, index),
73+
value,
74+
...this.controlValue.slice(index + 1),
75+
]
76+
77+
this.setValue(nextArray)
78+
79+
return nextArray
80+
},
81+
82+
length() {
83+
return this.controlValue.length
84+
},
85+
86+
map(callback) {
87+
return this.controlValue.map(callback)
88+
},
89+
90+
move(fromIndex, toIndex) {
91+
const elementToMove = this.controlValue[fromIndex]
92+
const arrayWithout = without(this.controlValue, elementToMove)
93+
const nextArray = [
94+
...arrayWithout.slice(0, toIndex),
95+
elementToMove,
96+
...arrayWithout.slice(toIndex + 1),
97+
]
98+
99+
this.setValue(nextArray)
100+
101+
return nextArray
102+
},
103+
104+
pop() {
105+
const elementToRemove = last(this.controlValue)
106+
const nextArray = without(this.controlValue, elementToRemove)
107+
108+
this.setValue(nextArray)
109+
110+
return nextArray
111+
},
112+
113+
push(value) {
114+
const nextArray = this.controlValue.concat(value)
115+
116+
this.setValue(nextArray)
117+
118+
return nextArray
119+
},
120+
121+
remove(index) {
122+
const nextArray = without(this.controlValue, this.controlValue[index])
123+
124+
this.setValue(nextArray)
125+
126+
return nextArray
127+
},
128+
129+
removeAll() {
130+
const nextArray = []
131+
132+
this.setValue(nextArray)
133+
134+
return nextArray
135+
},
136+
137+
shift() {
138+
const nextArray = without(this.controlValue, this.controlValue[0])
139+
140+
this.setValue(nextArray)
141+
142+
return nextArray
143+
},
144+
145+
swap(firstIndex, secondIndex) {
146+
const [smallerIndex, biggerIndex] = [firstIndex, secondIndex].sort()
147+
148+
const firstElement = this.controlValue[smallerIndex]
149+
const secondElement = this.controlValue[biggerIndex]
150+
151+
const nextArray = [
152+
...this.controlValue.slice(0, smallerIndex),
153+
secondElement,
154+
...this.controlValue.slice(smallerIndex + 1, biggerIndex),
155+
firstElement,
156+
...this.controlValue.slice(biggerIndex + 1),
157+
]
158+
159+
this.setValue(nextArray)
160+
161+
return nextArray
162+
},
163+
164+
unshift(value) {
165+
const nextArray = [value].concat(this.controlValue)
166+
167+
this.setValue(nextArray)
168+
169+
return nextArray
170+
},
171+
172+
renderComponent(value) {
173+
return this.renderField({ data: value, fields: this.fields })
174+
},
175+
},
176+
}

VueForm/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Switch from './components/ConnectedSwitch'
1010
import Slider from './components/ConnectedSlider'
1111
import TimePicker from './components/ConnectedTimePicker'
1212
import DatePicker from './components/ConnectedDatePicker'
13+
import ArrayField from './components/ConnectedArrayField'
1314
import FormItem from './components/ConnectedFormItem'
1415

1516
import Notification from './components/Notification'
@@ -29,6 +30,7 @@ export {
2930
TimePicker,
3031
DatePicker,
3132
FormItem,
33+
ArrayField,
3234
Notification,
3335
validators,
3436
}

VueForm/mixins/ConnectedControl.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ const ConnectedControlMixin = {
1414
this.cleanFormValue()
1515
},
1616

17+
computed: {
18+
state() {
19+
return this.useState()
20+
},
21+
},
22+
1723
methods: {
1824
handleFieldBlur(...args) {
1925
this.setTouched()
@@ -29,7 +35,7 @@ const ConnectedControlMixin = {
2935
},
3036

3137
render() {
32-
const [value, setValue, error] = this.useState()
38+
const [value, setValue, error] = this.state
3339
const label = this.omitFormItemLabel ? undefined : this.label || this.name
3440

3541
if (this.formItem) {

docs/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
width: 200px;
3838
}
3939

40+
.el-table:before {
41+
content: none;
42+
}
43+
4044
.github-corner:hover .octo-arm{
4145
animation: octocat-wave 560ms ease-in-out;
4246
}

docs/main.css

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11

2-
.root-container[data-v-a969b32e] {
2+
.root-container[data-v-434ec9e2] {
33
min-height: 100%;
44
}
5-
.title[data-v-a969b32e] {
5+
.title[data-v-434ec9e2] {
66
font-size: 40px;
77
margin: 10px 0 0;
88
font-weight: bold;
99
}
10-
.main-container[data-v-a969b32e] {
10+
.main-container[data-v-434ec9e2] {
1111
display: flex;
1212
flex-direction: row;
1313
justify-content: space-between;
1414
align-items: stretch;
1515
}
16-
.aside[data-v-a969b32e] {
16+
.aside[data-v-434ec9e2] {
1717
border-top: 1px solid rgba(0, 0, 0, 0.1);
1818
}
19-
.main[data-v-a969b32e] {
19+
.main[data-v-434ec9e2] {
2020
padding: 10px 40px 40px;
2121
border-top: 1px solid rgba(0, 0, 0, 0.1);
2222
border-left: 1px solid rgba(0, 0, 0, 0.1);
2323
}
24-
.link[data-v-a969b32e] {
24+
.link[data-v-434ec9e2] {
2525
display: block;
2626
line-height: 40px;
2727
padding-left: 30px;
2828
position: relative;
2929
color: #0a0a0a;
3030
}
31-
.link_active[data-v-a969b32e]:before {
31+
.link_active[data-v-434ec9e2]:before {
3232
content: '\261B';
3333
position: absolute;
3434
display: block;

docs/vendors~main.bundle.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/public/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
.values {
3636
width: 200px;
3737
}
38+
39+
.el-table:before {
40+
content: none;
41+
}
3842
</style>
3943
</head>
4044
<body>

0 commit comments

Comments
 (0)