-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathuseModel.ts
More file actions
165 lines (152 loc) · 5.02 KB
/
Copy pathuseModel.ts
File metadata and controls
165 lines (152 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { type Ref, type WritableComputedRef, computed, ref, watch } from 'vue';
import { isArray, isEqual as isEqualFunc, isUndefined } from 'lodash-es';
type ModelValuePropKey = 'modelValue';
interface UseNormalModelOptions<
Props extends Record<string, any>,
Key extends keyof Props,
> {
prop?: Key;
isEqual?: boolean;
deep?: boolean;
defaultValue?: Props[Key];
}
export const useNormalModel = <
Props extends Record<string, any>,
Key extends Extract<
keyof Props | ModelValuePropKey,
string
> = ModelValuePropKey,
EventName extends string = string,
>(
props: Props,
emit: (eventName: EventName, ...args: any[]) => void,
config: UseNormalModelOptions<Props, Key> = {},
): [WritableComputedRef<Props[Key]>, (val: Props[Key]) => void] => {
const {
prop = 'modelValue',
deep = false,
isEqual = false,
defaultValue,
} = config;
const usingProp = prop as Key; // 实际使用中 'modelValue' 本就应该在 Key 中
// NOTE: 不可以使用 ref<Type> 的写法,currentValue 会被直接推导成 Props[Key]
const currentValue: Ref<Props[Key]> = ref(
!isUndefined(props[usingProp]) ? props[usingProp] : defaultValue,
);
const pureUpdateCurrentValue = (value: Props[Key]) => {
if (
value === currentValue.value
|| (isEqual && isEqualFunc(value, currentValue.value))
) {
return;
}
currentValue.value = value;
};
const updateCurrentValue = (value: Props[Key]) => {
pureUpdateCurrentValue(value);
// TODO: need a more proper way instead of `as`
emit(`update:${usingProp}` as EventName, currentValue.value);
};
watch(
() => props[usingProp],
(val) => {
if (val === currentValue.value) {
return;
}
currentValue.value = val;
},
{
deep,
},
);
return [
computed({
get() {
return currentValue.value;
},
set(value) {
updateCurrentValue(value);
},
}),
updateCurrentValue,
];
};
type ArrayOrItem<T> = [T] extends [unknown[]] ? T | T[number] : T[] | T;
// type IncludeArray<U> = U extends unknown[] ? U : never;
// type ExtractArray<U> = U extends unknown[] ? U : never;
type GetKeysIsArrayType<Props> = keyof {
[Key in keyof Props as Props[Key] extends unknown[]
? Key
: never]: Props[Key];
};
/**
* TODO: 后续优化
* 使得 useArrayModel 在不传 config 使用 modelValue,且 modelValue 的类型不为数组的情况下,有更友好的类型报错提示
* 目前在上述情况下,modelValue 的类型被推导为 never,保证了部分场景。
*/
/* type UseArrayModelReturn<
Props extends Record<string, any>,
Key extends keyof Props,
> = Props[Key] extends never
? never
: [WritableComputedRef<Props[Key]>, (val: ArrayOrItem<Props[Key]>) => void]; */
export const useArrayModel = <
Props extends Record<string, any>,
Key extends Extract<
Extract<keyof Props | ModelValuePropKey, string>,
GetKeysIsArrayType<Props>
> = Extract<ModelValuePropKey, GetKeysIsArrayType<Props>>,
EventName extends string = string,
>(
props: Props,
emit: (eventName: EventName, ...args: any[]) => void,
config: UseNormalModelOptions<Props, Key> = {},
): [
WritableComputedRef<Props[Key]>,
(val: ArrayOrItem<Props[Key]>) => void,
] => {
const [computedValue, updateCurrentValue] = useNormalModel(props, emit, {
...config,
defaultValue: [] as Props[Key],
});
if (!isArray(computedValue.value)) {
console.warn(
'[useArrayModel] 绑定值类型不匹配, 仅支持数组类型, value:',
props[config?.prop || 'modelValue'],
);
updateCurrentValue([] as Props[Key]);
}
const updateItem = (value: ArrayOrItem<Props[Key]>) => {
if (isArray(value)) {
updateCurrentValue(value as Props[Key]);
return;
}
// 兼容重复赋值为不符合预期数据类型的场景
let val: Props[Key][number][] = [];
if (!isArray(computedValue.value)) {
console.warn(
'[useArrayModel] 绑定值类型不匹配, 仅支持数组类型, value:',
computedValue.value,
);
val = [];
} else {
val = [...computedValue.value];
}
const index = val.indexOf(value);
if (index !== -1) {
val.splice(index, 1);
} else {
val.push(value);
}
updateCurrentValue(val as Props[Key]);
};
return [computedValue, updateItem];
};
/**
* useArrayModel 的返回值
* 用于 modelValue 混杂了 useNormalModel 类型的情况,用于手动指明 useArrayModel 需要的 modelValue 类型
*/
export type UseArrayModelReturn<ModelValue> = [
WritableComputedRef<ModelValue>,
(val: ArrayOrItem<ModelValue>) => void,
];