Skip to content

Commit 45feeeb

Browse files
599316527Justineo
andcommitted
docs: update calendar/date-class demo (#23)
Co-authored-by: GU Yiling <[email protected]>
1 parent 4598e30 commit 45feeeb

File tree

3 files changed

+172
-3
lines changed

3 files changed

+172
-3
lines changed

one/docs/components/calendar.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444

4545
[[ demo src="/demo/calendar/date-class.vue" ]]
4646

47+
### 自定义日期内容
48+
49+
通过 [`before`](#slots-before) 插槽来自定义前置内容。
50+
51+
[[ demo src="/demo/calendar/slots.vue" ]]
52+
4753
## API
4854

4955
### 属性

one/docs/demo/calendar/date-class.vue

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,117 @@
22
<article>
33
<section>
44
<veui-calendar
5-
date-class="custom-date"
5+
class="demo-date-class"
6+
:date-class="holidayClass"
67
/>
78
</section>
89
</article>
910
</template>
1011

1112
<script>
13+
import { isString, inRange } from 'lodash'
1214
import { Calendar } from 'veui'
1315
16+
const holidays = [
17+
[true, ['2022-1-1', '2022-1-3']],
18+
19+
[true, ['2022-1-31', '2022-2-6']],
20+
[false, ['2022-1-29', '2022-1-30']],
21+
22+
[true, ['2022-4-3', '2022-4-5']],
23+
[false, '2022-4-2'],
24+
25+
[true, ['2022-4-30', '2022-5-4']],
26+
[false, '2022-4-24', '2022-5-7'],
27+
28+
[true, ['2022-6-3', '2022-6-5']],
29+
30+
[true, ['2022-9-10', '2022-9-12']],
31+
32+
[true, ['2022-10-1', '2022-10-7']],
33+
[false, ['2022-10-8', '2022-10-9']]
34+
]
35+
1436
export default {
1537
components: {
1638
'veui-calendar': Calendar
39+
},
40+
computed: {
41+
isHoliday () {
42+
const items = holidays.map(function ([isHoliday, ...dateRanges]) {
43+
dateRanges = dateRanges.map(range => isString(range) ? [range, range] : range)
44+
.map(([start, end]) => [parseDateString(start), parseDateString(end, true)])
45+
return [isHoliday, dateRanges]
46+
})
47+
return function (date) {
48+
const time = date.getTime()
49+
const match = items.find(([, ranges]) => ranges.some(([start, end]) => inRange(time, start, end)))
50+
return match ? match[0] : undefined
51+
}
52+
}
53+
},
54+
methods: {
55+
holidayClass (date) {
56+
const r = this.isHoliday(date)
57+
if (r !== undefined) {
58+
return r ? 'x-holiday' : 'x-workday'
59+
}
60+
return [0, 6].includes(date.getDay()) ? 'x-weekend' : undefined
61+
}
1762
}
1863
}
64+
65+
function parseDateString (str, isEnd) {
66+
const [year, month, day] = str.split('-')
67+
return new Date(year, month - 1, isEnd ? +day + 1 : day, 0, 0, 0).getTime()
68+
}
1969
</script>
2070

2171
<style lang="less">
22-
.custom-date {
23-
font-style: italic;
72+
.demo-date-class {
73+
.x-holiday button,
74+
.x-workday button {
75+
position: relative;
76+
border: 1px solid red;
77+
78+
&::before {
79+
content: "";
80+
position: absolute;
81+
top: -1px;
82+
right: -1px;
83+
width: 0;
84+
height: 0;
85+
border-style: solid;
86+
border-width: 0 20px 20px 0;
87+
border-color: transparent #ff2600 transparent transparent;
88+
89+
}
90+
&::after {
91+
content: "";
92+
position: absolute;
93+
top: -1px;
94+
right: -1px;
95+
font-size: 9px;
96+
font-weight: bold;
97+
color: white;
98+
}
99+
}
100+
.x-workday button {
101+
border: 1px solid blue;
102+
&::before {
103+
border-color: transparent blue transparent transparent;
104+
}
105+
&::after {
106+
content: "";
107+
}
108+
}
109+
.x-weekend button {
110+
opacity: .6;
111+
}
112+
.veui-calendar-aux {
113+
button {
114+
display: none;
115+
}
116+
}
24117
}
25118
</style>

one/docs/demo/calendar/slots.vue

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<template>
2+
<article>
3+
<veui-calendar class="default" v-model="date">
4+
<template #before>
5+
<div class="before">
6+
<veui-button ui="s basic" @click="pickDay(-1)">Yesterday</veui-button>
7+
<veui-button ui="s basic" @click="pickDay(0)">Today</veui-button>
8+
<veui-button ui="s basic" @click="pickDay(1)">Tomorrow</veui-button>
9+
</div>
10+
</template>
11+
<template #date="d">
12+
<sup v-if="d.date === 1" class="month-tip">{{ formatMonth(d) }}</sup>
13+
{{ d.date }}
14+
</template>
15+
</veui-calendar>
16+
</article>
17+
</template>
18+
19+
<script>
20+
import { Calendar, Button } from 'veui'
21+
22+
const formatter = new Intl.DateTimeFormat('zh-CN', { month: 'long' })
23+
24+
export default {
25+
components: {
26+
'veui-calendar': Calendar,
27+
'veui-button': Button
28+
},
29+
data () {
30+
return {
31+
date: new Date()
32+
}
33+
},
34+
methods: {
35+
pickDay (d) {
36+
let date = new Date()
37+
date.setDate(date.getDate() + d)
38+
this.date = date
39+
},
40+
formatMonth ({ year, month, date }) {
41+
return formatter.format(new Date(year, month, date))
42+
}
43+
}
44+
}
45+
</script>
46+
47+
<style lang="less" scoped>
48+
.default {
49+
/deep/ .veui-calendar-day,
50+
/deep/ .veui-calendar-aux {
51+
button {
52+
position: relative;
53+
}
54+
}
55+
}
56+
.month-tip {
57+
position: absolute;
58+
top: -1px;
59+
left: -2px;
60+
right: -2px;
61+
text-align: center;
62+
color: #848b99;
63+
}
64+
.before {
65+
padding: 4px;
66+
border-bottom: 1px solid #d8d8d8;
67+
display: flex;
68+
justify-content: space-around;
69+
}
70+
</style>

0 commit comments

Comments
 (0)