Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Radio): fix allow-uncheck is invalid #1582

Merged
merged 4 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/radio/__test__/__snapshots__/demo.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ exports[`Radio > Radio customVue demo works fine 1`] = `
value="0"
/>
<div
class="t-radio__icon t-radio__icon--left t-radio__icon--checked"
class="t-radio__icon t-radio__icon--left"
>
<!---->
<!---->
Expand Down Expand Up @@ -419,6 +419,7 @@ exports[`Radio > Radio customVue demo works fine 1`] = `
<view
class="demo-desc"
data-v-5173b2c6=""
style="margin: 24px 16px 16px;"
>
横向卡片单选框
</view>
Expand Down Expand Up @@ -453,7 +454,7 @@ exports[`Radio > Radio customVue demo works fine 1`] = `
value="0"
/>
<div
class="t-radio__icon t-radio__icon--left t-radio__icon--checked"
class="t-radio__icon t-radio__icon--left"
>
<!---->
<!---->
Expand Down Expand Up @@ -1557,7 +1558,7 @@ exports[`Radio > Radio mobileVue demo works fine 1`] = `
value="0"
/>
<div
class="t-radio__icon t-radio__icon--left t-radio__icon--checked"
class="t-radio__icon t-radio__icon--left"
>
<!---->
<!---->
Expand Down Expand Up @@ -1667,6 +1668,7 @@ exports[`Radio > Radio mobileVue demo works fine 1`] = `
<view
class="demo-desc"
data-v-5173b2c6=""
style="margin: 24px 16px 16px;"
>
横向卡片单选框
</view>
Expand Down Expand Up @@ -1701,7 +1703,7 @@ exports[`Radio > Radio mobileVue demo works fine 1`] = `
value="0"
/>
<div
class="t-radio__icon t-radio__icon--left t-radio__icon--checked"
class="t-radio__icon t-radio__icon--left"
>
<!---->
<!---->
Expand Down
2 changes: 2 additions & 0 deletions src/radio/__test__/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ describe('Radio', () => {
it(': icon', async () => {
const wrapper = mount(Radio, {
props: {
checked: true,
icon: 'dot',
},
});
const $icon = wrapper.find(`.${name}__icon-dot`);

expect($icon.exists()).toBeTruthy();

//tsx vm
Expand Down
9 changes: 8 additions & 1 deletion src/radio/demos/custom.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</view>
</t-radio-group>

<view class="demo-desc" style="margin: 48rpx 32rpx 32rpx">横向卡片单选框</view>
<view class="demo-desc" style="margin: 24px 16px 16px">横向卡片单选框</view>

<t-radio-group class="horizontal-box" :value="radio1" @change="onChange1">
<view v-for="(item, index) in 3" :key="index" :class="`card ${radio1 == index ? 'card--active' : ''}`">
Expand All @@ -32,6 +32,12 @@ const onChange1 = (value: any, context: { e: Event }) => {
};
</script>
<style scoped lang="less">
.demo-desc {
font-size: 14px;
color: rgba(0, 0, 0, 0.6);
margin-bottom: 16px;
}

.card {
display: block;
position: relative;
Expand Down Expand Up @@ -65,6 +71,7 @@ const onChange1 = (value: any, context: { e: Event }) => {
left: 1.5px;
top: 1.5px;
z-index: 1;
font-size: 14px;
}

/* 横向布局 */
Expand Down
18 changes: 2 additions & 16 deletions src/radio/demos/icon.vue
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
<template>
<t-radio class="block" default-checked label="单选" icon="line"></t-radio>
<t-radio class="block" default-checked label="单选" icon="dot"></t-radio>
<t-radio class="block" default-checked allow-uncheck label="单选" icon="line"></t-radio>
<t-radio class="block" default-checked allow-uncheck label="单选" icon="dot"></t-radio>
</template>

<script lang="ts" setup>
import { ref, h } from 'vue';
import { CheckRectangleFilledIcon, CheckRectangleIcon } from 'tdesign-icons-vue-next';

const radio = ref('2');
const TiconCheckRectangleFilled = h(CheckRectangleFilledIcon);
const TiconCheckRectangle = h(CheckRectangleIcon);
const checkRectangle = [TiconCheckRectangleFilled, TiconCheckRectangle];

const groupChangeFn = (value: any, context: { e: Event }) => {
console.log(value, context);
};
</script>

<style scoped>
.radio-group-demo {
background-color: #fff;
Expand Down
4 changes: 2 additions & 2 deletions src/radio/demos/placement.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<t-radio class="block" default-checked label="单选" />
<t-radio class="block" default-checked label="单选" placement="right" />
<t-radio class="block" default-checked allow-uncheck label="单选" />
<t-radio class="block" default-checked allow-uncheck label="单选" placement="right" />
</template>

<script lang="ts" setup></script>
Expand Down
38 changes: 15 additions & 23 deletions src/radio/radio.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { inject, computed, defineComponent, Ref, toRefs } from 'vue';
import { inject, computed, defineComponent, Ref } from 'vue';
import { CheckIcon, CheckCircleFilledIcon } from 'tdesign-icons-vue-next';

import { NOOP, useVModel } from '../shared';
import { useDefault } from '../shared';
import config from '../config';
import props from './props';
import { RadioValue, TdRadioGroupProps } from './type';
import { TdRadioGroupProps, TdRadioProps } from './type';
import { useFormDisabled } from '../form/hooks';
import { usePrefixClass } from '../hooks/useClass';
import { useContent, useTNodeJSX } from '../hooks/tnode';
Expand All @@ -14,29 +13,24 @@ const { prefix } = config;
export default defineComponent({
name: `${prefix}-radio`,
props,
setup(props) {
emits: ['update:checked', 'update:modelValue', 'change'],
setup(props, context) {
const renderTNodeContent = useContent();
const renderTNodeJSX = useTNodeJSX();
const radioClass = usePrefixClass('radio');

const { checked, modelValue, block } = toRefs(props);
const [innerChecked, setInnerChecked] = useVModel(
checked,
modelValue,
props.defaultChecked,
props.onChange,
'checked',
);
const [innerChecked, setInnerChecked] = useDefault<boolean, TdRadioProps>(props, context.emit, 'checked', 'change');

const rootGroupProps = inject('rootGroupProps', {}) as TdRadioGroupProps;
const rootGroupValue = inject('rootGroupValue', {}) as Ref;
const rootGroupChange = inject('rootGroupChange', NOOP) as (val: RadioValue, e: Event) => void;
const rootGroupChange = inject('rootGroupChange', undefined);

// extend radioGroup disabled props
const groupDisabled = computed(() => rootGroupProps?.disabled);
const isDisabled = useFormDisabled(groupDisabled);

const radioChecked = computed(() => innerChecked.value || props.value === rootGroupValue?.value);
const radioChecked = computed(() => innerChecked.value || (props.value && props.value === rootGroupValue?.value));

const finalBorderless = computed(() => {
if (props.borderless == null && 'borderless' in rootGroupProps) return rootGroupProps.borderless;
return props.borderless;
Expand All @@ -46,10 +40,7 @@ export default defineComponent({
return props.placement || 'left';
});

const finalAllowUncheck = computed(() => {
if (props.allowUncheck == null && 'allowUncheck' in rootGroupProps) return rootGroupProps.allowUncheck;
return props.allowUncheck;
});
const finalAllowUncheck = computed(() => Boolean(props.allowUncheck || rootGroupProps?.allowUncheck));

// input props attribute
const inputProps = computed(() => ({
Expand All @@ -71,7 +62,7 @@ export default defineComponent({
`${radioClass.value}`,
`${radioClass.value}--${finalPlacement.value}`,
{
[`${radioClass.value}--block`]: block.value,
[`${radioClass.value}--block`]: props.block,
},
]);

Expand Down Expand Up @@ -104,10 +95,11 @@ export default defineComponent({
return;
}
if (rootGroupChange) {
rootGroupChange(finalAllowUncheck.value && radioChecked.value ? '' : props.value, e);
const value = finalAllowUncheck.value && radioChecked.value ? undefined : props.value;
rootGroupChange(value, e);
} else {
if (!finalAllowUncheck.value && radioChecked.value) return;
setInnerChecked(!radioChecked.value, { e });
const value = finalAllowUncheck.value ? !radioChecked.value : true;
setInnerChecked(value, { e });
}
};

Expand Down
Loading