Skip to content

Commit f8ffa46

Browse files
committed
feat: Calendar 组件新增选择年份功能
1 parent ca386b9 commit f8ffa46

File tree

5 files changed

+135
-85
lines changed

5 files changed

+135
-85
lines changed

.idea/watcherTasks.xml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+29-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/calendar/h5/index.tsx

+57-31
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import ClText from "../../text";
77
import ClFlex from "../../flex";
88
import ClLayout from "../../layout";
99
import ClCard from "../../card";
10+
import ClIcon from "../../icon";
1011
import { classNames } from "../../../lib";
1112
import { BG_COLOR_LIST, TEXT_COLOR_LIST } from "../../../lib/model";
1213
import ClButton from "../../button";
@@ -27,6 +28,7 @@ interface IState {
2728
currentActiveLines: number;
2829
showMonths: boolean;
2930
today: string;
31+
currentYear: number;
3032
}
3133

3234
const FORMAT_DATE = "YYYY-MM-DD";
@@ -59,6 +61,7 @@ export default class Calendar_h5 extends Taro.Component<IProps, IState> {
5961
thisMonth: [],
6062
nextMonth: [],
6163
preMonth: [],
64+
currentYear: dayjs().year(),
6265
chooseDate: today,
6366
currentDate: dealYearMonth(dayjs(today)),
6467
currentActive: 1,
@@ -68,11 +71,12 @@ export default class Calendar_h5 extends Taro.Component<IProps, IState> {
6871
} as IState;
6972
}
7073
selectDate(month) {
71-
const year = this.state.currentDate.split("年")[0];
74+
const year = this.state.currentYear;
7275
const date = `${year}-${month + 1}-01`;
7376
this.changeMonth(date);
7477
this.setState({
75-
showMonths: false
78+
showMonths: false,
79+
currentDate: dealYearMonth(dayjs(date))
7680
});
7781
}
7882
clickDate({ item }) {
@@ -145,7 +149,7 @@ export default class Calendar_h5 extends Taro.Component<IProps, IState> {
145149
}
146150
return current;
147151
}
148-
changeDate() {
152+
changeDate(e) {
149153
let arrayIndex = e.detail.current;
150154
if (render) {
151155
arrayIndex = 1;
@@ -477,7 +481,8 @@ export default class Calendar_h5 extends Taro.Component<IProps, IState> {
477481
nextMonth,
478482
showMonths,
479483
currentDate,
480-
currentActiveLines
484+
currentActiveLines,
485+
currentYear
481486
} = this.state;
482487
const {
483488
highlightWeekend,
@@ -515,7 +520,6 @@ export default class Calendar_h5 extends Taro.Component<IProps, IState> {
515520
));
516521
const BGClassName = BG_COLOR_LIST[activeColor || "blue"];
517522
const oldTextClassName = TEXT_COLOR_LIST["gray"];
518-
console.log('render')
519523
const weeksComponent = preWeek.length
520524
? [preWeek, thisWeek, nextWeek].map((week, index) => (
521525
<SwiperItem key={week[3].date}>
@@ -709,36 +713,58 @@ export default class Calendar_h5 extends Taro.Component<IProps, IState> {
709713
<ClFlex justify={"between"} align={"center"}>
710714
<ClTip
711715
renderMessage={
712-
<ClGrid col={4}>
713-
{months.map((item, index) => (
714-
<View
715-
key={item}
716-
className={classNames([
717-
"flex justify-center align-center"
718-
])}
719-
>
720-
<ClLayout margin={"small"} marginDirection={"vertical"}>
721-
<ClButton
722-
size={"small"}
723-
bgColor={"light-gray"}
724-
shadow={false}
725-
long
726-
text={item}
727-
onClick={() => {
728-
this.selectDate(index);
729-
}}
730-
/>
731-
</ClLayout>
732-
</View>
733-
))}
734-
</ClGrid>
716+
<View>
717+
<ClLayout>
718+
<ClFlex align={"center"} justify={"between"}>
719+
<View onClick={() => {
720+
this.setState({
721+
currentYear: currentYear - 1
722+
})
723+
}}>
724+
<ClIcon iconName={"pullleft"} />
725+
</View>
726+
<ClText text={currentYear + ""} />
727+
<View onClick={() => {
728+
this.setState({
729+
currentYear: currentYear + 1
730+
})
731+
}}>
732+
<ClIcon iconName={"pullright"} />
733+
</View>
734+
</ClFlex>
735+
</ClLayout>
736+
<ClGrid col={4}>
737+
{months.map((item, index) => (
738+
<View
739+
key={item}
740+
className={classNames([
741+
"flex justify-center align-center"
742+
])}
743+
>
744+
<ClLayout margin={"small"} marginDirection={"vertical"}>
745+
<ClButton
746+
size={"small"}
747+
bgColor={"light-gray"}
748+
shadow={false}
749+
long
750+
text={item}
751+
onClick={() => {
752+
this.selectDate(index);
753+
}}
754+
/>
755+
</ClLayout>
756+
</View>
757+
))}
758+
</ClGrid>
759+
</View>
735760
}
736761
mode={"click"}
737762
width={300}
738763
show={this.state.showMonths}
739764
onChange={() => {
765+
const currentShow = !showMonths
740766
this.setState({
741-
showMonths: !showMonths
767+
showMonths: currentShow
742768
});
743769
}}
744770
>
@@ -783,8 +809,8 @@ export default class Calendar_h5 extends Taro.Component<IProps, IState> {
783809
? pxTransform(120)
784810
: pxTransform(120 * currentActiveLines)
785811
}}
786-
onChange={() => {
787-
this.changeDate.bind(this)
812+
onChange={(e) => {
813+
if (this.state.currentActive !== e.detail.current) this.changeDate(e)
788814
}}
789815
>
790816
{calendarType === "week" ? weeksComponent : monthsComponent}

0 commit comments

Comments
 (0)