-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Calendar.Button.Example.tsx
106 lines (95 loc) · 3.41 KB
/
Calendar.Button.Example.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import * as React from 'react';
import { DefaultButton, FocusTrapZone, Callout, DirectionalHint } from 'office-ui-fabric-react';
import { Calendar, DayOfWeek, defaultDayPickerStrings } from '@uifabric/date-time';
export interface ICalendarButtonExampleState {
showCalendar: boolean;
selectedDate: Date | null;
}
export interface ICalendarButtonExampleProps {
isDayPickerVisible?: boolean;
isMonthPickerVisible?: boolean;
highlightCurrentMonth?: boolean;
highlightSelectedMonth?: boolean;
buttonString?: string;
showMonthPickerAsOverlay?: boolean;
showGoToToday?: boolean;
}
export class CalendarButtonExample extends React.Component<ICalendarButtonExampleProps, ICalendarButtonExampleState> {
public static defaultProps: ICalendarButtonExampleProps = {
showMonthPickerAsOverlay: false,
isDayPickerVisible: true,
isMonthPickerVisible: true,
showGoToToday: true,
buttonString: 'Click for Calendar'
};
private _calendarButtonElement: HTMLElement;
public constructor(props: ICalendarButtonExampleProps) {
super(props);
this.state = {
showCalendar: false,
selectedDate: null
};
this._onClick = this._onClick.bind(this);
this._onDismiss = this._onDismiss.bind(this);
this._onSelectDate = this._onSelectDate.bind(this);
}
public render(): JSX.Element {
return (
<div>
<div ref={(calendarBtn: HTMLDivElement) => (this._calendarButtonElement = calendarBtn!)}>
<DefaultButton
onClick={this._onClick}
text={!this.state.selectedDate ? this.props.buttonString : this.state.selectedDate.toLocaleDateString()}
/>
</div>
{this.state.showCalendar && (
<Callout
isBeakVisible={false}
className="ms-DatePicker-callout"
gapSpace={0}
doNotLayer={false}
target={this._calendarButtonElement}
directionalHint={DirectionalHint.bottomLeftEdge}
onDismiss={this._onDismiss}
setInitialFocus={true}
>
<FocusTrapZone isClickableOutsideFocusTrap={true}>
<Calendar
onSelectDate={this._onSelectDate}
onDismiss={this._onDismiss}
isMonthPickerVisible={this.props.isMonthPickerVisible}
value={this.state.selectedDate!}
firstDayOfWeek={DayOfWeek.Sunday}
strings={defaultDayPickerStrings}
isDayPickerVisible={this.props.isDayPickerVisible}
highlightCurrentMonth={this.props.highlightCurrentMonth}
highlightSelectedMonth={this.props.highlightSelectedMonth}
showMonthPickerAsOverlay={this.props.showMonthPickerAsOverlay}
showGoToToday={this.props.showGoToToday}
/>
</FocusTrapZone>
</Callout>
)}
</div>
);
}
private _onClick(): void {
this.setState((prevState: ICalendarButtonExampleState) => {
prevState.showCalendar = !prevState.showCalendar;
return prevState;
});
}
private _onDismiss(): void {
this.setState((prevState: ICalendarButtonExampleState) => {
prevState.showCalendar = false;
return prevState;
});
}
private _onSelectDate(date: Date): void {
this.setState((prevState: ICalendarButtonExampleState) => {
prevState.showCalendar = false;
prevState.selectedDate = date;
return prevState;
});
}
}