mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-24 02:16:12 +08:00
feat: add useParentField
This commit is contained in:
parent
216417eae6
commit
0b147c5872
17
src/composition/use-parent-field.ts
Normal file
17
src/composition/use-parent-field.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import { watch, inject, WatchSource, getCurrentInstance } from 'vue';
|
||||||
|
import { FIELD_KEY } from '../field';
|
||||||
|
|
||||||
|
export function useParentField(watchSource: WatchSource) {
|
||||||
|
const field = inject(FIELD_KEY) as any;
|
||||||
|
|
||||||
|
if (field && !field.children) {
|
||||||
|
field.children = getCurrentInstance()!.proxy;
|
||||||
|
|
||||||
|
watch(watchSource, () => {
|
||||||
|
if (field) {
|
||||||
|
field.resetValidation();
|
||||||
|
field.validateWithTrigger('onChange');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -18,6 +18,8 @@ import { cellProps } from '../cell/shared';
|
|||||||
|
|
||||||
const [createComponent, bem] = createNamespace('field');
|
const [createComponent, bem] = createNamespace('field');
|
||||||
|
|
||||||
|
export const FIELD_KEY = 'vanField';
|
||||||
|
|
||||||
export default createComponent({
|
export default createComponent({
|
||||||
provide() {
|
provide() {
|
||||||
return {
|
return {
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
|
import { FIELD_KEY } from '../field';
|
||||||
|
|
||||||
export const FieldMixin = {
|
export const FieldMixin = {
|
||||||
inject: {
|
inject: {
|
||||||
vanField: {
|
vanField: {
|
||||||
from: 'vanField',
|
from: FIELD_KEY,
|
||||||
default: null,
|
default: null,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1,19 +1,17 @@
|
|||||||
import { ref, computed } from 'vue';
|
import { ref, computed } from 'vue';
|
||||||
|
|
||||||
|
// Utils
|
||||||
import { createNamespace, addUnit, getSizeStyle } from '../utils';
|
import { createNamespace, addUnit, getSizeStyle } from '../utils';
|
||||||
import { preventDefault } from '../utils/dom/event';
|
import { preventDefault } from '../utils/dom/event';
|
||||||
|
|
||||||
// Mixins
|
|
||||||
import { FieldMixin } from '../mixins/field';
|
|
||||||
|
|
||||||
// Composition
|
// Composition
|
||||||
import { useRect } from '../composition/use-rect';
|
import { useRect } from '../composition/use-rect';
|
||||||
import { useTouch } from '../composition/use-touch';
|
import { useTouch } from '../composition/use-touch';
|
||||||
|
import { useParentField } from '../composition/use-parent-field';
|
||||||
|
|
||||||
const [createComponent, bem] = createNamespace('slider');
|
const [createComponent, bem] = createNamespace('slider');
|
||||||
|
|
||||||
export default createComponent({
|
export default createComponent({
|
||||||
mixins: [FieldMixin],
|
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
disabled: Boolean,
|
disabled: Boolean,
|
||||||
vertical: Boolean,
|
vertical: Boolean,
|
||||||
@ -174,6 +172,7 @@ export default createComponent({
|
|||||||
|
|
||||||
// format initial value
|
// format initial value
|
||||||
updateValue(props.modelValue);
|
updateValue(props.modelValue);
|
||||||
|
useParentField(() => props.modelValue);
|
||||||
|
|
||||||
return () => (
|
return () => (
|
||||||
<div
|
<div
|
||||||
|
Loading…
x
Reference in New Issue
Block a user