This repository has been archived by the owner on Aug 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
80 lines (72 loc) · 2.02 KB
/
index.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
import moment, { Moment } from 'moment';
import { Knapp } from 'nav-frontend-knapper';
import Modal from 'nav-frontend-modal';
import { Input } from 'nav-frontend-skjema';
import React, { Component } from 'react';
import DayPicker from 'react-day-picker';
import 'react-day-picker/lib/style.css';
import { DATE_FORMAT_INPUT_FIELD } from '../commonConstants';
import { setTime } from '../CreateSeason';
import { _CHOOSE_DATE } from './strings';
interface IProps {
label: string;
objectKey: string;
value: Moment;
setDate: (key: string, time: Moment) => void;
}
interface IState {
showModal: boolean;
}
class DateInputField extends Component<IProps, IState> {
constructor(props: IProps) {
super(props);
this.state = {
showModal: false,
};
}
public render() {
const { showModal } = this.state;
const { label, value } = this.props;
return (
<div className="date-picker-field" id={label}>
<Input
label={label}
readOnly={true}
value={value.format(DATE_FORMAT_INPUT_FIELD)}
bredde="XL"
/>
<Knapp onClick={this.toggleModal} className="choose-date-button">
{_CHOOSE_DATE}
</Knapp>
<Modal
isOpen={showModal}
onRequestClose={this.toggleModal}
closeButton={false}
contentLabel=""
>
<DayPicker
onDayClick={this.setDay}
selectedDays={value.toDate()}
month={value.toDate()}
/>
</Modal>
</div>
);
}
public componentDidMount = () => {
// React wants this for rendering reasons
Modal.setAppElement('body');
}
private setDay = (day: Date) => {
const { setDate, objectKey } = this.props;
const dateToMoment = setTime(moment(day));
// Sends data to parent to set state
setDate(objectKey, dateToMoment);
this.toggleModal();
}
private toggleModal = () => {
const { showModal } = this.state;
this.setState({ showModal: !showModal });
}
}
export default DateInputField;