-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathDaysOfWeekInput.tsx
141 lines (126 loc) · 3.6 KB
/
DaysOfWeekInput.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import React from 'react';
import {superdeskApi} from '../../../superdeskApi';
import {Row, LineInput, Label, Checkbox} from '../../UI/Form';
interface IProps {
field?: string; // defaults to 'dates.recurring_rule.byday'
value: string;
label: string; // defaults to 'Repeat On'
required?: boolean;
invalid?: boolean;
message?: string;
readOnly?: boolean;
boxed?: boolean;
noMargin?: boolean;
onChange(field: string, value: string): void;
}
interface IState {
MO: boolean;
TU: boolean;
WE: boolean;
TH: boolean;
FR: boolean;
SA: boolean;
SU: boolean;
}
export class DaysOfWeekInput extends React.Component<IProps, IState> {
constructor(props) {
super(props);
this.state = {
MO: false,
TU: false,
WE: false,
TH: false,
FR: false,
SA: false,
SU: false,
};
}
componentWillMount() {
this.setDays(this.props);
}
componentWillReceiveProps(nextProps) {
if (nextProps.value !== this.props.value) {
this.setDays(nextProps);
}
}
setDays(props: IProps) {
const days: IState = {
MO: false,
TU: false,
WE: false,
TH: false,
FR: false,
SA: false,
SU: false,
};
const value = props.value || '';
value.split(' ').forEach((day) => {
if (Object.keys(days).indexOf(day) > -1) {
days[day] = true;
}
});
this.setState(days);
}
onChange(value: boolean, day: keyof IState) {
const days: IState = {
...this.state,
[day]: value,
};
let daysInString = Object.keys(days)
// Keep only the checked days
.map((d) => days[d] ? d : null)
// Keep only defined values in array
.filter((d) => d)
// Join array to produce the string we want to store
.join(' ');
this.props.onChange(
this.props.field ?? 'dates.recurring_rule.byday',
daysInString
);
}
render() {
const {gettext} = superdeskApi.localization;
const {
label = gettext('Repeat On'),
readOnly,
invalid,
message,
} = this.props;
return (
<div>
<Label
row={true}
text={label}
invalid={invalid}
/>
<Row
flex={true}
noPadding={true}
>
{Object.keys(this.state).map((day: keyof IState) => (
<LineInput
key={day}
noLabel={true}
noMargin={true}
>
<Checkbox
value={this.state[day]}
onChange={(f, val) => this.onChange(val, day)}
labelPosition="inside"
label={day}
readOnly={readOnly}
/>
</LineInput>
))}
</Row>
{!invalid ? null : (
<LineInput
noLabel={true}
invalid={invalid}
message={message}
/>
)}
</div>
);
}
}