/
VStepperItem.tsx
185 lines (162 loc) Β· 4.69 KB
/
VStepperItem.tsx
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
// Styles
import './VStepperItem.sass'
// Components
import { VAvatar } from '@/components/VAvatar/VAvatar'
import { VIcon } from '@/components/VIcon/VIcon'
// Composables
import { makeGroupItemProps, useGroupItem } from '@/composables/group'
// Directives
import { Ripple } from '@/directives/ripple'
// Utilities
import { computed } from 'vue'
import { VStepperSymbol } from './VStepper'
import { genericComponent, propsFactory, useRender } from '@/util'
// Types
import type { PropType } from 'vue'
import type { RippleDirectiveBinding } from '@/directives/ripple'
export type StepperItemSlot = {
canEdit: boolean
hasError: boolean
hasCompleted: boolean
title?: string | number
subtitle?: string | number
step: any
}
export type VStepperItemSlots = {
default: StepperItemSlot
icon: StepperItemSlot
title: StepperItemSlot
subtitle: StepperItemSlot
}
export type ValidationRule = () => string | boolean
export const makeVStepperItemProps = propsFactory({
color: String,
title: String,
subtitle: String,
complete: Boolean,
completeIcon: {
type: String,
default: '$complete',
},
editable: Boolean,
editIcon: {
type: String,
default: '$edit',
},
error: Boolean,
errorIcon: {
type: String,
default: '$error',
},
icon: String,
ripple: {
type: [Boolean, Object] as PropType<RippleDirectiveBinding['value']>,
default: true,
},
rules: {
type: Array as PropType<readonly ValidationRule[]>,
default: () => ([]),
},
...makeGroupItemProps(),
}, 'VStepperItem')
export const VStepperItem = genericComponent<VStepperItemSlots>()({
name: 'VStepperItem',
directives: { Ripple },
props: makeVStepperItemProps(),
emits: {
'group:selected': (val: { value: boolean }) => true,
},
setup (props, { slots }) {
const group = useGroupItem(props, VStepperSymbol, true)
const step = computed(() => group?.value.value ?? props.value)
const isValid = computed(() => props.rules.every(handler => handler() === true))
const canEdit = computed(() => !props.disabled && props.editable)
const hasError = computed(() => props.error || !isValid.value)
const hasCompleted = computed(() => props.complete || (props.rules.length > 0 && isValid.value))
const icon = computed(() => {
if (hasError.value) return props.errorIcon
if (hasCompleted.value) return props.completeIcon
if (props.editable) return props.editIcon
return props.icon
})
const slotProps = computed(() => ({
canEdit: canEdit.value,
hasError: hasError.value,
hasCompleted: hasCompleted.value,
title: props.title,
subtitle: props.subtitle,
step: step.value,
value: props.value,
}))
useRender(() => {
const hasColor = (
!group ||
group.isSelected.value ||
hasCompleted.value ||
canEdit.value
) && (
!hasError.value &&
!props.disabled
)
const hasTitle = !!(props.title != null || slots.title)
const hasSubtitle = !!(props.subtitle != null || slots.subtitle)
function onClick () {
group?.toggle()
}
return (
<button
class={[
'v-stepper-item',
{
'v-stepper-item--complete': hasCompleted.value,
'v-stepper-item--disabled': props.disabled,
'v-stepper-item--error': hasError.value,
},
group?.selectedClass.value,
]}
disabled={ !props.editable }
v-ripple={[
props.ripple && props.editable,
null,
null,
]}
onClick={ onClick }
>
<VAvatar
key="stepper-avatar"
class="v-stepper-item__avatar"
color={ hasColor ? props.color : undefined }
size={ 24 }
>
{ slots.icon?.(slotProps.value) ?? (
icon.value ? (
<VIcon icon={ icon.value }></VIcon>
) : step.value
)}
</VAvatar>
<div class="v-stepper-item__content">
{ hasTitle && (
<div
key="title"
class="v-stepper-item__title"
>
{ slots.title?.(slotProps.value) ?? props.title }
</div>
)}
{ hasSubtitle && (
<div
key="subtitle"
class="v-stepper-item__subtitle"
>
{ slots.subtitle?.(slotProps.value) ?? props.subtitle }
</div>
)}
{ slots.default?.(slotProps.value) }
</div>
</button>
)
})
return {}
},
})
export type VStepperItem = InstanceType<typeof VStepperItem>