refactor(Radio): refactor with composition api

This commit is contained in:
chenjiahan
2020-09-25 15:30:21 +08:00
parent 3402ba5c8f
commit aafbcfcf04
3 changed files with 51 additions and 51 deletions
+18 -29
View File
@@ -1,48 +1,37 @@
import { pick, createNamespace } from '../utils';
import { ChildrenMixin } from '../mixins/relation';
import { useParent } from '../composition/use-relation';
import Checker, { checkerProps } from '../checkbox/Checker';
import { RADIO_KEY } from '../radio-group';
const [createComponent, bem] = createNamespace('radio');
export default createComponent({
mixins: [ChildrenMixin('vanRadio')],
props: checkerProps,
emits: ['update:modelValue'],
computed: {
currentValue: {
get() {
return this.parent ? this.parent.modelValue : this.modelValue;
},
setup(props, { emit, slots }) {
const { parent } = useParent(RADIO_KEY);
set(val) {
(this.parent || this).$emit('update:modelValue', val);
},
},
const checked = () => {
const value = parent ? parent.props.modelValue : props.modelValue;
return value === props.name;
};
checked() {
return this.currentValue === this.name;
},
},
const toggle = () => {
const emitter = parent ? parent.emit : emit;
emitter('update:modelValue', props.name);
};
methods: {
toggle() {
this.currentValue = this.name;
},
},
render() {
return (
return () => (
<Checker
v-slots={pick(this.$slots, ['default', 'icon'])}
v-slots={pick(slots, ['default', 'icon'])}
bem={bem}
role="radio"
parent={this.parent}
checked={this.checked}
onToggle={this.toggle}
{...this.$props}
parent={parent}
checked={checked()}
onToggle={toggle}
{...props}
/>
);
},