-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathindex.ts
171 lines (161 loc) · 4.23 KB
/
index.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
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
import { connect, mapProps, h, mapReadPretty } from '@formily/vue'
import { defineComponent, PropType } from '@vue/composition-api'
import {
composeExport,
transformComponent,
resolveComponent,
SlotTypes,
} from '../__builtins__/shared'
import type {
Checkbox as _ElCheckboxProps,
CheckboxGroup as ElCheckboxGroupProps,
} from 'element-ui'
import {
Checkbox as ElCheckbox,
CheckboxGroup as ElCheckboxGroup,
CheckboxButton as ElCheckboxButton,
} from 'element-ui'
import { PreviewText } from '../preview-text'
type ElCheckboxProps = Omit<_ElCheckboxProps, 'value'> & {
value: ElCheckboxProps['label']
}
export interface CheckboxProps extends ElCheckboxProps {
option: Omit<_ElCheckboxProps, 'value'> & {
value: ElCheckboxProps['label']
label: SlotTypes
}
}
const CheckboxOption = defineComponent<CheckboxProps>({
name: 'Checkbox',
inheritAttrs: false,
props: {
option: {
type: Object,
default: null,
},
},
setup(curtomProps, { attrs, slots, listeners }) {
return () => {
const props = attrs as unknown as CheckboxProps
const option = curtomProps?.option
if (option) {
const children = {
default: () => [
resolveComponent(slots.default ?? option.label, { option }),
],
}
const newProps = {} as Partial<ElCheckboxProps>
Object.assign(newProps, option)
newProps.label = option.value
delete newProps.value
return h(
attrs.optionType === 'button' ? ElCheckboxButton : ElCheckbox,
{
attrs: {
...newProps,
},
},
children
)
}
return h(
ElCheckbox,
{
attrs: {
...props,
},
on: listeners,
},
slots
)
}
},
})
export type CheckboxGroupProps = ElCheckboxGroupProps & {
value: any[]
options?: Array<CheckboxProps | string>
optionType: 'default' | 'button'
}
const TransformElCheckboxGroup = transformComponent(ElCheckboxGroup, {
change: 'input',
})
const CheckboxGroupOption = defineComponent<CheckboxGroupProps>({
name: 'CheckboxGroup',
props: {
options: {
type: Array,
default: () => [],
},
optionType: {
type: String as PropType<CheckboxGroupProps['optionType']>,
default: 'default',
},
},
setup(customProps, { attrs, slots, listeners }) {
return () => {
const options = customProps.options || []
const children =
options.length !== 0
? {
default: () =>
options.map((option) => {
if (typeof option === 'string') {
return h(
Checkbox,
{
props: {
option: {
label: option,
value: option,
},
},
attrs: {
optionType: customProps.optionType,
},
},
slots?.option
? { default: () => slots.option({ option }) }
: {}
)
} else {
return h(
Checkbox,
{
props: {
option,
},
attrs: {
optionType: customProps.optionType,
},
},
slots?.option
? { default: () => slots.option({ option }) }
: {}
)
}
}),
}
: slots
return h(
TransformElCheckboxGroup,
{
attrs: {
...attrs,
},
on: listeners,
},
children
)
}
},
})
const CheckboxGroup = connect(
CheckboxGroupOption,
mapProps({ dataSource: 'options' }),
mapReadPretty(PreviewText.Select, {
multiple: true,
})
)
export const Checkbox = composeExport(connect(CheckboxOption), {
Group: CheckboxGroup,
})