feat: radio add disabled context

pull/6300/head^2
tangjinzhou 2023-02-21 12:06:31 +08:00
parent e04f73dfef
commit c301c63e8b
4 changed files with 39 additions and 13 deletions

View File

@ -4,16 +4,15 @@ import classNames from '../_util/classNames';
import PropTypes from '../_util/vue-types'; import PropTypes from '../_util/vue-types';
import Radio from './Radio'; import Radio from './Radio';
import useConfigInject from '../config-provider/hooks/useConfigInject'; import useConfigInject from '../config-provider/hooks/useConfigInject';
import { tuple } from '../_util/type'; import { booleanType, stringType, arrayType, functionType } from '../_util/type';
import type { RadioChangeEvent, RadioGroupButtonStyle, RadioGroupOptionType } from './interface'; import type { RadioChangeEvent, RadioGroupButtonStyle, RadioGroupOptionType } from './interface';
import { useInjectFormItemContext } from '../form/FormItemContext'; import { useInjectFormItemContext } from '../form/FormItemContext';
import { useProvideRadioGroupContext } from './context'; import { useProvideRadioGroupContext } from './context';
import { booleanType, stringType, arrayType, functionType } from '../_util/type';
// CSSINJS // CSSINJS
import useStyle from './style'; import useStyle from './style';
const RadioGroupSizeTypes = tuple('large', 'default', 'small'); const RadioGroupSizeTypes = ['large', 'default', 'small'] as const;
export type RadioGroupSize = (typeof RadioGroupSizeTypes)[number]; export type RadioGroupSize = (typeof RadioGroupSizeTypes)[number];
@ -28,7 +27,7 @@ export type RadioGroupChildOption = {
export const radioGroupProps = () => ({ export const radioGroupProps = () => ({
prefixCls: String, prefixCls: String,
value: PropTypes.any, value: PropTypes.any,
size: PropTypes.oneOf(RadioGroupSizeTypes), size: stringType<RadioGroupSize>(),
options: arrayType<Array<string | RadioGroupChildOption | number>>(), options: arrayType<Array<string | RadioGroupChildOption | number>>(),
disabled: booleanType(), disabled: booleanType(),
name: String, name: String,

View File

@ -13,6 +13,7 @@ import { booleanType, functionType } from '../_util/type';
// CSSINJS // CSSINJS
import useStyle from './style'; import useStyle from './style';
import { useInjectDisabled } from '../config-provider/DisabledContext';
export const radioProps = () => ({ export const radioProps = () => ({
prefixCls: String, prefixCls: String,
@ -45,12 +46,13 @@ export default defineComponent({
const radioGroupContext = useInjectRadioGroupContext(); const radioGroupContext = useInjectRadioGroupContext();
const vcCheckbox = ref<HTMLElement>(); const vcCheckbox = ref<HTMLElement>();
const { prefixCls: radioPrefixCls, direction } = useConfigInject('radio', props); const { prefixCls: radioPrefixCls, direction, disabled } = useConfigInject('radio', props);
const prefixCls = computed(() => const prefixCls = computed(() =>
radioGroupContext?.optionType.value === 'button' || radioOptionTypeContext === 'button' radioGroupContext?.optionType.value === 'button' || radioOptionTypeContext === 'button'
? `${radioPrefixCls.value}-button` ? `${radioPrefixCls.value}-button`
: radioPrefixCls.value, : radioPrefixCls.value,
); );
const contextDisabled = useInjectDisabled();
// Style // Style
const [wrapSSR, hashId] = useStyle(radioPrefixCls); const [wrapSSR, hashId] = useStyle(radioPrefixCls);
@ -88,6 +90,7 @@ export default defineComponent({
prefixCls: prefixCls.value, prefixCls: prefixCls.value,
id, id,
...omit(restProps, ['onUpdate:checked', 'onUpdate:value']), ...omit(restProps, ['onUpdate:checked', 'onUpdate:value']),
disabled: disabled.value ?? contextDisabled.value,
}; };
if (radioGroup) { if (radioGroup) {

View File

@ -18,9 +18,9 @@ Radio unavailable.
<template> <template>
<div> <div>
<a-radio v-model:checked="checked1" :disabled="disabled">Disabled</a-radio> <a-radio v-model:checked="checked1" :disabled="disabled">Disabled</a-radio>
<br />
<a-radio v-model:checked="checked2" :disabled="disabled">Disabled</a-radio> <a-radio v-model:checked="checked2" :disabled="disabled">Disabled</a-radio>
<div :style="{ marginTop: 20 }"> <br />
<div style="margin-top: 16px">
<a-button type="primary" @click="toggleDisabled">Toggle disabled</a-button> <a-button type="primary" @click="toggleDisabled">Toggle disabled</a-button>
</div> </div>
</div> </div>
@ -30,7 +30,7 @@ import { defineComponent, ref } from 'vue';
export default defineComponent({ export default defineComponent({
setup() { setup() {
const disabled = ref<boolean>(true); const disabled = ref<boolean>(true);
const checked1 = ref<boolean>(false); const checked1 = ref<boolean>(true);
const checked2 = ref<boolean>(false); const checked2 = ref<boolean>(false);
const toggleDisabled = () => { const toggleDisabled = () => {

View File

@ -1,7 +1,31 @@
<template> <template>
<a-alert message="Success Tips" type="success" show-icon closable> <div>
<template #action> <a-radio v-model:checked="checked1" :disabled="disabled">Disabled</a-radio>
<a-button v-if="false" size="small" type="text">UNDO</a-button> <a-radio v-model:checked="checked2" :disabled="disabled">Disabled</a-radio>
</template> <br />
</a-alert> <div style="margin-top: 16px">
<a-button type="primary" @click="toggleDisabled">Toggle disabled</a-button>
</div>
</div>
</template> </template>
<script lang="ts">
import { defineComponent, ref } from 'vue';
export default defineComponent({
setup() {
const disabled = ref<boolean>(true);
const checked1 = ref<boolean>(false);
const checked2 = ref<boolean>(false);
const toggleDisabled = () => {
disabled.value = !disabled.value;
};
return {
disabled,
checked1,
checked2,
toggleDisabled,
};
},
});
</script>