From 04be1cea6681fc82c84160fd3c29b5f811ea7714 Mon Sep 17 00:00:00 2001 From: Pavel Kharakh Date: Fri, 9 Nov 2018 21:48:28 +0200 Subject: [PATCH 1/2] fixed DateTimeWidget to generate a value according to json schema date-time format --- src/themes/bootstrap3/DateTimeWidget.js | 68 ++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/src/themes/bootstrap3/DateTimeWidget.js b/src/themes/bootstrap3/DateTimeWidget.js index 1833f88..ff481dc 100644 --- a/src/themes/bootstrap3/DateTimeWidget.js +++ b/src/themes/bootstrap3/DateTimeWidget.js @@ -1,8 +1,72 @@ import React from "react"; -import BaseInputWidget from "./BaseInputWidget"; +import PropTypes from "prop-types"; +import classNames from "classnames"; +import { Field } from "redux-form"; +const renderInput = field => { + const className = classNames([ + "form-group", + { "has-error": field.meta.touched && field.meta.error } + ]); + return ( +
+ + + {field.meta.touched && + field.meta.error && ( + {field.meta.error} + )} + {field.description && ( + {field.description} + )} +
+ ); +}; + + +const toDateTimeFormat = (datetime) => { + return `${datetime}Z`; +} +const toInputFormat = (datetime) => { + if (!datetime) { + return ""; + } + if (datetime.endsWith("Z")) { + return datetime.substring(0, datetime.length - 1); + } + return datetime; +} const DateTimeWidget = props => { - return ; + return ( + + ); +}; + +DateTimeWidget.propTypes = { + schema: PropTypes.object.isRequired, + required: PropTypes.bool, + fieldName: PropTypes.string, + label: PropTypes.string, + normalizer: PropTypes.func }; export default DateTimeWidget; From 3e38f41e2ddcb2018e38917deceee907a73e6d51 Mon Sep 17 00:00:00 2001 From: Pavel Kharakh Date: Fri, 9 Nov 2018 22:51:18 +0200 Subject: [PATCH 2/2] setting current timezone offset instead of "Z" --- src/themes/bootstrap3/DateTimeWidget.js | 28 ++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/themes/bootstrap3/DateTimeWidget.js b/src/themes/bootstrap3/DateTimeWidget.js index ff481dc..4cfce2e 100644 --- a/src/themes/bootstrap3/DateTimeWidget.js +++ b/src/themes/bootstrap3/DateTimeWidget.js @@ -32,16 +32,38 @@ const renderInput = field => { ); }; +const inputFormatLength = "YYYY-MM-DDTHH:mm:ss".length; + +const pad = (n) => { + return n < 10 ? '0' + n : n; +} + +const timeZoneOffset = () =>{ + const now = new Date(); + const tz = now.getTimezoneOffset(); + const sign = tz > 0 ? "-" : "+"; + const hours = pad(Math.floor(Math.abs(tz) / 60)); + const minutes = pad(Math.abs(tz) % 60); + + return `${sign}${hours}:${minutes}`; +} const toDateTimeFormat = (datetime) => { - return `${datetime}Z`; + //only change when fully entered + if(datetime.length < inputFormatLength){ + return datetime; + } + return `${datetime}${timeZoneOffset()}`; } + + const toInputFormat = (datetime) => { if (!datetime) { return ""; } - if (datetime.endsWith("Z")) { - return datetime.substring(0, datetime.length - 1); + // + if (datetime.length > inputFormatLength) { + return datetime.substring(0, inputFormatLength); } return datetime; }