-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (70 loc) · 2.74 KB
/
index.js
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
import { computed } from 'vue'
/**
* @typedef {import('vue').WritableComputedRef} ModelComputed - Ref containing v-model
*/
const createEmitter = (emit, modelName) => v => emit(`update:${modelName}`, v)
const getModifierName = modelName => modelName === 'modelValue' ? 'modelModifiers' : `${modelName}Modifiers`
const getOnUpdateName = modelName => `onUpdate:${modelName}`
/**
* @type {() => ({})} emptyObjectFactory
*/
const emptyObjectFactory = () => ({})
/**
* Creates a factory of createModel functions
* @arg {object} args
* @arg {string} [args.modelName = 'modelValue'] - the NAME of the model in v-model:NAME
* @arg {function} [args.modifier]
* @returns {function({ props: object, emit?: function }): ModelComputed}
*/
export function createModelFactory({ modelName = 'modelValue', modifier } = {}) {
return ({ props, emit }) => {
const setter = emit ? createEmitter(emit, modelName) : props[getOnUpdateName(modelName)]
const modifierName = getModifierName(modelName)
return computed({
get: () => props[modelName],
set: v => setter(modifier ? modifier(v, props[modifierName]) : v)
})
}
}
/**
* Create a Ref that will get/set the v-model
* @arg {object} args
* @arg {object} args.props - the Reactive props object from setup
* @arg {function} [args.emit] - the emit function from setup
* @arg {string} [args.modelName = 'modelValue'] - the NAME of the model in v-model:NAME
* @arg {function} [args.modifier] - a modifier function to be called on each set call
* @returns {ModelComputed}
*/
export function createModel({ props, emit, modelName = 'modelValue', modifier }) {
return createModelFactory({ modelName, modifier })({ props, emit })
}
/**
* Get the props needed to define a v-model
* @arg {object} args
* @arg {string} [args.modelName = 'modelValue'] - the NAME of the model in v-model:NAME
* @arg {any} [args.modelType = null] - the type the model should receive
* @arg {any} [args.modelDefault] - the default for the model (usually if not set)
* @arg {any} [args.modifierDefault = emptyObjectFactory] - the default for the modelModifiers value
* @returns {object} - props specification
*/
export function modelProps({ modelName = 'modelValue', modelType = null, modelDefault, modifierDefault = emptyObjectFactory } = {}) {
return {
[modelName]: {
type: modelType,
default: modelDefault
},
[getModifierName(modelName)]: {
type: Object,
default: modifierDefault
},
[getOnUpdateName(modelName)]: Function
}
}
/**
* @arg {object} args
* @arg {string} [args.modelName = 'modelValue'] - the NAME of the model in v-model:NAME
* @returns {Array<string>} - emits specification
*/
export function modelEmits({ modelName = 'modelValue' } = {}) {
return [`update:${modelName}`]
}