react-form-builder-more-flexibility 1.0.12 → 1.0.14

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.
@@ -21,6 +21,24 @@ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (O
21
21
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
22
22
  function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
23
23
  function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
24
+ // This is the utility function that will attempt to parse a date string
25
+ // using several different formats until it finds one that works.
26
+ function parseDate(dateStr, formatMask) {
27
+ var formats = ['yyyy-MM-dd',
28
+ // format updated
29
+ 'MM/dd/yyyy', formatMask
30
+ // add any other formats you might need to support
31
+ ];
32
+
33
+ var date = formats.find(function (newFormat) {
34
+ var parsedDate = (0, _dateFns.parse)(dateStr, newFormat, new Date());
35
+ return !Number.isNaN(parsedDate.getTime());
36
+ });
37
+ if (date) {
38
+ return (0, _dateFns.parse)(dateStr, date, new Date());
39
+ }
40
+ return null;
41
+ }
24
42
  var DatePicker = /*#__PURE__*/function (_React$Component) {
25
43
  (0, _inherits2["default"])(DatePicker, _React$Component);
26
44
  var _super = _createSuper(DatePicker);
@@ -28,21 +46,29 @@ var DatePicker = /*#__PURE__*/function (_React$Component) {
28
46
  var _this;
29
47
  (0, _classCallCheck2["default"])(this, DatePicker);
30
48
  _this = _super.call(this, props);
31
- // formatMask = '';
32
49
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleChange", function (dt) {
33
50
  var placeholder;
34
51
  var formatMask = _this.state.formatMask;
52
+ var iOSFormatMask = 'yyyy-MM-dd';
35
53
  if (dt && dt.target) {
36
54
  placeholder = dt && dt.target && dt.target.value === '' ? formatMask.toLowerCase() : '';
37
- var formattedDate = dt.target.value ? (0, _dateFns.format)(dt.target.value, formatMask) : '';
38
- _this.setState({
39
- value: formattedDate,
40
- internalValue: formattedDate,
41
- placeholder: placeholder
42
- });
55
+ var dateValue = dt.target.value ? parseDate(dt.target.value, formatMask) : null;
56
+ if (dateValue && Number.isNaN(dateValue)) {
57
+ console.log('Invalid date:', dt.target.value);
58
+ } else {
59
+ var formattedDate = dateValue ? (0, _dateFns.format)(dateValue, iOSFormatMask) : '';
60
+ _this.setState({
61
+ value: formattedDate,
62
+ internalValue: dateValue,
63
+ placeholder: placeholder
64
+ });
65
+ }
66
+ } else if (dt && Number.isNaN(dt)) {
67
+ console.log('Invalid date:', dt);
43
68
  } else {
69
+ var _formattedDate = dt ? (0, _dateFns.format)(dt, iOSFormatMask) : '';
44
70
  _this.setState({
45
- value: dt ? (0, _dateFns.format)(dt, formatMask) : '',
71
+ value: _formattedDate,
46
72
  internalValue: dt,
47
73
  placeholder: placeholder
48
74
  });
@@ -51,9 +77,14 @@ var DatePicker = /*#__PURE__*/function (_React$Component) {
51
77
  _this.inputField = /*#__PURE__*/_react["default"].createRef();
52
78
  var _DatePicker$updateFor = DatePicker.updateFormat(props, null),
53
79
  _formatMask = _DatePicker$updateFor.formatMask;
54
- _this.state = DatePicker.updateDateTime(props, {
80
+ var state = DatePicker.updateDateTime(props, {
55
81
  formatMask: _formatMask
56
82
  }, _formatMask);
83
+
84
+ // Ensure state.value and state.internalValue are not undefined
85
+ state.value = state.value || '';
86
+ state.internalValue = state.internalValue || '';
87
+ _this.state = state;
57
88
  return _this;
58
89
  }
59
90
  (0, _createClass2["default"])(DatePicker, [{
@@ -96,9 +127,6 @@ var DatePicker = /*#__PURE__*/function (_React$Component) {
96
127
  name: props.name,
97
128
  ref: props.ref,
98
129
  onChange: this.handleChange,
99
- dateFormat: "MM/DD/YYYY"
100
- // placeholder={this.state.placeholder}
101
- ,
102
130
  value: this.state.value,
103
131
  className: "form-control"
104
132
  }), !iOS && !readOnly && /*#__PURE__*/_react["default"].createElement(_reactDatepicker["default"], {
@@ -119,54 +147,50 @@ var DatePicker = /*#__PURE__*/function (_React$Component) {
119
147
  }))));
120
148
  }
121
149
  }], [{
122
- key: "updateFormat",
123
- value: function updateFormat(props, oldFormatMask) {
124
- var _props$data = props.data,
125
- showTimeSelect = _props$data.showTimeSelect,
126
- showTimeSelectOnly = _props$data.showTimeSelectOnly,
127
- showTimeInput = _props$data.showTimeInput;
128
- var dateFormat = showTimeSelect && showTimeSelectOnly ? '' : props.data.dateFormat;
129
- var timeFormat = showTimeSelect || showTimeInput ? props.data.timeFormat : '';
130
- var formatMask = "".concat(dateFormat, " ").concat(timeFormat).trim();
131
- var updated = formatMask !== oldFormatMask;
132
- return {
133
- updated: updated,
134
- formatMask: formatMask
135
- };
136
- }
137
- }, {
138
150
  key: "updateDateTime",
139
151
  value: function updateDateTime(props, state, formatMask) {
140
152
  var value;
141
153
  var internalValue;
142
154
  var defaultToday = props.data.defaultToday;
155
+ var iOSFormatMask = 'yyyy-MM-dd';
143
156
  if (defaultToday && (props.defaultValue === '' || props.defaultValue === undefined)) {
144
- value = (0, _dateFns.format)(new Date(), formatMask);
145
- internalValue = new Date();
157
+ var dateToFormat = new Date();
158
+ value = dateToFormat.toString() === 'Invalid Date' ? '' : (0, _dateFns.format)(dateToFormat, iOSFormatMask);
159
+ internalValue = dateToFormat.toString() === 'Invalid Date' ? '' : dateToFormat; // Ensure not undefined
146
160
  } else {
147
- value = props.defaultValue;
148
- if (value === '' || value === undefined) {
149
- internalValue = undefined;
161
+ value = props.defaultValue || ''; // Ensure not undefined
162
+ if (value === '') {
163
+ internalValue = '';
150
164
  } else {
151
- internalValue = (0, _dateFns.parse)(value, state.formatMask, new Date());
165
+ var parsedDate = (0, _dateFns.parse)(value, formatMask, new Date());
166
+ internalValue = parsedDate.toString() === 'Invalid Date' ? '' : parsedDate; // Ensure not undefined
152
167
  }
153
168
  }
169
+
154
170
  return {
155
171
  value: value,
156
172
  internalValue: internalValue,
157
173
  placeholder: formatMask.toLowerCase(),
158
174
  defaultToday: defaultToday,
159
- formatMask: state.formatMask
175
+ formatMask: formatMask
176
+ };
177
+ }
178
+ }, {
179
+ key: "updateFormat",
180
+ value: function updateFormat(props, oldFormatMask) {
181
+ var _props$data = props.data,
182
+ showTimeSelect = _props$data.showTimeSelect,
183
+ showTimeSelectOnly = _props$data.showTimeSelectOnly,
184
+ showTimeInput = _props$data.showTimeInput;
185
+ var dateFormat = showTimeSelect && showTimeSelectOnly ? '' : props.data.dateFormat;
186
+ var timeFormat = showTimeSelect || showTimeInput ? props.data.timeFormat : '';
187
+ var formatMask = "".concat(dateFormat, " ").concat(timeFormat).trim();
188
+ var updated = formatMask !== oldFormatMask;
189
+ return {
190
+ updated: updated,
191
+ formatMask: formatMask
160
192
  };
161
193
  }
162
-
163
- // componentWillReceiveProps(props) {
164
- // const formatUpdated = this.updateFormat(props);
165
- // if ((props.data.defaultToday !== !this.state.defaultToday) || formatUpdated) {
166
- // const state = this.updateDateTime(props, this.formatMask);
167
- // this.setState(state);
168
- // }
169
- // }
170
194
  }, {
171
195
  key: "getDerivedStateFromProps",
172
196
  value: function getDerivedStateFromProps(props, state) {
@@ -174,8 +198,7 @@ var DatePicker = /*#__PURE__*/function (_React$Component) {
174
198
  updated = _DatePicker$updateFor2.updated,
175
199
  formatMask = _DatePicker$updateFor2.formatMask;
176
200
  if (props.data.defaultToday !== state.defaultToday || updated) {
177
- var newState = DatePicker.updateDateTime(props, state, formatMask);
178
- return newState;
201
+ return DatePicker.updateDateTime(props, state, formatMask);
179
202
  }
180
203
  return null;
181
204
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-form-builder-more-flexibility",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
4
4
  "description": "A complete form builder for react.",
5
5
  "main": "lib/index.js",
6
6
  "types": "types/index.d.ts",