forked from hug-sun/element3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hug-sun#540 from XianQijing/master
refactor(element3): refactor RadioGroup component
- Loading branch information
Showing
12 changed files
with
177 additions
and
148 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@import 'mixins/mixins'; | ||
@import 'common/var'; | ||
|
||
@include b(radio-group) { | ||
display: inline-block; | ||
line-height: 1; | ||
vertical-align: middle; | ||
font-size: 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/element3/src/components/RadioButton/tests/RadioButton.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import ElRadioGroup from './src/RadioGroup' | ||
|
||
ElRadioGroup.install = function (app) { | ||
app.component(ElRadioGroup.name, ElRadioGroup) | ||
} | ||
|
||
export { ElRadioGroup } |
45 changes: 45 additions & 0 deletions
45
packages/element3/src/components/RadioGroup/src/RadioGroup.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<template> | ||
<div class="el-radio-group" role="radiogroup"> | ||
<slot></slot> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { props } from './props' | ||
import { defineComponent, inject, computed, toRefs, watch } from 'vue' | ||
import { useGlobalOptions } from '../../../use/globalConfig' | ||
import { useEmitter } from '../../../use/emitter' | ||
export default defineComponent({ | ||
name: 'ElRadioGroup', | ||
props, | ||
emits: ['update:modelValue', 'change'], | ||
setup(props) { | ||
const { size, modelValue } = toRefs(props) | ||
const globalConfig = useGlobalOptions() | ||
const elFormItem = inject('elFormItem', {}) | ||
const { dispatch } = useEmitter() | ||
watch(modelValue, (v) => { | ||
dispatch('el.form.change', v) | ||
}) | ||
const radioGroupSize = useRadioGroupSize({ size, elFormItem, globalConfig }) | ||
return { | ||
radioGroupSize | ||
} | ||
} | ||
}) | ||
const useRadioGroupSize = ({ size, elFormItem, globalConfig }) => { | ||
return computed(() => { | ||
return ( | ||
size?.value || (elFormItem as any)?.elFormItemSize || globalConfig.size | ||
) | ||
}) | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { RadioGroupSize } from './types' | ||
import { PropType } from 'vue' | ||
|
||
export const props = { | ||
modelValue: [String, Number, Symbol, Boolean], | ||
size: { | ||
type: String as PropType<RadioGroupSize>, | ||
validator(val: string): boolean { | ||
return ['medium', 'small', 'mini', ''].includes(val) | ||
} | ||
}, | ||
fill: { | ||
type: String, | ||
default: '#409EFF' | ||
}, | ||
textColor: { | ||
type: String, | ||
default: '#ffffff' | ||
}, | ||
disabled: Boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type RadioGroupSize = 'medium' | 'small' | 'mini' |
67 changes: 67 additions & 0 deletions
67
packages/element3/src/components/RadioGroup/tests/RadioGroup.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import RadioGroup from '../src/RadioGroup.vue' | ||
import { mount } from '@vue/test-utils' | ||
import { ref } from 'vue' | ||
|
||
describe('radioGroup.vue', () => { | ||
it('should show content', () => { | ||
const wrapper = mount(RadioGroup, { | ||
slots: { | ||
default: 'radioGroup' | ||
} | ||
}) | ||
|
||
expect(wrapper).toHaveTextContent('radioGroup') | ||
}) | ||
|
||
it('form-item size', async () => { | ||
const wrapper = mount(RadioGroup, { | ||
props: { | ||
size: '' | ||
}, | ||
global: { | ||
provide: { | ||
elFormItem: { | ||
elFormItemSize: 'small' | ||
} | ||
} | ||
} | ||
}) | ||
|
||
expect(wrapper.vm.radioGroupSize).toEqual('small') | ||
|
||
await wrapper.setProps({ | ||
size: 'medium' | ||
}) | ||
expect(wrapper.vm.radioGroupSize).toEqual('medium') | ||
}) | ||
|
||
it('size', async () => { | ||
const wrapper = mount(RadioGroup, { | ||
props: { | ||
size: 'mini' | ||
} | ||
}) | ||
|
||
expect(wrapper.vm.radioGroupSize).toEqual('mini') | ||
|
||
await wrapper.setProps({ | ||
size: 'small' | ||
}) | ||
expect(wrapper.vm.radioGroupSize).toEqual('small') | ||
}) | ||
|
||
it('modelValue', async () => { | ||
const modelValue = ref('') | ||
const wrapper = mount(RadioGroup, { | ||
props: { | ||
modelValue, | ||
'update:modelValue'(v) { | ||
modelValue.value = v | ||
} | ||
} | ||
}) | ||
|
||
await wrapper.setProps({ modelValue: '上海' }) | ||
expect(wrapper.vm.modelValue).toEqual('上海') | ||
}) | ||
}) |
24 changes: 24 additions & 0 deletions
24
packages/element3/src/components/RadioGroup/tests/props.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { props } from '../src/props' | ||
|
||
describe('RadioGroup.props', () => { | ||
it('size', () => { | ||
const { size } = props | ||
|
||
expect(size.validator('medium')).toBe(true) | ||
expect(size.validator('small')).toBe(true) | ||
expect(size.validator('mini')).toBe(true) | ||
expect(size.validator('')).toBe(true) | ||
}) | ||
|
||
it('fill', () => { | ||
const { fill } = props | ||
|
||
expect(fill.default).toBe('#409EFF') | ||
}) | ||
|
||
it('textColor', () => { | ||
const { textColor } = props | ||
|
||
expect(textColor.default).toBe('#ffffff') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters