mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat(Form): binding checkbox
This commit is contained in:
parent
f385c94040
commit
1fe96f3edc
@ -15,6 +15,12 @@ const [createComponent, bem] = createNamespace('field');
|
||||
export default createComponent({
|
||||
inheritAttrs: false,
|
||||
|
||||
provide() {
|
||||
return {
|
||||
vanField: this,
|
||||
};
|
||||
},
|
||||
|
||||
inject: {
|
||||
vanForm: {
|
||||
default: null,
|
||||
@ -111,6 +117,10 @@ export default createComponent({
|
||||
return { width: addUnit(labelWidth) };
|
||||
}
|
||||
},
|
||||
|
||||
formValue() {
|
||||
return this.children ? this.children.value : this.value;
|
||||
},
|
||||
},
|
||||
|
||||
methods: {
|
||||
@ -135,7 +145,7 @@ export default createComponent({
|
||||
}
|
||||
|
||||
return !this.rules.some(rule => {
|
||||
if (rule.required && !this.value) {
|
||||
if (rule.required && !this.formValue) {
|
||||
this.validateMessage = rule.message;
|
||||
return true;
|
||||
}
|
||||
|
@ -17,7 +17,11 @@
|
||||
:rules="[{ required: true, message: 'password is required' }]"
|
||||
placeholder="password"
|
||||
/>
|
||||
<van-field name="agree" label="agree">
|
||||
<van-field
|
||||
name="agree"
|
||||
label="agree"
|
||||
:rules="[{ required: true, message: 'need agree' }]"
|
||||
>
|
||||
<van-checkbox v-model="agree" slot="input" shape="square" />
|
||||
</van-field>
|
||||
<van-button type="primary">submit</van-button>
|
||||
|
@ -24,7 +24,7 @@ export default createComponent({
|
||||
|
||||
getFormData() {
|
||||
return this.fields.reduce((form, field) => {
|
||||
form[field.name] = field.value;
|
||||
form[field.name] = field.formValue;
|
||||
return form;
|
||||
}, {});
|
||||
},
|
||||
|
@ -2,11 +2,12 @@
|
||||
* Common part of Checkbox & Radio
|
||||
*/
|
||||
import Icon from '../icon';
|
||||
import { FieldMixin } from './field';
|
||||
import { ChildrenMixin } from './relation';
|
||||
import { addUnit } from '../utils';
|
||||
|
||||
export const CheckboxMixin = ({ parent, bem, role }) => ({
|
||||
mixins: [ChildrenMixin(parent)],
|
||||
mixins: [ChildrenMixin(parent), FieldMixin],
|
||||
|
||||
props: {
|
||||
name: null,
|
||||
|
13
src/mixins/field.js
Normal file
13
src/mixins/field.js
Normal file
@ -0,0 +1,13 @@
|
||||
export const FieldMixin = {
|
||||
inject: {
|
||||
vanField: {
|
||||
default: null,
|
||||
},
|
||||
},
|
||||
|
||||
created() {
|
||||
if (this.vanField) {
|
||||
this.vanField.children = this;
|
||||
}
|
||||
},
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user