import { Box, InputLabel, MenuItem, Select } from "@mui/material"; import "dayjs/locale/fr"; import { TimepickerSpecificProps } from "interface"; import React, { memo, useEffect } from "react"; import { makeStylesEdt } from "../../theme"; import { createCustomizableLunaticField } from "../../utils/create-customizable-lunatic-field"; export type DurationProps = { value?: string; handleChange(response: { [name: string]: string }, value: string | undefined): void; label?: string; hourLabel?: string; andLabel?: string; minLabel?: string; response: { [name: string]: string }; componentSpecificProps?: TimepickerSpecificProps; variables: Map; bindingDependencies: string[]; }; const enum DurationEnum { HOUR = "h", MINUTES = "m", } const getNumElements = (typeDuration: string) => { if (DurationEnum.HOUR == typeDuration) { return 24; } else if (DurationEnum.MINUTES == typeDuration) { return 60; } else { return 0; } }; const Duration = memo((props: DurationProps) => { let { handleChange, value, label, hourLabel, andLabel, minLabel, response, componentSpecificProps, variables, bindingDependencies, } = props; value = variables.get(bindingDependencies[0]); const { classes } = useStyles(); const [hour, setHour] = React.useState(""); const [minutes, setMinutes] = React.useState(""); useEffect(() => { if (value != null) { setHour(value.split(":")[0]); setMinutes(value.split(":")[1]); } }, []); useEffect(() => { const hours = isNaN(Number(hour)) || hour == null || hour.length == 0 ? undefined : hour; const min = isNaN(Number(minutes)) || minutes == null || minutes.length == 0 ? undefined : minutes; if (hours == null && min == null) { handleChange(response, undefined); } else { const newValue = (hours ?? "0") + ":" + (min ?? "0"); handleChange(response, newValue); } }, [hour, minutes]); function setValueLunatic(newHour: string, newMin: string) { const hours = isNaN(Number(newHour)) || newHour == null ? "0" : newHour; setHour(hours); const minutes = isNaN(Number(newMin)) || newMin == null ? "0" : newMin; setMinutes(minutes); const newValue = hours + ":" + minutes; handleChange(response, newValue); } const listHourElements: number[] = Array.from(Array(getNumElements("h")).keys()); const listMinElements: number[] = Array.from(Array(getNumElements("m")).keys()); return ( <>

{label} ?

{hourLabel && ( {hourLabel} )} {andLabel} {minLabel && ( {minLabel} )} ); }); const useStyles = makeStylesEdt({ "name": { Duration } })(theme => ({ labelSpacer: { marginBottom: "1rem", textAlign: "center", }, innerLabel: { fontSize: "14px !important", color: theme.palette.info.main, fontWeight: "bold", display: "flex", justifyContent: "center", }, containerBox: { backgroundColor: theme.variables.white, padding: "2rem 3rem", display: "flex", justifyContent: "center", borderRadius: "10px", }, durationBox: { display: "flex", justifyContent: "space-between", width: "85%", }, durationInnerBox: { width: "46%", }, selectBox: { width: "100%", ".MuiOutlinedInput-input": { minHeight: "1rem !important", height: "1rem !important", }, }, menuSelectBox: { height: "45%", }, h1: { fontSize: "18px", margin: 0, lineHeight: "1.5rem", fontWeight: "bold", }, })); export default createCustomizableLunaticField(Duration, "Duration");