Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed DateTimeWidget to generate a value according to json schema date-time format #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 88 additions & 2 deletions src/themes/bootstrap3/DateTimeWidget.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,94 @@
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 (
<div className={className}>
<label className="control-label" htmlFor={field.id}>
{field.label}
</label>
<input
{...field.input}
type="datetime-local"
step="1"
required={field.required}
className="form-control"
placeholder={field.placeholder}
/>
{field.meta.touched &&
field.meta.error && (
<span className="help-block">{field.meta.error}</span>
)}
{field.description && (
<span className="help-block">{field.description}</span>
)}
</div>
);
};

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) => {
//only change when fully entered
if(datetime.length < inputFormatLength){
return datetime;
}
return `${datetime}${timeZoneOffset()}`;
}


const toInputFormat = (datetime) => {
if (!datetime) {
return "";
}
//
if (datetime.length > inputFormatLength) {
return datetime.substring(0, inputFormatLength);
}
return datetime;
}
const DateTimeWidget = props => {
return <BaseInputWidget type="datetime-local" {...props} />;
return (
<Field
component={renderInput}
label={props.label}
name={props.fieldName}
required={props.required}
id={"field-" + props.fieldName}
placeholder={props.schema.default}
description={props.schema.description}
normalize={toDateTimeFormat}
format={toInputFormat}
/>
);
};

DateTimeWidget.propTypes = {
schema: PropTypes.object.isRequired,
required: PropTypes.bool,
fieldName: PropTypes.string,
label: PropTypes.string,
normalizer: PropTypes.func
};

export default DateTimeWidget;