Skip to content

Commit

Permalink
refactor(switch): sfc to tsx (Tencent#1322)
Browse files Browse the repository at this point in the history
* refactor(switch): sfc to tsx

* test: snap  update

* test: snap  update

* docs: sync api
  • Loading branch information
liweijie0812 authored Apr 23, 2024
1 parent 5aaa468 commit 639a301
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 128 deletions.
4 changes: 0 additions & 4 deletions src/cell/__test__/__snapshots__/demo.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ exports[`Cell > Cell mobileVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium t-switch__label--checked"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down Expand Up @@ -708,7 +707,6 @@ exports[`Cell > Cell mobileVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium t-switch__label--checked"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down Expand Up @@ -1404,7 +1402,6 @@ exports[`Cell > Cell multipleVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium t-switch__label--checked"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down Expand Up @@ -1885,7 +1882,6 @@ exports[`Cell > Cell singleVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium t-switch__label--checked"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down
1 change: 0 additions & 1 deletion src/form/__test__/__snapshots__/demo.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1709,7 +1709,6 @@ exports[`Form > Form mobileVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down
1 change: 0 additions & 1 deletion src/loading/__test__/__snapshots__/demo.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ exports[`Loading > Loading delayVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down
26 changes: 0 additions & 26 deletions src/switch/__test__/__snapshots__/demo.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ exports[`Switch > Switch baseVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium t-switch__label--checked"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down Expand Up @@ -79,7 +78,6 @@ exports[`Switch > Switch colorVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium t-switch__label--checked"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down Expand Up @@ -128,10 +126,7 @@ exports[`Switch > Switch labelVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium t-switch__label--checked"
>
<!---->
</div>
</div>
</div>
Expand Down Expand Up @@ -176,7 +171,6 @@ exports[`Switch > Switch labelVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium t-switch__label--checked"
>
<!--v-if-->
<svg
class="t-icon t-icon-check"
fill="none"
Expand Down Expand Up @@ -269,7 +263,6 @@ exports[`Switch > Switch mobileVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium t-switch__label--checked"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down Expand Up @@ -332,10 +325,7 @@ exports[`Switch > Switch mobileVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium t-switch__label--checked"
>
<!---->
</div>
</div>
</div>
Expand Down Expand Up @@ -380,7 +370,6 @@ exports[`Switch > Switch mobileVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium t-switch__label--checked"
>
<!--v-if-->
<svg
class="t-icon t-icon-check"
fill="none"
Expand Down Expand Up @@ -455,7 +444,6 @@ exports[`Switch > Switch mobileVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium t-switch__label--checked"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down Expand Up @@ -549,7 +537,6 @@ exports[`Switch > Switch mobileVue demo works fine 1`] = `
<!--v-if-->
<!---->
</div>
<!---->
</div>
</div>
</div>
Expand Down Expand Up @@ -617,7 +604,6 @@ exports[`Switch > Switch mobileVue demo works fine 1`] = `
<!--v-if-->
<!---->
</div>
<!---->
</div>
</div>
</div>
Expand Down Expand Up @@ -663,7 +649,6 @@ exports[`Switch > Switch mobileVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down Expand Up @@ -705,7 +690,6 @@ exports[`Switch > Switch mobileVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium t-switch__label--checked"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down Expand Up @@ -775,7 +759,6 @@ exports[`Switch > Switch mobileVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--large t-switch__label--checked"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down Expand Up @@ -819,7 +802,6 @@ exports[`Switch > Switch mobileVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium t-switch__label--checked"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down Expand Up @@ -863,7 +845,6 @@ exports[`Switch > Switch mobileVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--small t-switch__label--checked"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down Expand Up @@ -919,7 +900,6 @@ exports[`Switch > Switch sizeVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--large t-switch__label--checked"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down Expand Up @@ -963,7 +943,6 @@ exports[`Switch > Switch sizeVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium t-switch__label--checked"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down Expand Up @@ -1007,7 +986,6 @@ exports[`Switch > Switch sizeVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--small t-switch__label--checked"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down Expand Up @@ -1085,7 +1063,6 @@ exports[`Switch > Switch statusVue demo works fine 1`] = `
<!--v-if-->
<!---->
</div>
<!---->
</div>
</div>
</div>
Expand Down Expand Up @@ -1153,7 +1130,6 @@ exports[`Switch > Switch statusVue demo works fine 1`] = `
<!--v-if-->
<!---->
</div>
<!---->
</div>
</div>
</div>
Expand Down Expand Up @@ -1199,7 +1175,6 @@ exports[`Switch > Switch statusVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down Expand Up @@ -1241,7 +1216,6 @@ exports[`Switch > Switch statusVue demo works fine 1`] = `
<div
class="t-switch__label t-switch__label--medium t-switch__label--checked"
>
<!--v-if-->
<!---->
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/switch/__test__/index.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mount } from '@vue/test-utils';
import { describe, it, expect, vi } from 'vitest';
import Switch from '../switch.vue';
import Switch from '../switch';
import { ref } from 'vue';

describe('switch', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/switch/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Switch from './switch.vue';
import Switch from './switch';
import { withInstall, WithInstallType } from '../shared';
import { TdSwitchProps } from './type';

Expand Down
2 changes: 1 addition & 1 deletion src/switch/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default {
customValue: {
type: Array as PropType<TdSwitchProps['customValue']>,
},
/** 是否禁用组件,默认为 false */
/** 是否禁用组件 */
disabled: Boolean,
/** 开关的图标;[打开时的图标,关闭时的图标] */
icon: {
Expand Down
7 changes: 4 additions & 3 deletions src/switch/switch.en-US.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
:: BASE_DOC ::

## API

### Switch Props

name | type | default | description | required
Expand All @@ -10,7 +11,7 @@ disabled | Boolean | - | \- | N
icon | Array | [] | Typescript:`TNode[]`[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/blob/develop/src/common.ts) | N
label | Array | [] | Typescript:`string[]` | N
loading | Boolean | false | \- | N
size | String | medium | optionssmall/medium/large | N
size | String | medium | options: small/medium/large | N
value | String / Number / Boolean | - | `v-model` and `v-model:value` is supported。Typescript:`T` `type SwitchValue = string \| number \| boolean`[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/switch/type.ts) | N
defaultValue | String / Number / Boolean | - | uncontrolled property。Typescript:`T` `type SwitchValue = string \| number \| boolean`[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/tree/develop/src/switch/type.ts) | N
onChange | Function | | Typescript:`(value: T, context: { e: MouseEvent }) => void`<br/> | N
Expand All @@ -21,8 +22,8 @@ name | params | description
-- | -- | --
change | `(value: T, context: { e: MouseEvent })` | \-

### CSS 变量

### CSS Variables
The component provides the following CSS variables, which can be used to customize styles.
Name | Default Value | Description
-- | -- | --
Expand Down Expand Up @@ -56,4 +57,4 @@ Name | Default Value | Description
--td-switch-width | 45px | -
--td-switch-label-font-size | 12px | -
--td-switch-label-font-size | 14px | -
--td-switch-label-font-size | 16px | -
--td-switch-label-font-size | 16px | -
9 changes: 5 additions & 4 deletions src/switch/switch.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
:: BASE_DOC ::

## API

### Switch Props

名称 | 类型 | 默认值 | 说明 | 必传
名称 | 类型 | 默认值 | 描述 | 必传
-- | -- | -- | -- | --
customValue | Array | - | 用于自定义开关的值,[打开时的值,关闭时的值]。默认为 [true, false]。示例:[1, 0]['open', 'close']。TS 类型:`Array<SwitchValue>` | N
disabled | Boolean | - | 是否禁用组件,默认为 false | N
disabled | Boolean | - | 是否禁用组件 | N
icon | Array | [] | 开关的图标;[打开时的图标,关闭时的图标]。TS 类型:`TNode[]`[通用类型定义](https://github.com/Tencent/tdesign-mobile-vue/blob/develop/src/common.ts) | N
label | Array | [] | 开关的标签;[打开时的标签,关闭时的标签]。TS 类型:`string[]` | N
loading | Boolean | false | 是否处于加载中状态 | N
Expand All @@ -21,8 +22,8 @@ onChange | Function | | TS 类型:`(value: T, context: { e: MouseEvent }) =>
-- | -- | --
change | `(value: T, context: { e: MouseEvent })` | 数据发生变化时触发


### CSS 变量

组件提供了下列 CSS 变量,可用于自定义样式。
名称 | 默认值 | 描述
-- | -- | --
Expand Down Expand Up @@ -56,4 +57,4 @@ change | `(value: T, context: { e: MouseEvent })` | 数据发生变化时触发
--td-switch-width | 45px | -
--td-switch-label-font-size | 12px | -
--td-switch-label-font-size | 14px | -
--td-switch-label-font-size | 16px | -
--td-switch-label-font-size | 16px | -
79 changes: 79 additions & 0 deletions src/switch/switch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { computed, defineComponent } from 'vue';
import TLoading from '../loading';
import { useToggle, useDefault } from '../shared';
import config from '../config';
import SwitchProps from './props';
import { SwitchValue, TdSwitchProps } from './type';
import { useFormDisabled } from '../form/hooks';
import { usePrefixClass } from '../hooks/useClass';

const { prefix } = config;
const name = `${prefix}-switch`;
export default defineComponent({
name,
props: SwitchProps,
setup(props, context) {
const switchClass = usePrefixClass('switch');

const disabled = useFormDisabled();
const switchValues = props.customValue || [true, false];
const [innerValue] = useDefault<SwitchValue, TdSwitchProps>(props, context.emit, 'value', 'change');
const { state, toggle } = useToggle<SwitchValue>(switchValues, innerValue.value);
const checked = computed(() => innerValue.value === switchValues[0]);
const switchClasses = computed(() => [
`${switchClass.value}`,
`${switchClass.value}--${props.size}`,
{
[`${switchClass.value}--checked`]: checked.value,
[`${switchClass.value}--disabled`]: disabled.value || props.loading,
},
]);
const dotClasses = computed(() => [
`${switchClass.value}__dot`,
`${switchClass.value}__dot--${props.size}`,
{
[`${switchClass.value}__dot--checked`]: checked.value,
[`${switchClass.value}__dot--plain`]: props.label?.length !== 2 && props.icon?.length !== 2 && !props.loading,
},
]);
const labelClasses = computed(() => [
`${switchClass.value}__label`,
`${switchClass.value}__label--${props.size}`,
{
[`${switchClass.value}__label--checked`]: checked.value,
},
]);
const iconContent = computed(() => props.icon[checked.value ? 0 : 1]);

function handleToggle(event: Event) {
event.preventDefault();
if (disabled.value || props.loading) {
return;
}

if (state.value === innerValue.value) {
toggle();
}

innerValue.value = state.value;
}
return () => {
const readerContent = () => {
if (props.loading) {
return <TLoading inherit-color size="16.25px" />;
}
if (props.label.length === 2) {
return checked.value ? props.label[0] : props.label[1];
}
return iconContent.value;
};
return (
<div class={switchClasses.value} onClick={handleToggle}>
<div class={dotClasses.value}>
<div class={labelClasses.value}>{readerContent()}</div>
</div>
</div>
);
};
},
});
Loading

0 comments on commit 639a301

Please sign in to comment.