-
Notifications
You must be signed in to change notification settings - Fork 542
/
InputDateFabric.tsx
63 lines (54 loc) · 2.05 KB
/
InputDateFabric.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as React from "react";
import * as FabricUI from "office-ui-fabric-react";
import * as AC from "adaptivecards";
import * as Shared from "../../Utils/shared";
export class InputDateFabric extends Shared.ReactInputElement {
public value: string;
private date: Date;
private placeholder: string;
private minDate: Date;
private maxDate: Date;
protected renderReact = (): JSX.Element => {
return this.buildDatePicker();
}
public getJsonTypeName = (): string => "Input.Date";
public parse = (json: any, errors?: AC.IValidationError[]) => {
this.id = AC.getStringValue(json.id);
this.placeholder = AC.getStringValue(json.placeholder);
this.parseDates(json, errors);
}
private parseDates = (json: any, errors?: AC.IValidationError[]) => {
const dateString = AC.getStringValue(json.value);
this.value = dateString;
this.date = dateString ? this.getDate(dateString) : new Date();
this.minDate = json.min ? this.getDate(AC.getStringValue(json.min)) : undefined;
this.maxDate = json.max ? this.getDate(AC.getStringValue(json.max)) : undefined;
}
private getDate = (dateString: string, errors?: AC.IValidationError[]): Date => {
try {
return new Date(dateString);
} catch (error) {
Shared.raiseParseError({
error: AC.ValidationError.InvalidPropertyValue,
message: error,
}, errors);
return undefined;
}
}
private buildDatePicker = () => {
return (<FabricUI.DatePicker
id={this.id}
onSelectDate={this.handleSelectDate}
value={this.date}
placeholder={this.placeholder}
allowTextInput={true}
minDate={this.minDate}
maxDate={this.maxDate}
/>);
}
private handleSelectDate = (date: Date) => {
this.value = date.toLocaleDateString("en-US");
}
}