import React, { useState } from "react"; import { useTranslation } from "react-i18next"; import moment from "moment"; import { Controller, useFormContext } from "react-hook-form"; import { DatePicker, Flex, FlexItem, FormGroup, NumberInput, Radio, Split, SplitItem, TimePicker, } from "@patternfly/react-core"; import { HelpItem } from "../../../components/help-enabler/HelpItem"; const DATE_TIME_FORMAT = /(\d\d\d\d-\d\d-\d\d)? (\d\d?):(\d\d?)/; const DateTime = ({ name }: { name: string }) => { const { control } = useFormContext(); const parseDate = (value: string, date?: Date): string => { const parts = value.match(DATE_TIME_FORMAT); if (date) { const parsedDate = moment(date).format("yyyy-MM-DD"); return `${parsedDate} ${parts ? parts[2] : "00"}:${ parts ? parts[3] : "00" }:00`; } return value; }; const parseTime = ( value: string, hour?: number | null, minute?: number | null ): string => { const parts = value.match(DATE_TIME_FORMAT); if (minute !== undefined && minute !== null) { return `${parts ? parts[1] : ""} ${hour}:${ minute < 10 ? `0${minute}` : minute }:00`; } return value; }; return ( { const dateTime = value.match(DATE_TIME_FORMAT) || ["", "", "0", "00"]; return ( { onChange(parseDate(value, date)); }} /> onChange(parseTime(value, hour, minute)) } is24Hour /> ); }} /> ); }; type NumberControlProps = { name: string; min: number; max: number; }; const NumberControl = ({ name, min, max }: NumberControlProps) => { const { control } = useFormContext(); const setValue = (newValue: number) => Math.min(newValue, max); return ( ( onChange(Number(value) + 1)} onMinus={() => onChange(Number(value) - 1)} onChange={(event) => { const newValue = Number(event.currentTarget.value); onChange(setValue(!isNaN(newValue) ? newValue : 0)); }} /> )} /> ); }; const FromTo = ({ name, ...rest }: NumberControlProps) => { const { t } = useTranslation("clients"); return ( } > {t("common:to")} ); }; export const Time = () => { const { t } = useTranslation("clients"); const { getValues } = useFormContext(); const [repeat, setRepeat] = useState(getValues("month")); return ( <> } > setRepeat(false)} label={t("notRepeat")} className="pf-u-mb-md" /> setRepeat(true)} label={t("repeat")} className="pf-u-mb-md" /> {repeat && ( <> )} } > } > ); };