mirror of
https://github.com/strapi/strapi.git
synced 2025-09-09 08:39:45 +00:00
Add datepicker and time picker
Signed-off-by: soupette <cyril@strapi.io>
This commit is contained in:
parent
ee0fb7b4e8
commit
afec87a74e
@ -1,4 +1,6 @@
|
|||||||
import { get, isArray, isObject } from 'lodash';
|
import get from 'lodash/get';
|
||||||
|
import isArray from 'lodash/isArray';
|
||||||
|
import isObject from 'lodash/isObject';
|
||||||
|
|
||||||
/* eslint-disable indent */
|
/* eslint-disable indent */
|
||||||
|
|
||||||
@ -23,13 +25,23 @@ const cleanData = (retrievedData, currentSchema, componentsSchema) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 'date':
|
// TODO
|
||||||
cleanedData =
|
// case 'date':
|
||||||
value && value._isAMomentObject === true ? value.format('YYYY-MM-DD') : value;
|
// cleanedData =
|
||||||
break;
|
// value && value._isAMomentObject === true ? value.format('YYYY-MM-DD') : value;
|
||||||
case 'datetime':
|
// break;
|
||||||
cleanedData = value && value._isAMomentObject === true ? value.toISOString() : value;
|
// case 'datetime':
|
||||||
|
// cleanedData = value && value._isAMomentObject === true ? value.toISOString() : value;
|
||||||
|
// break;
|
||||||
|
case 'time': {
|
||||||
|
cleanedData = value;
|
||||||
|
|
||||||
|
if (value && value.split(':').length < 3) {
|
||||||
|
cleanedData = `${value}:00`;
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case 'media':
|
case 'media':
|
||||||
if (getOtherInfos(schema, [current, 'multiple']) === true) {
|
if (getOtherInfos(schema, [current, 'multiple']) === true) {
|
||||||
cleanedData = value ? value.filter(file => !(file instanceof File)) : null;
|
cleanedData = value ? value.filter(file => !(file instanceof File)) : null;
|
||||||
|
@ -5,15 +5,19 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
import { useIntl } from 'react-intl';
|
import { useIntl } from 'react-intl';
|
||||||
|
import cloneDeep from 'lodash/cloneDeep';
|
||||||
|
import { formatISO } from 'date-fns';
|
||||||
|
import { DatePicker } from '@strapi/parts/DatePicker';
|
||||||
import { NumberInput } from '@strapi/parts/NumberInput';
|
import { NumberInput } from '@strapi/parts/NumberInput';
|
||||||
import { Select, Option } from '@strapi/parts/Select';
|
import { Select, Option } from '@strapi/parts/Select';
|
||||||
import { Textarea } from '@strapi/parts/Textarea';
|
import { Textarea } from '@strapi/parts/Textarea';
|
||||||
import { TextInput } from '@strapi/parts/TextInput';
|
import { TextInput } from '@strapi/parts/TextInput';
|
||||||
|
import { TimePicker } from '@strapi/parts/TimePicker';
|
||||||
import { ToggleInput } from '@strapi/parts/ToggleInput';
|
import { ToggleInput } from '@strapi/parts/ToggleInput';
|
||||||
import Hide from '@strapi/icons/Hide';
|
import Hide from '@strapi/icons/Hide';
|
||||||
import Show from '@strapi/icons/Show';
|
import Show from '@strapi/icons/Show';
|
||||||
import PropTypes from 'prop-types';
|
|
||||||
|
|
||||||
const GenericInput = ({
|
const GenericInput = ({
|
||||||
autoComplete,
|
autoComplete,
|
||||||
@ -102,6 +106,29 @@ const GenericInput = ({
|
|||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
case 'date': {
|
||||||
|
return (
|
||||||
|
<DatePicker
|
||||||
|
clearLabel={formatMessage({ id: 'clearLabel', defaultMessage: 'Clear' })}
|
||||||
|
disabled={disabled}
|
||||||
|
error={errorMessage}
|
||||||
|
label={label}
|
||||||
|
labelAction={labelAction}
|
||||||
|
id={name}
|
||||||
|
hint={hint}
|
||||||
|
name={name}
|
||||||
|
onChange={date => {
|
||||||
|
const formattedDate = formatISO(cloneDeep(date), { representation: 'date' });
|
||||||
|
|
||||||
|
onChange({ target: { name, value: formattedDate, type } });
|
||||||
|
}}
|
||||||
|
onClear={() => onChange({ target: { name, value: '', type } })}
|
||||||
|
placeholder={formattedPlaceholder}
|
||||||
|
selectedDate={value ? new Date(value) : null}
|
||||||
|
selectedDateLabel={formattedDate => `Date picker, current is ${formattedDate}`}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
case 'number': {
|
case 'number': {
|
||||||
return (
|
return (
|
||||||
<NumberInput
|
<NumberInput
|
||||||
@ -223,6 +250,39 @@ const GenericInput = ({
|
|||||||
</Textarea>
|
</Textarea>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
case 'time': {
|
||||||
|
let time = value;
|
||||||
|
|
||||||
|
// The backend send a value which has the following format: '00:45:00.000'
|
||||||
|
// or the time picker only supports hours & minutes so we need to mutate the value
|
||||||
|
if (value && value.split(':').length > 2) {
|
||||||
|
time = time.split(':');
|
||||||
|
time.pop();
|
||||||
|
time = time.join(':');
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<TimePicker
|
||||||
|
clearLabel={formatMessage({ id: 'clearLabel', defaultMessage: 'Clear' })}
|
||||||
|
disabled={disabled}
|
||||||
|
error={errorMessage}
|
||||||
|
label={label}
|
||||||
|
labelAction={labelAction}
|
||||||
|
id={name}
|
||||||
|
hint={hint}
|
||||||
|
name={name}
|
||||||
|
onChange={time => {
|
||||||
|
onChange({ target: { name, value: `${time}`, type } });
|
||||||
|
}}
|
||||||
|
onClear={() => {
|
||||||
|
onChange({ target: { name, value: null, type } });
|
||||||
|
}}
|
||||||
|
placeholder={formattedPlaceholder}
|
||||||
|
step={step}
|
||||||
|
value={time}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
default: {
|
default: {
|
||||||
return <div>{type} is not supported</div>;
|
return <div>{type} is not supported</div>;
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ import { TimePicker } from '@strapi/parts/TimePicker';
|
|||||||
import { Select, Option } from '@strapi/parts/Select';
|
import { Select, Option } from '@strapi/parts/Select';
|
||||||
import { useIntl } from 'react-intl';
|
import { useIntl } from 'react-intl';
|
||||||
import { formatISO } from 'date-fns';
|
import { formatISO } from 'date-fns';
|
||||||
import { cloneDeep } from 'lodash';
|
import cloneDeep from 'lodash/cloneDeep';
|
||||||
|
|
||||||
const Inputs = ({ label, onChange, options, type, value }) => {
|
const Inputs = ({ label, onChange, options, type, value }) => {
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage } = useIntl();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user