2023-01-16 09:45:12 +00:00
|
|
|
import { commonMessages } from "@dashboard/intl";
|
|
|
|
import { DateTime, joinDateTime, splitDateTime } from "@dashboard/misc";
|
2021-10-01 12:41:31 +00:00
|
|
|
import { TextField } from "@material-ui/core";
|
|
|
|
import { TextFieldProps } from "@material-ui/core/TextField";
|
|
|
|
import { makeStyles } from "@saleor/macaw-ui";
|
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import { useIntl } from "react-intl";
|
|
|
|
|
|
|
|
import ErrorNoticeBar from "./ErrorNoticeBar";
|
|
|
|
|
|
|
|
type DateTimeFieldProps = Omit<TextFieldProps, "label" | "error"> & {
|
|
|
|
onChange: (value: string) => void;
|
|
|
|
error: string | React.ReactNode;
|
|
|
|
setError?: () => void;
|
|
|
|
futureDatesOnly?: boolean;
|
|
|
|
value: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const useStyles = makeStyles(
|
|
|
|
theme => ({
|
|
|
|
dateInput: {
|
2022-06-21 09:36:55 +00:00
|
|
|
marginRight: theme.spacing(2),
|
2021-10-01 12:41:31 +00:00
|
|
|
},
|
|
|
|
errorNoticeBar: {
|
2022-06-21 09:36:55 +00:00
|
|
|
marginTop: theme.spacing(2),
|
|
|
|
},
|
2021-10-01 12:41:31 +00:00
|
|
|
}),
|
2022-06-21 09:36:55 +00:00
|
|
|
{ name: "DateTimeTimezoneField" },
|
2021-10-01 12:41:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
export const DateTimeTimezoneField: React.FC<DateTimeFieldProps> = ({
|
|
|
|
disabled,
|
|
|
|
name,
|
|
|
|
onChange,
|
|
|
|
error,
|
|
|
|
fullWidth,
|
2022-06-21 09:36:55 +00:00
|
|
|
value: initialValue,
|
2021-10-01 12:41:31 +00:00
|
|
|
}) => {
|
|
|
|
const classes = useStyles({});
|
|
|
|
const intl = useIntl();
|
|
|
|
const [value, setValue] = useState<DateTime>(
|
2022-06-21 09:36:55 +00:00
|
|
|
initialValue ? splitDateTime(initialValue) : { date: "", time: "" },
|
2021-10-01 12:41:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const newDate = joinDateTime(value.date, value.time);
|
|
|
|
onChange(newDate);
|
|
|
|
}, [value]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<TextField
|
|
|
|
className={classes.dateInput}
|
|
|
|
fullWidth={fullWidth}
|
|
|
|
disabled={disabled}
|
|
|
|
error={!!error}
|
|
|
|
label={intl.formatMessage(commonMessages.date)}
|
|
|
|
name={`${name}:date`}
|
|
|
|
onChange={event => {
|
|
|
|
const date = event.target.value;
|
|
|
|
setValue(value => ({ ...value, date }));
|
|
|
|
}}
|
|
|
|
type="date"
|
|
|
|
value={value.date}
|
|
|
|
InputLabelProps={{ shrink: true }}
|
|
|
|
/>
|
|
|
|
<TextField
|
|
|
|
fullWidth={fullWidth}
|
|
|
|
disabled={disabled}
|
|
|
|
error={!!error}
|
|
|
|
label={intl.formatMessage(commonMessages.time)}
|
|
|
|
name={`${name}:time`}
|
|
|
|
onChange={event => {
|
|
|
|
const time = event.target.value;
|
|
|
|
setValue(value => ({ ...value, time }));
|
|
|
|
}}
|
|
|
|
type="time"
|
|
|
|
value={value.time}
|
|
|
|
InputLabelProps={{ shrink: true }}
|
|
|
|
/>
|
|
|
|
|
|
|
|
{error && (
|
|
|
|
<ErrorNoticeBar className={classes.errorNoticeBar} message={error} />
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|