13
13
14
14
<script >
15
15
import { shallowEqualObjects , arrayify , has } from ' ./libs/utils'
16
- import Registry from ' ./libs/registry'
16
+ import useRegistry , { useRegistryComputed , useRegistryMethods , useRegistryProviders } from ' ./libs/registry'
17
17
import FormSubmission from ' ./FormSubmission'
18
18
19
19
export default {
20
20
provide () {
21
21
return {
22
- formulateFormSetter: this .setFieldValue ,
23
- formulateFormRegister: this .register ,
24
- getFormValues: this .getFormValues ,
22
+ ... useRegistryProviders (this ),
25
23
observeErrors: this .addErrorObserver ,
26
24
removeErrorObserver: this .removeErrorObserver ,
27
25
formulateFieldValidation: this .formulateFieldValidation
@@ -56,52 +54,20 @@ export default {
56
54
},
57
55
data () {
58
56
return {
59
- registry: new Registry (),
60
- internalFormModelProxy: {},
57
+ ... useRegistry (this ),
61
58
formShouldShowErrors: false ,
62
59
errorObservers: [],
63
60
namedErrors: [],
64
61
namedFieldErrors: {}
65
62
}
66
63
},
67
64
computed: {
68
- /**
69
- * @todo in 2.3.0 this will expand and be extracted to a separate module to
70
- * support better scoped slot interoperability.
71
- */
65
+ ... useRegistryComputed (),
72
66
formContext () {
73
67
return {
74
68
errors: this .mergedFormErrors
75
69
}
76
70
},
77
- hasInitialValue () {
78
- return (
79
- (this .formulateValue && typeof this .formulateValue === ' object' ) ||
80
- (this .values && typeof this .values === ' object' )
81
- )
82
- },
83
- isVmodeled () {
84
- return !! (this .$options .propsData .hasOwnProperty (' formulateValue' ) &&
85
- this ._events &&
86
- Array .isArray (this ._events .input ) &&
87
- this ._events .input .length )
88
- },
89
- initialValues () {
90
- if (
91
- has (this .$options .propsData , ' formulateValue' ) &&
92
- typeof this .formulateValue === ' object'
93
- ) {
94
- // If there is a v-model on the form, use those values as first priority
95
- return Object .assign ({}, this .formulateValue ) // @todo - use a deep clone to detach reference types
96
- } else if (
97
- has (this .$options .propsData , ' values' ) &&
98
- typeof this .values === ' object'
99
- ) {
100
- // If there are values, use them as secondary priority
101
- return Object .assign ({}, this .values )
102
- }
103
- return {}
104
- },
105
71
classes () {
106
72
const classes = { ' formulate-form' : true }
107
73
if (this .name ) {
@@ -137,11 +103,11 @@ export default {
137
103
) {
138
104
for (const field in newValue) {
139
105
if (this .registry .has (field) &&
140
- ! shallowEqualObjects (newValue[field], this .internalFormModelProxy [field]) &&
106
+ ! shallowEqualObjects (newValue[field], this .proxy [field]) &&
141
107
! shallowEqualObjects (newValue[field], this .registry .get (field).internalModelProxy [field])
142
108
) {
143
109
this .setFieldValue (field, newValue[field])
144
- this .registry [ field] .context .model = newValue[field]
110
+ this .registry . get ( field) .context .model = newValue[field]
145
111
}
146
112
}
147
113
}
@@ -170,11 +136,7 @@ export default {
170
136
this .$formulate .deregister (this )
171
137
},
172
138
methods: {
173
- applyInitialValues () {
174
- if (this .hasInitialValue ) {
175
- this .internalFormModelProxy = this .initialValues
176
- }
177
- },
139
+ ... useRegistryMethods (),
178
140
applyErrors ({ formErrors, inputErrors }) {
179
141
// given an object of errors, apply them to this form
180
142
this .namedErrors = formErrors
@@ -193,36 +155,6 @@ export default {
193
155
removeErrorObserver (observer ) {
194
156
this .errorObservers = this .errorObservers .filter (obs => obs .callback !== observer)
195
157
},
196
- setFieldValue (field , value ) {
197
- Object .assign (this .internalFormModelProxy , { [field]: value })
198
- this .$emit (' input' , Object .assign ({}, this .internalFormModelProxy ))
199
- },
200
- register (field , component ) {
201
- // Don't re-register fields... @todo come up with another way of handling this that doesn't break multi option
202
- if (this .registry .has (field)) {
203
- return false
204
- }
205
- this .registry .add (field, component)
206
- const hasVModelValue = has (component .$options .propsData , ' formulateValue' )
207
- const hasValue = has (component .$options .propsData , ' value' )
208
- if (
209
- ! component .context .isSubField () &&
210
- ! hasVModelValue &&
211
- this .hasInitialValue &&
212
- this .initialValues [field]
213
- ) {
214
- // In the case that the form is carrying an initial value and the
215
- // element is not, set it directly.
216
- component .context .model = this .initialValues [field]
217
- } else if (
218
- (hasVModelValue || hasValue) &&
219
- ! shallowEqualObjects (component .internalModelProxy , this .initialValues [field])
220
- ) {
221
- // In this case, the field is v-modeled or has an initial value and the
222
- // form has no value or a different value, so use the field value
223
- this .setFieldValue (field, component .internalModelProxy )
224
- }
225
- },
226
158
registerErrorComponent (component ) {
227
159
if (! this .errorComponents .includes (component)) {
228
160
this .errorComponents .push (component)
@@ -248,9 +180,6 @@ export default {
248
180
input .formShouldShowErrors = true
249
181
})
250
182
},
251
- getFormValues () {
252
- return this .internalFormModelProxy
253
- },
254
183
formulateFieldValidation (errorObject ) {
255
184
this .$emit (' validation' , errorObject)
256
185
},
0 commit comments