@gpa-gemstone/react-forms 1.1.24 → 1.1.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/DateRangePicker.d.ts +5 -2
- package/lib/DateRangePicker.js +49 -88
- package/package.json +2 -2
package/lib/DateRangePicker.d.ts
CHANGED
|
@@ -4,7 +4,10 @@ export default function DateRangePicker<T>(props: {
|
|
|
4
4
|
FromField: keyof T;
|
|
5
5
|
ToField: keyof T;
|
|
6
6
|
Setter: (record: T) => void;
|
|
7
|
-
Disabled?: boolean;
|
|
8
7
|
Label: string;
|
|
9
|
-
Valid
|
|
8
|
+
Valid: (fieldFrom: keyof T, fieldTo: keyof T) => boolean;
|
|
9
|
+
Disabled?: boolean;
|
|
10
|
+
Feedback?: string;
|
|
11
|
+
Format?: string;
|
|
12
|
+
Type?: ('datetime-local' | 'date');
|
|
10
13
|
}): JSX.Element;
|
package/lib/DateRangePicker.js
CHANGED
|
@@ -34,47 +34,38 @@ var __assign = (this && this.__assign) || function () {
|
|
|
34
34
|
};
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
var React = require("react");
|
|
37
|
+
var moment = require("moment");
|
|
37
38
|
function DateRangePicker(props) {
|
|
38
|
-
|
|
39
|
-
var
|
|
40
|
-
var
|
|
41
|
-
|
|
42
|
-
var
|
|
39
|
+
// Range box vars, need a secondary var to avoid looping react hooks
|
|
40
|
+
var _a = React.useState('Custom'), formRange = _a[0], setFormRange = _a[1];
|
|
41
|
+
var _b = React.useState('Custom'), range = _b[0], setRange = _b[1];
|
|
42
|
+
// Tracks weather or not props.Record changes are due to internal input boxes or externally
|
|
43
|
+
var _c = React.useState(false), internal = _c[0], setInternal = _c[1];
|
|
44
|
+
// Adds a buffer between the outside props and what the box is reading to prevent box overwriting every render with a keystroke
|
|
45
|
+
var _d = React.useState(ParseRecord()), boxRecord = _d[0], setBoxRecord = _d[1];
|
|
46
|
+
// Formats that will be used for dateBoxes
|
|
47
|
+
var boxFormat = "YYYY-MM-DD" + (props.Type === undefined || props.Type === 'date' ? "" : "[T]hh:mm:ss");
|
|
48
|
+
var recordFormat = props.Format !== undefined ? props.Format : "YYYY-MM-DD" + (props.Type === undefined || props.Type === 'date' ? "" : "[T]hh:mm:ss.SSS[Z]");
|
|
43
49
|
React.useEffect(function () {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
setTstart(propsStart);
|
|
49
|
-
}
|
|
50
|
-
else if (propsStart.getTime() !== Tstart.getTime())
|
|
51
|
-
setTstart(propsStart);
|
|
52
|
-
if (propsEnd == null || Tend == null) {
|
|
53
|
-
if (!(propsEnd == null && Tend == null))
|
|
54
|
-
setTstart(propsEnd);
|
|
55
|
-
}
|
|
56
|
-
else if (propsEnd.getTime() !== Tend.getTime())
|
|
57
|
-
setTend(propsEnd);
|
|
50
|
+
setRange(ToRange(moment(props.Record[props.ToField], recordFormat).diff(moment(props.Record[props.FromField], recordFormat), 'days')));
|
|
51
|
+
if (!internal)
|
|
52
|
+
setBoxRecord(ParseRecord());
|
|
53
|
+
setInternal(false);
|
|
58
54
|
}, [props.Record]);
|
|
59
55
|
React.useEffect(function () {
|
|
60
|
-
var
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
setTstart(new Date(new Date().valueOf() - 1000 * 24 * 60 * 60 * days));
|
|
74
|
-
setTend(new Date());
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}, [range]);
|
|
56
|
+
var _a, _b;
|
|
57
|
+
setRange(formRange);
|
|
58
|
+
var toTime = moment(props.Record[props.FromField], recordFormat).add(GetDays(formRange), 'days');
|
|
59
|
+
props.Setter(__assign(__assign({}, props.Record), (_a = {}, _a[props.ToField] = toTime.format(recordFormat), _a)));
|
|
60
|
+
setBoxRecord(__assign(__assign({}, boxRecord), (_b = {}, _b[props.ToField] = toTime.format(boxFormat), _b)));
|
|
61
|
+
}, [formRange]);
|
|
62
|
+
function ParseRecord() {
|
|
63
|
+
var record = __assign({}, props.Record);
|
|
64
|
+
var ParseExternalField = function (field) { return props.Record[field] === null ? '' : moment(props.Record[field], recordFormat).format(boxFormat); };
|
|
65
|
+
record[props.ToField] = ParseExternalField(props.ToField);
|
|
66
|
+
record[props.FromField] = ParseExternalField(props.FromField);
|
|
67
|
+
return record;
|
|
68
|
+
}
|
|
78
69
|
function GetDays(val) {
|
|
79
70
|
if (val === '1 Day')
|
|
80
71
|
return 1;
|
|
@@ -90,47 +81,6 @@ function DateRangePicker(props) {
|
|
|
90
81
|
return 365;
|
|
91
82
|
return 0;
|
|
92
83
|
}
|
|
93
|
-
function ToDate(str) {
|
|
94
|
-
if (str === null)
|
|
95
|
-
return null;
|
|
96
|
-
var dt = new Date(str);
|
|
97
|
-
if (isNaN(dt.getTime()))
|
|
98
|
-
return null;
|
|
99
|
-
return dt;
|
|
100
|
-
}
|
|
101
|
-
React.useEffect(function () {
|
|
102
|
-
if (Tstart != null)
|
|
103
|
-
setStartInput(ToString(Tstart));
|
|
104
|
-
}, [Tstart]);
|
|
105
|
-
React.useEffect(function () {
|
|
106
|
-
// only if InputStart is a valid ToString
|
|
107
|
-
if (StartInput.match('^([0-9][0-9][0-9][0-9])-([0-1][0-9])-([0-3][0-9])') != null)
|
|
108
|
-
setTstart(ToDate(StartInput));
|
|
109
|
-
else
|
|
110
|
-
setTstart(null);
|
|
111
|
-
}, [StartInput]);
|
|
112
|
-
React.useEffect(function () {
|
|
113
|
-
if (Tend != null)
|
|
114
|
-
setEndInput(ToString(Tend));
|
|
115
|
-
}, [Tend]);
|
|
116
|
-
React.useEffect(function () {
|
|
117
|
-
// only if EndInput is a valid ToString
|
|
118
|
-
if (EndInput.match('^([0-9][0-9][0-9][0-9])-([0-1][0-9])-([0-3][0-9])') != null)
|
|
119
|
-
setTend(ToDate(EndInput));
|
|
120
|
-
else
|
|
121
|
-
setTend(null);
|
|
122
|
-
}, [EndInput]);
|
|
123
|
-
function UpdateTime() {
|
|
124
|
-
var _a;
|
|
125
|
-
var to = (Tend !== null ? ToString(Tend) : '');
|
|
126
|
-
var from = (Tstart !== null ? ToString(Tstart) : '');
|
|
127
|
-
var record = __assign(__assign({}, props.Record), (_a = {}, _a[props.ToField] = to, _a[props.FromField] = from, _a));
|
|
128
|
-
props.Setter(record);
|
|
129
|
-
}
|
|
130
|
-
function ToString(dt) {
|
|
131
|
-
return dt.getUTCFullYear() + "-" + (dt.getUTCMonth() + 1).toString()
|
|
132
|
-
.padStart(2, '0') + "-" + dt.getUTCDate().toString().padStart(2, '0');
|
|
133
|
-
}
|
|
134
84
|
function ToRange(days) {
|
|
135
85
|
if (days === 1)
|
|
136
86
|
return ('1 Day');
|
|
@@ -147,15 +97,28 @@ function DateRangePicker(props) {
|
|
|
147
97
|
else
|
|
148
98
|
return ('Custom');
|
|
149
99
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
100
|
+
function dateBox(field) {
|
|
101
|
+
return React.createElement("div", { className: "col" },
|
|
102
|
+
React.createElement("input", { className: "form-control" + (props.Valid(props.FromField, props.ToField) ? '' : ' is-invalid'), type: props.Type === undefined ? 'date' : props.Type, onChange: function (evt) {
|
|
103
|
+
var _a;
|
|
104
|
+
var record = __assign({}, props.Record);
|
|
105
|
+
if (evt.target.value !== '')
|
|
106
|
+
record[field] = moment(evt.target.value, boxFormat).format(recordFormat);
|
|
107
|
+
else
|
|
108
|
+
record[field] = null;
|
|
109
|
+
// These two updates should be batched together
|
|
110
|
+
props.Setter(record);
|
|
111
|
+
setBoxRecord(__assign(__assign({}, boxRecord), (_a = {}, _a[field] = evt.target.value, _a)));
|
|
112
|
+
setInternal(true);
|
|
113
|
+
}, value: boxRecord[field], disabled: props.Disabled === undefined ? false : props.Disabled }),
|
|
114
|
+
field !== props.FromField ? null :
|
|
115
|
+
React.createElement("div", { className: "invalid-feedback" }, props.Feedback === undefined ? 'From and to dates required, and from must be before to.' : props.Feedback));
|
|
116
|
+
}
|
|
154
117
|
return (React.createElement("div", { className: "form-group" },
|
|
155
|
-
React.createElement("label", null, props.Label),
|
|
118
|
+
props.Label === "" ? null : React.createElement("label", null, props.Label),
|
|
156
119
|
React.createElement("div", { className: "row" },
|
|
157
120
|
React.createElement("div", { className: "col" },
|
|
158
|
-
React.createElement("select", { className: "form-control", value: range, onChange: function (evt) { return
|
|
121
|
+
React.createElement("select", { className: "form-control", value: range, onChange: function (evt) { return setFormRange(evt.target.value); } },
|
|
159
122
|
React.createElement("option", { value: "Custom" }, "Custom"),
|
|
160
123
|
React.createElement("option", { value: "1 Day" }, "1 Day"),
|
|
161
124
|
React.createElement("option", { value: "7 Days" }, "7 Days"),
|
|
@@ -163,9 +126,7 @@ function DateRangePicker(props) {
|
|
|
163
126
|
React.createElement("option", { value: "90 Days" }, "90 Days"),
|
|
164
127
|
React.createElement("option", { value: "180 Days" }, "180 Days"),
|
|
165
128
|
React.createElement("option", { value: "365 Days" }, "365 Days"))),
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
React.createElement("div", { className: "col" },
|
|
169
|
-
React.createElement("input", { className: "form-control" + (endValid ? '' : ' is-invalid'), type: "date", onChange: function (evt) { setEndInput(evt.target.value); }, value: EndInput, disabled: props.Disabled == null ? false : props.Disabled })))));
|
|
129
|
+
dateBox(props.FromField),
|
|
130
|
+
dateBox(props.ToField))));
|
|
170
131
|
}
|
|
171
132
|
exports.default = DateRangePicker;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gpa-gemstone/react-forms",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.27",
|
|
4
4
|
"description": "React Form modules for gpa webapps",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"@gpa-gemstone/helper-functions": "0.0.16",
|
|
49
49
|
"@types/react": "^17.0.14",
|
|
50
50
|
"@types/styled-components": "^5.1.11",
|
|
51
|
-
"react": "^
|
|
51
|
+
"react": "^18.2.0",
|
|
52
52
|
"styled-components": "5.3.3",
|
|
53
53
|
"moment": "2.29.4"
|
|
54
54
|
},
|