-
Notifications
You must be signed in to change notification settings - Fork 624
/
Copy pathRadio.vue
152 lines (143 loc) · 3.82 KB
/
Radio.vue
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
<template>
<div :class="ui.wrapper" :data-n-ids="attrs['data-n-ids']">
<div :class="ui.container">
<input
:id="inputId"
v-model="pick"
:name="name"
:required="required"
:value="value"
:disabled="disabled"
type="radio"
:class="inputClass"
v-bind="attrs"
@change="onChange"
>
</div>
<div v-if="label || $slots.label" :class="ui.inner">
<label :for="inputId" :class="ui.label">
<slot name="label" :label="label">{{ label }}</slot>
<span v-if="required" :class="ui.required">*</span>
</label>
<p v-if="help || $slots.help" :class="ui.help">
<slot name="help" :help="help">
{{ help }}
</slot>
</p>
</div>
</div>
</template>
<script lang="ts">
import { computed, defineComponent, inject, toRef } from 'vue'
import type { PropType } from 'vue'
import { twJoin } from 'tailwind-merge'
import { useUI } from '../../composables/useUI'
import { useFormGroup } from '../../composables/useFormGroup'
import { mergeConfig, twMerge } from '../../utils'
import type { DeepPartial, Strategy } from '../../types/index'
// @ts-expect-error
import appConfig from '#build/app.config'
import { radio } from '#ui/ui.config'
import type colors from '#ui-colors'
import { useId } from '#imports'
const config = mergeConfig<typeof radio>(appConfig.ui.strategy, appConfig.ui.radio, radio)
export default defineComponent({
inheritAttrs: false,
props: {
id: {
type: String,
default: null
},
value: {
type: [String, Number, Boolean],
default: null
},
modelValue: {
type: [String, Number, Boolean, Object] as PropType<string | number | boolean | object | null>,
default: null
},
name: {
type: String,
default: null
},
disabled: {
type: Boolean,
default: false
},
help: {
type: String,
default: null
},
label: {
type: String,
default: null
},
required: {
type: Boolean,
default: false
},
color: {
type: String as PropType<typeof colors[number]>,
default: () => config.default.color,
validator(value: string) {
return appConfig.ui.colors.includes(value)
}
},
inputClass: {
type: String,
default: null
},
class: {
type: [String, Object, Array] as PropType<any>,
default: () => ''
},
ui: {
type: Object as PropType<DeepPartial<typeof config> & { strategy?: Strategy }>,
default: () => ({})
}
},
emits: ['update:modelValue', 'change'],
setup(props, { emit }) {
const { ui, attrs } = useUI('radio', toRef(props, 'ui'), config, toRef(props, 'class'))
const inputId = props.id ?? useId()
const radioGroup = inject('radio-group', null)
const { emitFormChange, color, name } = radioGroup ?? useFormGroup(props, config)
const pick = computed({
get() {
return props.modelValue
},
set(value) {
emit('update:modelValue', value)
if (!radioGroup) {
emitFormChange()
}
}
})
function onChange(event: Event) {
emit('change', (event.target as HTMLInputElement).value)
}
const inputClass = computed(() => {
return twMerge(twJoin(
ui.value.base,
ui.value.form,
ui.value.background,
ui.value.border,
color.value && ui.value.ring.replaceAll('{color}', color.value),
color.value && ui.value.color.replaceAll('{color}', color.value)
), props.inputClass)
})
return {
inputId,
// eslint-disable-next-line vue/no-dupe-keys
ui,
attrs,
pick,
// eslint-disable-next-line vue/no-dupe-keys
name,
// eslint-disable-next-line vue/no-dupe-keys
inputClass,
onChange
}
}
})
</script>