/
QRadio.js
198 lines (160 loc) · 5.18 KB
/
QRadio.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { h, ref, computed, getCurrentInstance, toRaw } from 'vue'
import QIcon from '../icon/QIcon.js'
import useDark, { useDarkProps } from '../../composables/private/use-dark.js'
import useSize, { useSizeProps } from '../../composables/private/use-size.js'
import useRefocusTarget from '../../composables/private/use-refocus-target.js'
import { useFormProps, useFormInject } from '../../composables/private/use-form.js'
import { createComponent } from '../../utils/private/create.js'
import optionSizes from '../../utils/private/option-sizes.js'
import { stopAndPrevent } from '../../utils/event.js'
import { hSlot, hMergeSlot } from '../../utils/private/render.js'
const svg = h('svg', {
key: 'svg',
class: 'q-radio__bg absolute non-selectable',
viewBox: '0 0 24 24'
}, [
h('path', {
d: 'M12,22a10,10 0 0 1 -10,-10a10,10 0 0 1 10,-10a10,10 0 0 1 10,10a10,10 0 0 1 -10,10m0,-22a12,12 0 0 0 -12,12a12,12 0 0 0 12,12a12,12 0 0 0 12,-12a12,12 0 0 0 -12,-12'
}),
h('path', {
class: 'q-radio__check',
d: 'M12,6a6,6 0 0 0 -6,6a6,6 0 0 0 6,6a6,6 0 0 0 6,-6a6,6 0 0 0 -6,-6'
})
])
export default createComponent({
name: 'QRadio',
props: {
...useDarkProps,
...useSizeProps,
...useFormProps,
modelValue: { required: true },
val: { required: true },
label: String,
leftLabel: Boolean,
checkedIcon: String,
uncheckedIcon: String,
color: String,
keepColor: Boolean,
dense: Boolean,
disable: Boolean,
tabindex: [ String, Number ]
},
emits: [ 'update:modelValue' ],
setup (props, { slots, emit }) {
const { proxy } = getCurrentInstance()
const isDark = useDark(props, proxy.$q)
const sizeStyle = useSize(props, optionSizes)
const rootRef = ref(null)
const { refocusTargetEl, refocusTarget } = useRefocusTarget(props, rootRef)
const isTrue = computed(() => toRaw(props.modelValue) === toRaw(props.val))
const classes = computed(() =>
'q-radio cursor-pointer no-outline row inline no-wrap items-center'
+ (props.disable === true ? ' disabled' : '')
+ (isDark.value === true ? ' q-radio--dark' : '')
+ (props.dense === true ? ' q-radio--dense' : '')
+ (props.leftLabel === true ? ' reverse' : '')
)
const innerClass = computed(() => {
const color = props.color !== void 0 && (
props.keepColor === true
|| isTrue.value === true
)
? ` text-${ props.color }`
: ''
return 'q-radio__inner relative-position '
+ `q-radio__inner--${ isTrue.value === true ? 'truthy' : 'falsy' }${ color }`
})
const icon = computed(() =>
(isTrue.value === true
? props.checkedIcon
: props.uncheckedIcon
) || null
)
const tabindex = computed(() => (
props.disable === true ? -1 : props.tabindex || 0
))
const formAttrs = computed(() => {
const prop = { type: 'radio' }
props.name !== void 0 && Object.assign(prop, {
// see https://vuejs.org/guide/extras/render-function.html#creating-vnodes (.prop)
'.checked': isTrue.value === true,
'^checked': isTrue.value === true ? 'checked' : void 0,
name: props.name,
value: props.val
})
return prop
})
const injectFormInput = useFormInject(formAttrs)
function onClick (e) {
if (e !== void 0) {
stopAndPrevent(e)
refocusTarget(e)
}
if (props.disable !== true && isTrue.value !== true) {
emit('update:modelValue', props.val, e)
}
}
function onKeydown (e) {
if (e.keyCode === 13 || e.keyCode === 32) {
stopAndPrevent(e)
}
}
function onKeyup (e) {
if (e.keyCode === 13 || e.keyCode === 32) {
onClick(e)
}
}
// expose public methods
Object.assign(proxy, { set: onClick })
return () => {
const content = icon.value !== null
? [
h('div', {
key: 'icon',
class: 'q-radio__icon-container absolute-full flex flex-center no-wrap'
}, [
h(QIcon, {
class: 'q-radio__icon',
name: icon.value
})
])
]
: [ svg ]
props.disable !== true && injectFormInput(
content,
'unshift',
' q-radio__native q-ma-none q-pa-none'
)
const child = [
h('div', {
class: innerClass.value,
style: sizeStyle.value,
'aria-hidden': 'true'
}, content)
]
if (refocusTargetEl.value !== null) {
child.push(refocusTargetEl.value)
}
const label = props.label !== void 0
? hMergeSlot(slots.default, [ props.label ])
: hSlot(slots.default)
label !== void 0 && child.push(
h('div', {
class: 'q-radio__label q-anchor--skip'
}, label)
)
return h('div', {
ref: rootRef,
class: classes.value,
tabindex: tabindex.value,
role: 'radio',
'aria-label': props.label,
'aria-checked': isTrue.value === true ? 'true' : 'false',
'aria-disabled': props.disable === true ? 'true' : void 0,
onClick,
onKeydown,
onKeyup
}, child)
}
}
})