|
| 1 | +import dayjs from "dayjs"; |
| 2 | +import { useField } from "formik"; |
| 3 | +import { useCallback, useMemo, useState } from "react"; |
| 4 | +import CreatableSelect from "react-select/creatable"; |
| 5 | + |
| 6 | +type FormikDurationNanosecondsFieldProps = { |
| 7 | + name: string; |
| 8 | + required?: boolean; |
| 9 | + label?: string; |
| 10 | + hint?: string; |
| 11 | + hintPosition?: "top" | "bottom"; |
| 12 | + isClearable?: boolean; |
| 13 | +}; |
| 14 | +export default function FormikDurationNanosecondsField({ |
| 15 | + name, |
| 16 | + required = false, |
| 17 | + label, |
| 18 | + hint, |
| 19 | + hintPosition = "bottom", |
| 20 | + isClearable = true |
| 21 | +}: FormikDurationNanosecondsFieldProps) { |
| 22 | + const [isTouched, setIsTouched] = useState(false); |
| 23 | + |
| 24 | + const [field, meta] = useField<string>({ |
| 25 | + name, |
| 26 | + type: "text", |
| 27 | + required, |
| 28 | + validate: useCallback( |
| 29 | + (value: string) => { |
| 30 | + if (required && !value) { |
| 31 | + return "This field is required"; |
| 32 | + } |
| 33 | + |
| 34 | + // if value is less than 1 minute, show error |
| 35 | + if (parseInt(value, 10) < 60 * 1e9) { |
| 36 | + return "Duration must be greater than 1 minute"; |
| 37 | + } |
| 38 | + }, |
| 39 | + [required] |
| 40 | + ) |
| 41 | + }); |
| 42 | + |
| 43 | + const value = useMemo(() => { |
| 44 | + // we want to take nanoseconds and convert them to 1h, 1m, 1s |
| 45 | + if (!field.value) { |
| 46 | + return undefined; |
| 47 | + } |
| 48 | + |
| 49 | + const duration = dayjs.duration( |
| 50 | + parseInt(field.value, 10) / 1000000, |
| 51 | + "milliseconds" |
| 52 | + ); |
| 53 | + return `${duration.humanize()}`; |
| 54 | + }, [field.value]); |
| 55 | + |
| 56 | + const handleOnChange = (value?: string) => { |
| 57 | + if (!value) { |
| 58 | + field.onChange({ |
| 59 | + target: { |
| 60 | + name: field.name, |
| 61 | + value: "" |
| 62 | + } |
| 63 | + }); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + // we want to take 1h, 1m, 1s and convert them to nanoseconds |
| 68 | + let nanoseconds = 0; |
| 69 | + if (value.includes("h")) { |
| 70 | + nanoseconds = parseInt(value.replace("h", ""), 10) * 60 * 60 * 1e9; |
| 71 | + } else if (value.includes("m")) { |
| 72 | + // 1m = 60s |
| 73 | + nanoseconds = parseInt(value.replace("m", ""), 10) * 60 * 1e9; |
| 74 | + } else if (value.includes("s")) { |
| 75 | + nanoseconds = parseInt(value.replace("s", ""), 10) * 1e9; |
| 76 | + } else if (value.includes("d")) { |
| 77 | + nanoseconds = parseInt(value.replace("d", ""), 10) * 24 * 60 * 60 * 1e9; |
| 78 | + } else if (value.includes("w")) { |
| 79 | + nanoseconds = |
| 80 | + parseInt(value.replace("w", ""), 10) * 7 * 24 * 60 * 60 * 1e9; |
| 81 | + } |
| 82 | + |
| 83 | + field.onChange({ |
| 84 | + target: { |
| 85 | + name: field.name, |
| 86 | + value: nanoseconds |
| 87 | + } |
| 88 | + }); |
| 89 | + }; |
| 90 | + |
| 91 | + return ( |
| 92 | + <div className="flex flex-col"> |
| 93 | + {label && <label className="form-label mb-0">{label}</label>} |
| 94 | + {hint && hintPosition === "top" && ( |
| 95 | + <p className="text-sm text-gray-500">{hint}</p> |
| 96 | + )} |
| 97 | + <CreatableSelect<{ label: string; value: string }> |
| 98 | + className="h-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 sm:text-sm" |
| 99 | + onChange={(value) => { |
| 100 | + handleOnChange(value?.value ?? undefined); |
| 101 | + setIsTouched(true); |
| 102 | + }} |
| 103 | + value={[{ label: value!, value: value! }]} |
| 104 | + options={[ |
| 105 | + "3m", |
| 106 | + "5m", |
| 107 | + "10m", |
| 108 | + "15m", |
| 109 | + "30m", |
| 110 | + "1h", |
| 111 | + "4h", |
| 112 | + "8h", |
| 113 | + "1d", |
| 114 | + "3d", |
| 115 | + "7d" |
| 116 | + ].map((value) => ({ |
| 117 | + label: value, |
| 118 | + value |
| 119 | + }))} |
| 120 | + onBlur={(event) => { |
| 121 | + field.onBlur(event); |
| 122 | + setIsTouched(true); |
| 123 | + }} |
| 124 | + onFocus={(event) => { |
| 125 | + field.onBlur(event); |
| 126 | + setIsTouched(true); |
| 127 | + }} |
| 128 | + name={field.name} |
| 129 | + isClearable={isClearable} |
| 130 | + isMulti={false} |
| 131 | + menuPortalTarget={document.body} |
| 132 | + styles={{ |
| 133 | + menuPortal: (base) => ({ ...base, zIndex: 9999 }) |
| 134 | + }} |
| 135 | + menuPosition={"fixed"} |
| 136 | + menuShouldBlockScroll={true} |
| 137 | + /> |
| 138 | + {hint && hintPosition === "bottom" && ( |
| 139 | + <p className="text-sm text-gray-500">{hint}</p> |
| 140 | + )} |
| 141 | + {isTouched && meta.error ? ( |
| 142 | + <p className="w-full py-1 text-sm text-red-500">{meta.error}</p> |
| 143 | + ) : null} |
| 144 | + </div> |
| 145 | + ); |
| 146 | +} |
0 commit comments