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

View File

@ -33,17 +33,21 @@ export default defineComponent({
setup(props, { emit, slots }) {
const iconRef = ref();
const getParentProp = (name: string) => {
if (props.parent) {
return props.parent.props[name];
}
return null;
};
const disabled = computed(
() => (props.parent && props.parent.disabled) || props.disabled
() => getParentProp('disabled') || props.disabled
);
const direction = computed(
() => (props.parent && props.parent.direction) || null
);
const direction = computed(() => getParentProp('direction') || null);
const iconStyle = computed(() => {
const checkedColor =
props.checkedColor || (props.parent && props.parent.checkedColor);
const checkedColor = props.checkedColor || getParentProp('checkedColor');
if (checkedColor && props.checked && !disabled.value) {
return {
@ -72,8 +76,8 @@ export default defineComponent({
};
const renderIcon = () => {
const { bem, shape, parent, checked } = props;
const iconSize = props.iconSize || (parent && parent.iconSize);
const { bem, shape, checked } = props;
const iconSize = props.iconSize || getParentProp('iconSize');
return (
<div

View File

@ -1,32 +1,39 @@
import { watch } from 'vue';
import { createNamespace } from '../utils';
import { FieldMixin } from '../mixins/field';
import { ParentMixin } from '../mixins/relation';
import { useChildren } from '../composition/use-relation';
import { useParentField } from '../composition/use-parent-field';
const [createComponent, bem] = createNamespace('radio-group');
export default createComponent({
mixins: [ParentMixin('vanRadio'), FieldMixin],
export const RADIO_KEY = 'vanRadio';
export default createComponent({
props: {
disabled: Boolean,
iconSize: [Number, String],
direction: String,
modelValue: null,
checkedColor: String,
iconSize: [Number, String],
},
emits: ['change', 'update:modelValue'],
watch: {
value(value) {
this.$emit('change', value);
},
},
setup(props, { emit, slots }) {
const { linkChildren } = useChildren(RADIO_KEY);
render() {
return (
<div class={bem([this.direction])} role="radiogroup">
{this.$slots.default?.()}
watch(
() => props.modelValue,
(value) => {
emit('change', value);
}
);
linkChildren({ emit, props });
useParentField(() => props.modelValue);
return () => (
<div class={bem([props.direction])} role="radiogroup">
{slots.default?.()}
</div>
);
},

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}
/>
);
},