types(Radio): use tsx (#8129)

This commit is contained in:
neverland 2021-02-11 10:35:22 +08:00 committed by GitHub
parent b5944524d9
commit 4ce467c683
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 13 deletions

View File

@ -3,19 +3,18 @@ import { createNamespace } from '../utils';
import { useChildren } from '@vant/use'; import { useChildren } from '@vant/use';
import { useExpose } from '../composables/use-expose'; import { useExpose } from '../composables/use-expose';
import { useLinkField } from '../composables/use-link-field'; import { useLinkField } from '../composables/use-link-field';
import { CheckerParent, CheckerDirection } from '../checkbox/Checker';
const [createComponent, bem] = createNamespace('checkbox-group'); const [createComponent, bem] = createNamespace('checkbox-group');
export const CHECKBOX_GROUP_KEY = 'vanCheckboxGroup'; export const CHECKBOX_GROUP_KEY = 'vanCheckboxGroup';
export type CheckboxGroupDirection = 'horizontal' | 'vertical';
export type CheckboxGroupToggleAllOptions = { export type CheckboxGroupToggleAllOptions = {
checked?: boolean; checked?: boolean;
skipDisabled?: boolean; skipDisabled?: boolean;
}; };
export type CheckboxGroupProvide = { export type CheckboxGroupProvide = CheckerParent & {
props: { props: {
max: number | string; max: number | string;
modelValue: any[]; modelValue: any[];
@ -27,7 +26,7 @@ export default createComponent({
props: { props: {
max: [Number, String], max: [Number, String],
disabled: Boolean, disabled: Boolean,
direction: String as PropType<CheckboxGroupDirection>, direction: String as PropType<CheckerDirection>,
iconSize: [Number, String], iconSize: [Number, String],
checkedColor: String, checkedColor: String,
modelValue: { modelValue: {

View File

@ -3,13 +3,13 @@ import { addUnit } from '../utils';
import Icon from '../icon'; import Icon from '../icon';
export type CheckerShape = 'square' | 'round'; export type CheckerShape = 'square' | 'round';
export type CheckerDirection = 'horizontal' | 'vertical';
export type CheckerLabelPosition = 'left' | 'right'; export type CheckerLabelPosition = 'left' | 'right';
export type CheckerParent = {
type CheckerParent = {
props: { props: {
disabled?: boolean; disabled?: boolean;
iconSize?: number | string; iconSize?: number | string;
direction?: 'horizontal' | 'vertical'; direction?: CheckerDirection;
checkedColor?: string; checkedColor?: string;
}; };
}; };

View File

@ -2,17 +2,25 @@ import { watch } from 'vue';
import { createNamespace } from '../utils'; import { createNamespace } from '../utils';
import { useChildren } from '@vant/use'; import { useChildren } from '@vant/use';
import { useLinkField } from '../composables/use-link-field'; import { useLinkField } from '../composables/use-link-field';
import { CheckerParent } from '../checkbox/Checker';
const [createComponent, bem] = createNamespace('radio-group'); const [createComponent, bem] = createNamespace('radio-group');
export const RADIO_KEY = 'vanRadio'; export const RADIO_KEY = 'vanRadio';
export type RadioGroupProvide = CheckerParent & {
props: {
modelValue: any;
};
updateModelValue: (value: unknown) => void;
};
export default createComponent({ export default createComponent({
props: { props: {
disabled: Boolean, disabled: Boolean,
iconSize: [Number, String], iconSize: [Number, String],
direction: String, direction: String,
modelValue: null, modelValue: null as any,
checkedColor: String, checkedColor: String,
}, },
@ -21,6 +29,10 @@ export default createComponent({
setup(props, { emit, slots }) { setup(props, { emit, slots }) {
const { linkChildren } = useChildren(RADIO_KEY); const { linkChildren } = useChildren(RADIO_KEY);
const updateModelValue = (value: unknown) => {
emit('update:modelValue', value);
};
watch( watch(
() => props.modelValue, () => props.modelValue,
(value) => { (value) => {
@ -28,7 +40,11 @@ export default createComponent({
} }
); );
linkChildren({ emit, props }); linkChildren({
props,
updateModelValue,
});
useLinkField(() => props.modelValue); useLinkField(() => props.modelValue);
return () => ( return () => (

View File

@ -1,7 +1,7 @@
import { pick, createNamespace } from '../utils'; import { pick, createNamespace } from '../utils';
import { useParent } from '@vant/use'; import { useParent } from '@vant/use';
import Checker, { checkerProps } from '../checkbox/Checker'; import Checker, { checkerProps } from '../checkbox/Checker';
import { RADIO_KEY } from '../radio-group'; import { RADIO_KEY, RadioGroupProvide } from '../radio-group';
const [createComponent, bem] = createNamespace('radio'); const [createComponent, bem] = createNamespace('radio');
@ -11,7 +11,7 @@ export default createComponent({
emits: ['update:modelValue'], emits: ['update:modelValue'],
setup(props, { emit, slots }) { setup(props, { emit, slots }) {
const { parent } = useParent(RADIO_KEY); const { parent } = useParent<RadioGroupProvide>(RADIO_KEY);
const checked = () => { const checked = () => {
const value = parent ? parent.props.modelValue : props.modelValue; const value = parent ? parent.props.modelValue : props.modelValue;
@ -19,8 +19,11 @@ export default createComponent({
}; };
const toggle = () => { const toggle = () => {
const emitter = parent ? parent.emit : emit; if (parent) {
emitter('update:modelValue', props.name); parent.updateModelValue(props.name);
} else {
emit('update:modelValue', props.name);
}
}; };
return () => ( return () => (