Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(element): radio/checkbox add optionType prop #2114

Merged
merged 2 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/element/docs/demos/guide/checkbox/template.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
{ label: '选项2', value: 2 },
]"
:decorator="[FormItem]"
:component="[Checkbox.Group]"
/>
:component="[Checkbox.Group, { optionType: 'button' }]"
>
<template v-slot:option="{ option }">
<div>{{ option.label }}</div>
</template>
</ArrayField>
<Submit @submit="onSubmit">提交</Submit>
</Form>
</template>
Expand Down
6 changes: 3 additions & 3 deletions packages/element/docs/demos/guide/form-item/size.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
name="size"
title="Radio.Group"
x-decorator="FormItem"
x-component="RadioGroup"
x-component="Radio.Group"
:enum="[
{ value: 'small', label: 'Small' },
{ value: 'default', label: 'Default' },
Expand Down Expand Up @@ -106,7 +106,7 @@ import {
DatePicker,
Switch,
InputNumber,
RadioGroup,
Radio,
} from '@formily/element'

const Div = {
Expand Down Expand Up @@ -139,7 +139,7 @@ const fields = createSchemaField({
DatePicker,
Switch,
InputNumber,
RadioGroup,
Radio,
Div,
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const SchemaField = createSchemaField({
Input,
},
})
const formStep = FormStep.createFormStep
const formStep = FormStep.createFormStep()

export default {
components: {
Expand Down
3 changes: 3 additions & 0 deletions packages/element/docs/demos/guide/radio/json-schema.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export default {
],
'x-decorator': 'FormItem',
'x-component': 'Radio.Group',
'x-component-props': {
optionType: 'button',
},
},
},
}
Expand Down
4 changes: 1 addition & 3 deletions packages/element/docs/demos/guide/space/json-schema.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
FormLayout,
FormItem,
Input,
TextArea,
Submit,
Space,
} from '@formily/element'
Expand All @@ -27,7 +26,6 @@ const { SchemaField } = createSchemaField({
FormLayout,
FormItem,
Input,
TextArea,
Space,
},
})
Expand Down Expand Up @@ -112,7 +110,7 @@ export default {
type: 'string',
title: '文本框',
'x-decorator': 'FormItem',
'x-component': 'TextArea',
'x-component': 'Input.TextArea',
'x-component-props': {
style: {
width: 400,
Expand Down
7 changes: 4 additions & 3 deletions packages/element/docs/guide/checkbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

### 扩展属性

| 属性名 | 类型 | 描述 | 默认值 |
| ------- | ------------------------------------------------------------------------------------------ | ---- | ------ |
| options | [CheckboxProps](https://element.eleme.io/#/zh-CN/component/checkbox#checkbox-attributes)[] | 选项 | [] |
| 属性名 | 类型 | 描述 | 默认值 |
| ---------- | ------------------------------------------------------------------------------------------ | -------- | ------- |
| options | [CheckboxProps](https://element.eleme.io/#/zh-CN/component/checkbox#checkbox-attributes)[] | 选项 | [] |
| optionType | default/button | 样式类型 | default |
7 changes: 4 additions & 3 deletions packages/element/docs/guide/radio.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

### 扩展属性

| 属性名 | 类型 | 描述 | 默认值 |
| ------- | --------------------------------------------------------------------------------- | ---- | ------ |
| options | [RadioProps](https://element.eleme.io/#/zh-CN/component/radio#radio-attributes)[] | 选项 | [] |
| 属性名 | 类型 | 描述 | 默认值 |
| ---------- | --------------------------------------------------------------------------------- | -------- | ------- |
| options | [RadioProps](https://element.eleme.io/#/zh-CN/component/radio#radio-attributes)[] | 选项 | [] |
| optionType | default/button | 样式类型 | default |
1 change: 1 addition & 0 deletions packages/element/src/__builtins__/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './create-context'
export * from './utils'
export * from './portal'
export * from './loading'
export * from './types'
13 changes: 11 additions & 2 deletions packages/element/src/__builtins__/shared/resolve-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ import { h, toRaw } from '@vue/composition-api'
import { Component, VNode } from 'vue'

export const resolveComponent = (
child?: Component | string | number | ((...args: any[]) => VNode[] | VNode),
child?:
| Component
| string
| number
| boolean
| ((...args: any[]) => VNode[] | VNode),
props?: Record<string, any>
) => {
if (child) {
if (typeof child === 'string' || typeof child === 'number') {
if (
typeof child === 'string' ||
typeof child === 'number' ||
typeof child === 'boolean'
) {
return child
} else if (typeof child === 'function') {
return (child as Function)(props)
Expand Down
8 changes: 8 additions & 0 deletions packages/element/src/__builtins__/shared/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Component, VNode } from 'vue'

export type SlotTypes =
| Component
| string
| number
| ((props: Record<string, any>) => VNode[] | VNode)
| VNode
59 changes: 49 additions & 10 deletions packages/element/src/checkbox/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { connect, mapProps, h, mapReadPretty } from '@formily/vue'
import { defineComponent } from '@vue/composition-api'
import { composeExport, getComponentByTag } from '../__builtins__/shared'
import { defineComponent, PropType } from '@vue/composition-api'
import {
composeExport,
getComponentByTag,
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'

Expand All @@ -18,12 +24,13 @@ type ElCheckboxProps = Omit<_ElCheckboxProps, 'value'> & {
export interface CheckboxProps extends ElCheckboxProps {
option: Omit<_ElCheckboxProps, 'value'> & {
value: ElCheckboxProps['label']
label: string
label: SlotTypes
}
}

const CheckboxOption = defineComponent<CheckboxProps>({
name: 'Checkbox',
inheritAttrs: false,
props: {
option: {
type: Object,
Expand All @@ -35,16 +42,18 @@ const CheckboxOption = defineComponent<CheckboxProps>({
const props = attrs as unknown as CheckboxProps
const option = curtomProps?.option
if (option) {
const children = option.label
? { default: () => [option.label] }
: slots
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(
ElCheckbox,
attrs.optionType === 'button' ? ElCheckboxButton : ElCheckbox,
{
attrs: {
...newProps,
Expand All @@ -71,6 +80,7 @@ const CheckboxOption = defineComponent<CheckboxProps>({
export type CheckboxGroupProps = ElCheckboxGroupProps & {
value: any[]
options?: Array<CheckboxProps | string>
optionType: 'default' | 'button'
}

const TransformElCheckboxGroup = getComponentByTag(ElCheckboxGroup, {
Expand All @@ -84,6 +94,10 @@ const CheckboxGroupOption = defineComponent<CheckboxGroupProps>({
type: Array,
default: () => [],
},
optionType: {
type: String as PropType<CheckboxGroupProps['optionType']>,
default: 'default',
},
},
setup(customProps, { attrs, slots, listeners }) {
return () => {
Expand All @@ -96,11 +110,36 @@ const CheckboxGroupOption = defineComponent<CheckboxGroupProps>({
if (typeof option === 'string') {
return h(
Checkbox,
{ props: { option: { label: option, value: option } } },
{}
{
props: {
option: {
label: option,
value: option,
},
},
attrs: {
optionType: customProps.optionType,
},
},
slots?.option
? { default: () => slots.option({ option }) }
: {}
)
} else {
return h(Checkbox, { props: { option } }, {})
return h(
Checkbox,
{
props: {
option,
},
attrs: {
optionType: customProps.optionType,
},
},
slots?.option
? { default: () => slots.option({ option }) }
: {}
)
}
}),
}
Expand Down
12 changes: 6 additions & 6 deletions packages/element/src/form-item/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ export const FormBaseItem = defineComponent<FormItemProps>({
className: {},
required: {},
label: {},
colon: { default: true },
colon: {},
layout: {},
tooltip: {},
labelStyle: {},
labelAlign: {},
labelWrap: { default: false },
labelWrap: {},
labelWidth: {},
wrapperWidth: {},
labelCol: {},
wrapperCol: {},
wrapperAlign: { default: 'left' },
wrapperAlign: {},
wrapperWrap: {},
wrapperStyle: {},
fullness: {},
Expand Down Expand Up @@ -151,16 +151,16 @@ export const FormBaseItem = defineComponent<FormItemProps>({
return () => {
const {
label,
colon,
colon = deepLayout.colon ?? true,
layout = deepLayout.layout ?? 'horizontal',
tooltip,
labelStyle = {},
labelWrap = deepLayout.labelWrap,
labelWrap = deepLayout.labelWrap ?? false,
labelWidth = deepLayout.labelWidth,
wrapperWidth = deepLayout.wrapperWidth,
labelCol = deepLayout.labelCol,
wrapperCol = deepLayout.wrapperCol,
wrapperAlign = deepLayout.wrapperAlign,
wrapperAlign = deepLayout.wrapperAlign ?? 'left',
wrapperWrap = deepLayout.wrapperWrap,
wrapperStyle = {},
fullness = deepLayout.fullness,
Expand Down
2 changes: 1 addition & 1 deletion packages/element/src/form-item/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@
}

.#{$form-item-prefix}-colon {
margin-left: 4px / 2;
margin-left: 2px;
margin-right: 8px;
}

Expand Down
Loading