forked from logaretm/vee-validate
/
modes.ts
60 lines (48 loc) · 1.14 KB
/
modes.ts
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
import { ValidationFlags } from './types';
import { setConfig } from './config';
import { isCallable } from './utils';
interface ModeContext {
errors: string[];
value: any;
flags: ValidationFlags;
}
export interface InteractionSetting {
on?: string[];
debounce?: number;
}
export type InteractionModeFactory = (ctx: ModeContext) => InteractionSetting;
const aggressive: InteractionModeFactory = () => ({
on: ['input', 'blur']
});
const lazy: InteractionModeFactory = () => ({
on: ['change']
});
const eager: InteractionModeFactory = ({ errors }) => {
if (errors.length) {
return {
on: ['input', 'change']
};
}
return {
on: ['change', 'blur']
};
};
const passive: InteractionModeFactory = () => ({
on: []
});
export const modes: { [k: string]: InteractionModeFactory } = {
aggressive,
eager,
passive,
lazy
};
export const setInteractionMode = (mode: string, implementation?: InteractionModeFactory) => {
setConfig({ mode });
if (!implementation) {
return;
}
if (!isCallable(implementation)) {
throw new Error('A mode implementation must be a function');
}
modes[mode] = implementation;
};