Skip to content

Commit

Permalink
[RFR] Migrate DateInput to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
djhi committed Aug 13, 2019
1 parent ded57c0 commit 7dd497c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import React from 'react';
import React, { FunctionComponent } from 'react';
import PropTypes from 'prop-types';
import TextField from '@material-ui/core/TextField';
import { useInput, FieldTitle } from 'ra-core';
import TextField, { TextFieldProps } from '@material-ui/core/TextField';
import { useInput, FieldTitle, InputProps } from 'ra-core';

import sanitizeRestProps from './sanitizeRestProps';
import InputHelperText from './InputHelperText';

/**
* Convert Date object to String
*
* @param {Date} v value to convert
* @param {Date} value value to convert
* @returns {String} A standardized date (yyyy-MM-dd), to be passed to an <input type="date" />
*/
const convertDateToString = v => {
if (!(v instanceof Date) || isNaN(v.getDate())) return;
const convertDateToString = (value: Date) => {
if (!(value instanceof Date) || isNaN(value.getDate())) return;
const pad = '00';
const yyyy = v.getFullYear().toString();
const MM = (v.getMonth() + 1).toString();
const dd = v.getDate().toString();
const yyyy = value.getFullYear().toString();
const MM = (value.getMonth() + 1).toString();
const dd = value.getDate().toString();
return `${yyyy}-${(pad + MM).slice(-2)}-${(pad + dd).slice(-2)}`;
};

const dateRegex = /^\d{4}-\d{2}-\d{2}$/;

const format = value => {
const format = (value: string | Date) => {
// null, undefined and empty string values should not go through dateFormatter
// otherwise, it returns undefined and will make the input an uncontrolled one.
if (value == null || value === '') {
Expand All @@ -42,7 +42,9 @@ const format = value => {
return convertDateToString(new Date(value));
};

export const DateInput = ({
export const DateInput: FunctionComponent<
InputProps<TextFieldProps> & Omit<TextFieldProps, 'helperText' | 'label'>
> = ({
label,
options,
source,
Expand Down Expand Up @@ -102,7 +104,6 @@ export const DateInput = ({
};

DateInput.propTypes = {
className: PropTypes.string,
label: PropTypes.string,
options: PropTypes.object,
resource: PropTypes.string,
Expand Down

0 comments on commit 7dd497c

Please sign in to comment.