This commit is contained in:
2022-07-21 03:28:35 +00:00
parent d7c883d6df
commit 51b34b0e1d
30103 changed files with 4152204 additions and 23 deletions
+134
View File
@@ -0,0 +1,134 @@
// Mixins
import mixins from '../../util/mixins';
import BindsAttrs from '../../mixins/binds-attrs';
import { provide as RegistrableProvide } from '../../mixins/registrable';
/* @vue/component */
export default mixins(BindsAttrs, RegistrableProvide('form')
/* @vue/component */
).extend({
name: 'v-form',
provide() {
return {
form: this
};
},
inheritAttrs: false,
props: {
disabled: Boolean,
lazyValidation: Boolean,
readonly: Boolean,
value: Boolean
},
data: () => ({
inputs: [],
watchers: [],
errorBag: {}
}),
watch: {
errorBag: {
handler(val) {
const errors = Object.values(val).includes(true);
this.$emit('input', !errors);
},
deep: true,
immediate: true
}
},
methods: {
watchInput(input) {
const watcher = input => {
return input.$watch('hasError', val => {
this.$set(this.errorBag, input._uid, val);
}, {
immediate: true
});
};
const watchers = {
_uid: input._uid,
valid: () => {},
shouldValidate: () => {}
};
if (this.lazyValidation) {
// Only start watching inputs if we need to
watchers.shouldValidate = input.$watch('shouldValidate', val => {
if (!val) return; // Only watch if we're not already doing it
if (this.errorBag.hasOwnProperty(input._uid)) return;
watchers.valid = watcher(input);
});
} else {
watchers.valid = watcher(input);
}
return watchers;
},
/** @public */
validate() {
return this.inputs.filter(input => !input.validate(true)).length === 0;
},
/** @public */
reset() {
this.inputs.forEach(input => input.reset());
this.resetErrorBag();
},
resetErrorBag() {
if (this.lazyValidation) {
// Account for timeout in validatable
setTimeout(() => {
this.errorBag = {};
}, 0);
}
},
/** @public */
resetValidation() {
this.inputs.forEach(input => input.resetValidation());
this.resetErrorBag();
},
register(input) {
this.inputs.push(input);
this.watchers.push(this.watchInput(input));
},
unregister(input) {
const found = this.inputs.find(i => i._uid === input._uid);
if (!found) return;
const unwatch = this.watchers.find(i => i._uid === found._uid);
if (unwatch) {
unwatch.valid();
unwatch.shouldValidate();
}
this.watchers = this.watchers.filter(i => i._uid !== found._uid);
this.inputs = this.inputs.filter(i => i._uid !== found._uid);
this.$delete(this.errorBag, found._uid);
}
},
render(h) {
return h('form', {
staticClass: 'v-form',
attrs: {
novalidate: true,
...this.attrs$
},
on: {
submit: e => this.$emit('submit', e)
}
}, this.$slots.default);
}
});
//# sourceMappingURL=VForm.js.map
File diff suppressed because one or more lines are too long
+4
View File
@@ -0,0 +1,4 @@
import VForm from './VForm';
export { VForm };
export default VForm;
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/components/VForm/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAP,MAAkB,SAAlB;AAEA,SAAS,KAAT;AACA,eAAe,KAAf","sourcesContent":["import VForm from './VForm'\n\nexport { VForm }\nexport default VForm\n"],"sourceRoot":"","file":"index.js"}