-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCalendarField.js
243 lines (208 loc) · 8.55 KB
/
CalendarField.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import React, { useState, useCallback } from "react"
import cx from "classnames"
import PropTypes from "prop-types"
import get from "lodash.get"
import { Field, FormGroup, unwrapType, FieldMode, Icon, Addon, GlobalConfig, renderStaticField } from "domainql-form"
import config from "../config";
import i18n from "../i18n";
import CalendarModal from "./CalendarModal";
import { DateTime } from "luxon";
function toggleValue(open)
{
return !open;
}
const CalendarField = props => {
const { minDate, maxDate, addonClass = "btn-light", autoFocus, children, dateFormat = config.dateFormat, timestampFormat = config.timestampFormat,... fieldProps} = props;
const [ isOpen, setOpen] = useState(false);
const toggle = useCallback(
() => setOpen(toggleValue),
[]
);
return (
<Field
{... fieldProps}
fieldContext={ ctx => {
ctx.dateFormat = dateFormat;
ctx.timestampFormat = timestampFormat;
}}
addons={ Addon.filterAddons(children)}
>
{
(formConfig, ctx) => {
const { fieldType, mode, fieldId, inputClass, tooltip, path, qualifiedName, placeholder, addons } = ctx;
const errorMessages = formConfig.getErrors(qualifiedName);
const scalarType = unwrapType(fieldType).name;
const timestamp = get(formConfig.root, path);
const fieldValue = Field.getValue(formConfig, ctx, errorMessages);
//console.log("CalendarField value = ", fieldValue);
const buttonTitle = i18n("Open calendar");
let fieldElement;
if (mode === FieldMode.PLAIN_TEXT)
{
// we don't use renderStaticField to have the correct timestamp/date formatting
fieldElement = (
<span
id={ fieldId }
data-name={ qualifiedName }
className={
cx(
inputClass,
"form-control-plaintext"
)
}
>
{
fieldValue || GlobalConfig.none()
}
</span>
);
}
else
{
const disablePlaceholder = mode === FieldMode.READ_ONLY || mode === FieldMode.DISABLED;
let placeholderAttribute;
if (!disablePlaceholder) {
if (placeholder === true) {
placeholderAttribute = scalarType === "Date" ?
i18n("Date Format {0}", dateFormat) :
i18n("Timestamp Format {0}", timestampFormat);
} else {
placeholderAttribute = placeholder;
}
}
fieldElement = Addon.renderWithAddons(
<input
id={fieldId}
name={qualifiedName}
className={
cx(
inputClass,
"form-control",
errorMessages.length > 0 && "is-invalid"
)
}
type="text"
placeholder={placeholderAttribute}
disabled={mode === FieldMode.DISABLED}
title={tooltip}
readOnly={mode === FieldMode.READ_ONLY}
value={fieldValue}
onKeyPress={ ctx.handleKeyPress }
onChange={ctx.handleChange}
onBlur={ctx.handleBlur}
autoFocus={autoFocus ? true : null}
/>,
addons.concat(
<Addon placement={ Addon.RIGHT }>
<button
className={ cx("btn", addonClass) }
type="button"
title={ buttonTitle }
disabled={ mode !== FieldMode.NORMAL }
onClick={ () => setOpen(true) }
>
<Icon className="fa-calendar-check"/>
</button>
</Addon>
)
);
}
return (
<FormGroup
{ ... ctx }
formConfig={ formConfig }
errorMessages={ errorMessages }
>
{
fieldElement
}
<CalendarModal
ctx={ ctx }
formConfig={ formConfig }
isOpen={ isOpen }
toggle={ toggle }
name={ qualifiedName }
value={ timestamp }
scalarType={ scalarType }
minDate={ minDate }
maxDate={ maxDate }
/>
</FormGroup>
);
}
}
</Field>
);
};
CalendarField.propTypes = {
/**
* Minimum date the user can select
*/
minDate: PropTypes.instanceOf(DateTime),
/**
* Maximum date the user can select
*/
maxDate: PropTypes.instanceOf(DateTime),
// FIELD PROP TYPES
/**
* Name / path for the calendar field value (e.g. "name", but also "foos.0.name")
*/
name: PropTypes.string.isRequired,
/**
* Mode for this calendar field. If not set or set to null, the mode will be inherited from the <Form/> or <FormBlock>.
*/
mode: PropTypes.oneOf(FieldMode.values()),
/**
* Additional help text for this field. Is rendered for non-erroneous fields in place of the error. If a function
* is given, it should be a stable reference ( e.g. with useCallback()) to prevent creating the field context over
* and over. The same considerations apply to using elements. ( The expression <Bla/> recreates that element on every
* invocation, use static element references)
*/
helpText: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.element]),
/**
* Tooltip / title attribute for the input element
*/
tooltip: PropTypes.string,
/**
* Label for the field.
*/
label: PropTypes.string,
/**
* Placeholder text to render for text inputs or true for a default placeholder.
* By default no placeholder is rendered.
*/
placeholder: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
/**
* Additional HTML classes for the input element.
*/
inputClass: PropTypes.string,
/**
* Additional HTML classes for the label element.
*/
labelClass: PropTypes.string,
/**
* Additional HTML classes for the form group element.
*/
formGroupClass: PropTypes.string,
/**
* Additional HTML classes for the calendar button addon
*/
addonClass: PropTypes.string,
/**
* Date format string to use for "Date" typed fields.
*/
dateFormat: PropTypes.string,
/**
* Pass-through autoFocus attribute for the Calendar input field
*/
autoFocus: PropTypes.bool,
/**
* Date format string to use for "Timestamp" typed fields.
*/
timestampFormat: PropTypes.string,
/**
* Optional local on-change handler ( ({oldValue, fieldContext}, value) => ... )
*/
onChange: PropTypes.func
};
export default CalendarField;