Repository navigation
Expand file tree
/
Copy pathdate-range-fieldview.js
More file actions
69 lines (67 loc) · 1.79 KB
/
Copy pathdate-range-fieldview.js
File metadata and controls
69 lines (67 loc) · 1.79 KB
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
const {
input,
div,
text,
script,
domReady,
text_attr,
} = require("@saltcorn/markup/tags");
const flatpickr_date_range = {
type: "Date",
isFilter: true,
isEdit: false,
configFields: [
{
name: "placeholder",
label: "Placeholder",
type: "String",
},
{
name: "future_only",
label: "Future dates only",
type: "Bool",
},
{
name: "dateFormat",
label: "Date format",
required: true,
type: "String",
default: "Y-m-d",
sublabel: `<a href="https://flatpickr.js.org/formatting/">Formatting options</a>`,
},
],
run: (nm, v, attrs = {}, cls, required, field, state = {}) => {
const rndid = Math.floor(Math.random() * 16777215).toString(16);
const opts = {
mode: "range",
dateFormat: "Y-m-d",
altInput: !!attrs.dateFormat,
altFormat: attrs.dateFormat || undefined,
minDate: attrs?.future_only ? "today" : undefined,
//maxDate: attrs.maxDate,
locale: attrs.locale,
defaultDate: [state[`_fromdate_${nm}`], state[`_todate_${nm}`]],
};
return (
input({
type: "text",
class: ["form-control", cls],
placeholder: attrs.placeholder,
disabled: attrs.disabled,
id: `input${text_attr(nm)}${rndid}`,
}) +
script(
domReady(
`$('#input${text(nm)}${rndid}').flatpickr({
...${JSON.stringify(opts)},
onChange: function(selectedDates, dateStr, instance) {
if(selectedDates.length==2)
set_state_fields({_fromdate_${nm}: selectedDates[0].toLocaleDateString('en-CA'), _todate_${nm}: selectedDates[1].toLocaleDateString('en-CA') })
},
});`
)
)
);
},
};
module.exports = flatpickr_date_range;