Skip to content

Commit 9df8317

Browse files
committed
refactor: segmented #6286
1 parent 62e7f94 commit 9df8317

17 files changed

+520
-336
lines changed

components/_util/type.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,18 @@ export function functionType<T = () => {}>(defaultVal?: T) {
5959
return { type: Function as PropType<T>, default: defaultVal as T };
6060
}
6161

62-
export function anyType<T = any>(defaultVal?: T) {
63-
return { validator: () => true, default: defaultVal as T } as unknown as { type: PropType<T> };
62+
export function anyType<T = any>(defaultVal?: T, required?: boolean) {
63+
const type = { validator: () => true, default: defaultVal as T } as unknown;
64+
return required
65+
? (type as {
66+
type: PropType<T>;
67+
default: T;
68+
required: true;
69+
})
70+
: (type as {
71+
default: T;
72+
type: PropType<T>;
73+
});
6474
}
6575
export function vNodeType<T = VueNode>() {
6676
return { validator: () => true } as unknown as { type: PropType<T> };

components/segmented/demo/basic.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ The most basic usage.
1515
</docs>
1616

1717
<template>
18-
<a-segmented :options="data" />
18+
<a-segmented v-model:value="value" :options="data" />
1919
</template>
2020

2121
<script lang="ts">
22-
import { defineComponent, reactive } from 'vue';
22+
import { defineComponent, reactive, ref } from 'vue';
2323
2424
export default defineComponent({
2525
setup() {
2626
const data = reactive(['Daily', 'Weekly', 'Monthly', 'Quarterly', 'Yearly']);
27+
const value = ref(data[0]);
2728
return {
2829
data,
30+
value,
2931
};
3032
},
3133
});

components/segmented/demo/block.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@ title:
1414
`block` property will make the `Segmented` fit to its parent width.
1515
</docs>
1616
<template>
17-
<a-segmented block :options="data" />
17+
<a-segmented v-model:value="value" block :options="data" />
1818
</template>
1919

2020
<script lang="ts">
21-
import { defineComponent, reactive } from 'vue';
21+
import { defineComponent, reactive, ref } from 'vue';
2222
2323
export default defineComponent({
2424
setup() {
2525
const data = reactive([123, 456, 'longtext-longtext-longtext-longtext']);
26+
const value = ref(data[0]);
2627
return {
2728
data,
29+
value,
2830
};
2931
},
3032
});

components/segmented/demo/controlled.vue

-35
This file was deleted.

components/segmented/demo/custom.vue

+78-55
Original file line numberDiff line numberDiff line change
@@ -14,80 +14,103 @@ title:
1414
Custom each Segmented Item.
1515
</docs>
1616
<template>
17-
<a-segmented :options="data">
18-
<template #title="index">
19-
<template v-if="index === 0">
20-
<div style="padding: 4px 4px">
21-
<a-avatar src="https://joeschmoe.io/api/v1/random" />
22-
<div>User 1</div>
23-
</div>
24-
</template>
25-
<template v-if="index === 1">
26-
<div style="padding: 4px 4px">
27-
<a-avatar style="background-color: #f56a00">K</a-avatar>
28-
<div>User 2</div>
29-
</div>
30-
</template>
31-
<template v-if="index === 2">
32-
<div style="padding: 4px 4px">
33-
<a-avatar style="background-color: #1890ff">
34-
<template #icon><UserOutlined /></template>
17+
<a-segmented v-model:value="value" :options="data">
18+
<template #label="{ value: val, payload = {} }">
19+
<div style="padding: 4px 4px">
20+
<template v-if="payload.icon">
21+
<a-avatar :src="payload.src" :style="payload.style">
22+
<template #icon><component :is="payload.icon" /></template>
23+
{{ payload.content }}
3524
</a-avatar>
36-
<div>User 3</div>
37-
</div>
38-
</template>
25+
</template>
26+
<template v-else>
27+
<a-avatar :src="payload.src" :style="payload.style">
28+
{{ payload.content }}
29+
</a-avatar>
30+
</template>
31+
<div>{{ val }}</div>
32+
</div>
3933
</template>
4034
</a-segmented>
4135
<br />
4236
<br />
43-
<a-segmented :options="options2">
44-
<template #title="index">
45-
<template v-if="index === 0">
46-
<div style="padding: 4px 4px">
47-
<div>Spring</div>
48-
<div>Jan-Mar</div>
49-
</div>
50-
</template>
51-
<template v-if="index === 1">
52-
<div style="padding: 4px 4px">
53-
<div>Summer</div>
54-
<div>Apr-Jun</div>
55-
</div>
56-
</template>
57-
<template v-if="index === 2">
58-
<div style="padding: 4px 4px">
59-
<div>Autumn</div>
60-
<div>Jul-Sept</div>
61-
</div>
62-
</template>
63-
<template v-if="index === 3">
64-
<div style="padding: 4px 4px">
65-
<div>Winter</div>
66-
<div>Oct-Dec</div>
67-
</div>
68-
</template>
37+
<a-segmented v-model:value="value2" :options="options2">
38+
<template #label="{ payload }">
39+
<div style="padding: 4px 4px">
40+
<div>{{ payload.title }}</div>
41+
<div>{{ payload.subTitle }}</div>
42+
</div>
6943
</template>
7044
</a-segmented>
7145
</template>
7246

7347
<script lang="ts">
74-
import { defineComponent, reactive } from 'vue';
48+
import { defineComponent, ref } from 'vue';
7549
import { UserOutlined } from '@ant-design/icons-vue';
7650
import ASegmented from 'ant-design-vue/es/segmented/src/segmented';
77-
7851
export default defineComponent({
7952
components: { ASegmented, UserOutlined },
8053
setup() {
81-
const data = reactive([{ value: 'user1' }, { value: 'user2' }, { value: 'user3' }]);
82-
const options2 = reactive([
83-
{ value: 'spring' },
84-
{ value: 'summer' },
85-
{ value: 'autumn' },
86-
{ value: 'winter' },
54+
const data = ref([
55+
{
56+
value: 'user1',
57+
payload: {
58+
src: 'https://joeschmoe.io/api/v1/random',
59+
style: { backgroundColor: '#f56a00' },
60+
},
61+
},
62+
{
63+
value: 'user2',
64+
payload: {
65+
style: { backgroundColor: '#f56a00' },
66+
content: 'K',
67+
},
68+
},
69+
{
70+
value: 'user3',
71+
payload: {
72+
icon: UserOutlined,
73+
style: { backgroundColor: '#f56a00' },
74+
},
75+
},
76+
]);
77+
const options2 = ref([
78+
{
79+
value: 'spring',
80+
payload: {
81+
title: 'Spring',
82+
subTitle: 'Jan-Mar',
83+
},
84+
},
85+
{
86+
value: 'summer',
87+
payload: {
88+
title: 'Summer',
89+
subTitle: 'Apr-Jun',
90+
},
91+
},
92+
{
93+
value: 'autumn',
94+
payload: {
95+
title: 'Autumn',
96+
subTitle: 'Jul-Sept',
97+
},
98+
},
99+
{
100+
value: 'winter',
101+
payload: {
102+
title: 'Winter',
103+
subTitle: 'Oct-Dec',
104+
},
105+
},
87106
]);
107+
const value = ref('user1');
108+
const value2 = ref('spring');
88109
return {
89110
data,
90111
options2,
112+
value,
113+
value2,
91114
};
92115
},
93116
});

components/segmented/demo/disabled.vue

+7-3
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ Disabled Segmented.
1515
</docs>
1616
<template>
1717
<div>
18-
<a-segmented disabled :options="data" />
18+
<a-segmented v-model:value="value" disabled :options="data" />
1919
<br />
2020
<br />
21-
<a-segmented :options="data2" />
21+
<a-segmented v-model:value="value2" :options="data2" />
2222
</div>
2323
</template>
2424

2525
<script lang="ts">
26-
import { defineComponent, reactive } from 'vue';
26+
import { defineComponent, reactive, ref } from 'vue';
2727
2828
export default defineComponent({
2929
setup() {
@@ -35,9 +35,13 @@ export default defineComponent({
3535
{ value: 'Quarterly', disabled: true },
3636
'Yearly',
3737
]);
38+
const value = ref(data[0]);
39+
const value2 = ref('Daily');
3840
return {
3941
data,
4042
data2,
43+
value,
44+
value2,
4145
};
4246
},
4347
});

components/segmented/demo/dynamic.vue

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,28 @@ title:
1414
Load dynamically.
1515
</docs>
1616
<template>
17-
<a-segmented :options="data"></a-segmented>
17+
<a-segmented v-model:value="value" :options="data"></a-segmented>
1818
<br />
1919
<br />
20-
<a-button type="primary" @click="loadMore" :disabled="isDisabled">Load More</a-button>
20+
<a-button type="primary" :disabled="isDisabled" @click="loadMore">Load More</a-button>
2121
</template>
2222

2323
<script lang="ts">
2424
import { defineComponent, reactive, ref } from 'vue';
2525
export default defineComponent({
2626
setup() {
2727
const data = reactive(['Daily', 'Weekly', 'Monthly']);
28-
const isDisabled = ref<boolean>(false);
28+
const isDisabled = ref(false);
2929
const loadMore = () => {
3030
data.push(...['Quarterly', 'Yearly']);
3131
isDisabled.value = true;
3232
};
33+
const value = ref(data[0]);
3334
return {
3435
data,
3536
loadMore,
3637
isDisabled,
38+
value,
3739
};
3840
},
3941
});

components/segmented/demo/icon.vue

-49
This file was deleted.

0 commit comments

Comments
 (0)