|
| 1 | +import KeyboardArrowDown from "@mui/icons-material/KeyboardArrowDown"; |
| 2 | +import FormHelperText from "@mui/material/FormHelperText"; |
| 3 | +import MenuItem from "@mui/material/MenuItem"; |
| 4 | +import Select from "@mui/material/Select"; |
| 5 | +import TextField, { type TextFieldProps } from "@mui/material/TextField"; |
| 6 | +import { type FC, useEffect, useReducer } from "react"; |
| 7 | +import { |
| 8 | + type TimeUnit, |
| 9 | + durationInDays, |
| 10 | + durationInHours, |
| 11 | + suggestedTimeUnit, |
| 12 | +} from "utils/time"; |
| 13 | + |
| 14 | +type DurationFieldProps = Omit<TextFieldProps, "value" | "onChange"> & { |
| 15 | + valueMs: number; |
| 16 | + onChange: (value: number) => void; |
| 17 | +}; |
| 18 | + |
| 19 | +type State = { |
| 20 | + unit: TimeUnit; |
| 21 | + // Handling empty values as strings in the input simplifies the process, |
| 22 | + // especially when a user clears the input field. |
| 23 | + durationFieldValue: string; |
| 24 | +}; |
| 25 | + |
| 26 | +type Action = |
| 27 | + | { type: "SYNC_WITH_PARENT"; parentValueMs: number } |
| 28 | + | { type: "CHANGE_DURATION_FIELD_VALUE"; fieldValue: string } |
| 29 | + | { type: "CHANGE_TIME_UNIT"; unit: TimeUnit }; |
| 30 | + |
| 31 | +const reducer = (state: State, action: Action): State => { |
| 32 | + switch (action.type) { |
| 33 | + case "SYNC_WITH_PARENT": { |
| 34 | + return initState(action.parentValueMs); |
| 35 | + } |
| 36 | + case "CHANGE_DURATION_FIELD_VALUE": { |
| 37 | + return { |
| 38 | + ...state, |
| 39 | + durationFieldValue: action.fieldValue, |
| 40 | + }; |
| 41 | + } |
| 42 | + case "CHANGE_TIME_UNIT": { |
| 43 | + const currentDurationMs = durationInMs( |
| 44 | + state.durationFieldValue, |
| 45 | + state.unit, |
| 46 | + ); |
| 47 | + |
| 48 | + if ( |
| 49 | + action.unit === "days" && |
| 50 | + !canConvertDurationToDays(currentDurationMs) |
| 51 | + ) { |
| 52 | + return state; |
| 53 | + } |
| 54 | + |
| 55 | + return { |
| 56 | + unit: action.unit, |
| 57 | + durationFieldValue: |
| 58 | + action.unit === "hours" |
| 59 | + ? durationInHours(currentDurationMs).toString() |
| 60 | + : durationInDays(currentDurationMs).toString(), |
| 61 | + }; |
| 62 | + } |
| 63 | + default: { |
| 64 | + return state; |
| 65 | + } |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +export const DurationField: FC<DurationFieldProps> = (props) => { |
| 70 | + const { |
| 71 | + valueMs: parentValueMs, |
| 72 | + onChange, |
| 73 | + helperText, |
| 74 | + ...textFieldProps |
| 75 | + } = props; |
| 76 | + const [state, dispatch] = useReducer(reducer, initState(parentValueMs)); |
| 77 | + const currentDurationMs = durationInMs(state.durationFieldValue, state.unit); |
| 78 | + |
| 79 | + useEffect(() => { |
| 80 | + if (parentValueMs !== currentDurationMs) { |
| 81 | + dispatch({ type: "SYNC_WITH_PARENT", parentValueMs }); |
| 82 | + } |
| 83 | + }, [currentDurationMs, parentValueMs]); |
| 84 | + |
| 85 | + return ( |
| 86 | + <div> |
| 87 | + <div |
| 88 | + css={{ |
| 89 | + display: "flex", |
| 90 | + gap: 8, |
| 91 | + }} |
| 92 | + > |
| 93 | + <TextField |
| 94 | + {...textFieldProps} |
| 95 | + fullWidth |
| 96 | + value={state.durationFieldValue} |
| 97 | + onChange={(e) => { |
| 98 | + const durationFieldValue = intMask(e.currentTarget.value); |
| 99 | + |
| 100 | + dispatch({ |
| 101 | + type: "CHANGE_DURATION_FIELD_VALUE", |
| 102 | + fieldValue: durationFieldValue, |
| 103 | + }); |
| 104 | + |
| 105 | + const newDurationInMs = durationInMs( |
| 106 | + durationFieldValue, |
| 107 | + state.unit, |
| 108 | + ); |
| 109 | + if (newDurationInMs !== parentValueMs) { |
| 110 | + onChange(newDurationInMs); |
| 111 | + } |
| 112 | + }} |
| 113 | + inputProps={{ |
| 114 | + step: 1, |
| 115 | + }} |
| 116 | + /> |
| 117 | + <Select |
| 118 | + disabled={props.disabled} |
| 119 | + css={{ width: 120, "& .MuiSelect-icon": { padding: 2 } }} |
| 120 | + value={state.unit} |
| 121 | + onChange={(e) => { |
| 122 | + const unit = e.target.value as TimeUnit; |
| 123 | + dispatch({ |
| 124 | + type: "CHANGE_TIME_UNIT", |
| 125 | + unit, |
| 126 | + }); |
| 127 | + }} |
| 128 | + inputProps={{ "aria-label": "Time unit" }} |
| 129 | + IconComponent={KeyboardArrowDown} |
| 130 | + > |
| 131 | + <MenuItem value="hours">Hours</MenuItem> |
| 132 | + <MenuItem |
| 133 | + value="days" |
| 134 | + disabled={!canConvertDurationToDays(currentDurationMs)} |
| 135 | + > |
| 136 | + Days |
| 137 | + </MenuItem> |
| 138 | + </Select> |
| 139 | + </div> |
| 140 | + |
| 141 | + {helperText && ( |
| 142 | + <FormHelperText error={props.error}>{helperText}</FormHelperText> |
| 143 | + )} |
| 144 | + </div> |
| 145 | + ); |
| 146 | +}; |
| 147 | + |
| 148 | +function initState(value: number): State { |
| 149 | + const unit = suggestedTimeUnit(value); |
| 150 | + const durationFieldValue = |
| 151 | + unit === "hours" |
| 152 | + ? durationInHours(value).toString() |
| 153 | + : durationInDays(value).toString(); |
| 154 | + |
| 155 | + return { |
| 156 | + unit, |
| 157 | + durationFieldValue, |
| 158 | + }; |
| 159 | +} |
| 160 | + |
| 161 | +function intMask(value: string): string { |
| 162 | + return value.replace(/\D/g, ""); |
| 163 | +} |
| 164 | + |
| 165 | +function durationInMs(durationFieldValue: string, unit: TimeUnit): number { |
| 166 | + const durationInMs = parseInt(durationFieldValue, 10); |
| 167 | + |
| 168 | + if (Number.isNaN(durationInMs)) { |
| 169 | + return 0; |
| 170 | + } |
| 171 | + |
| 172 | + return unit === "hours" |
| 173 | + ? hoursToDuration(durationInMs) |
| 174 | + : daysToDuration(durationInMs); |
| 175 | +} |
| 176 | + |
| 177 | +function hoursToDuration(hours: number): number { |
| 178 | + return hours * 60 * 60 * 1000; |
| 179 | +} |
| 180 | + |
| 181 | +function daysToDuration(days: number): number { |
| 182 | + return days * 24 * hoursToDuration(1); |
| 183 | +} |
| 184 | + |
| 185 | +function canConvertDurationToDays(duration: number): boolean { |
| 186 | + return Number.isInteger(durationInDays(duration)); |
| 187 | +} |
0 commit comments