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

[RFR] Migrate DateInput to TypeScript #3512

Merged
merged 1 commit into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
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
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