Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | 76x 76x 404x 404x 404x 2828x 404x 38x 38x 404x 594x 404x 25x 25x 25x 808x 404x 12x 12x 808x 2020x 134x 134x 28x 218x 162x 218x 25x 25x 50x 119x 119x 94x 35x 10x 45x 55x 62x 62x 62x 51x 51x 51x 51x 506x 102x 506x 176x 32x 32x 32x 32x 120x 32x 38x 32x 32x 32x 64x 32x 32x 34x 32x 32x 640x 96x 224x 38x 160x 166x 113x 96x 113x 96x 8x 8x 16x | import { type ChangeEvent, type ComponentProps, useState } from "react";
import { Badge } from "@/ahuora-design-system/ui/badge";
import { Input } from "@/ahuora-design-system/ui/input";
import { Label } from "@/ahuora-design-system/ui/label";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/ahuora-design-system/ui/select";
import { cn } from "@/lib/utils";
import {
formatNumericInputValue,
stripNumericInputFormatting,
unitLooksMonetary,
} from "../model/economicsFormatters";
type ValueWithUnitBaseProps = {
id: string;
unitId?: string;
label: string;
help?: string;
value: string;
unit: string;
disabled?: boolean;
error?: string;
errorTone?: "warning" | "destructive";
invalid?: boolean;
messageId?: string;
renderError?: boolean;
labelClassName?: string;
layout?: "default" | "compact" | "mapping";
valueAriaLabel?: string;
unitAriaLabel?: string;
defaultToZero?: boolean;
onValueChange: (value: string) => void;
};
const WARNING_INVALID_CLASS =
"aria-[invalid=true]:border-orange-500 aria-[invalid=true]:focus-visible:border-b-orange-500";
const DESTRUCTIVE_INVALID_CLASS =
"aria-[invalid=true]:border-destructive aria-[invalid=true]:focus-visible:border-b-destructive";
export type UnitSelectOption<TUnit extends string = string> = {
value: TUnit;
label: string;
};
export type NumericInputFormat = "plain" | "currency" | "auto-unit";
export type FormattedNumericInputProps = Omit<
ComponentProps<typeof Input>,
"value" | "onChange"
> & {
value: string | number | null | undefined;
unit?: string | null;
numericFormat?: NumericInputFormat;
defaultToZero?: boolean;
onValueChange: (value: string) => void;
};
export function FormattedNumericInput({
value,
unit,
numericFormat = "plain",
defaultToZero = true,
onValueChange,
...inputProps
}: FormattedNumericInputProps) {
const [isEditingEmptyValue, setIsEditingEmptyValue] = useState(false);
const useGrouping =
numericFormat === "currency" ||
(numericFormat === "auto-unit" && unitLooksMonetary(unit));
const normalizedValue = stripNumericInputFormatting(
value == null ? "" : String(value),
);
const displayValue =
isEditingEmptyValue && !normalizedValue.trim()
? ""
: formatNumericInputValue(value, {
defaultToZero,
useGrouping,
});
const handleValueInput = (event: ChangeEvent<HTMLInputElement>) => {
const nextValue = stripNumericInputFormatting(event.currentTarget.value);
setIsEditingEmptyValue(nextValue === "");
onValueChange(nextValue);
};
return (
<Input
{...inputProps}
inputMode="decimal"
value={displayValue}
onInput={handleValueInput}
onChange={handleValueInput}
onBlur={(event) => {
setIsEditingEmptyValue(false);
inputProps.onBlur?.(event);
}}
/>
);
}
export function FieldLabel({
htmlFor,
label,
help,
labelClassName,
wrapperClassName = "flex min-h-6 items-center gap-2",
}: {
htmlFor: string;
label: string;
help?: string;
labelClassName?: string;
wrapperClassName?: string;
}) {
return (
<div className={wrapperClassName}>
<Label htmlFor={htmlFor} className={labelClassName}>
{label}
</Label>
{help && <HelpBadge help={help} />}
</div>
);
}
export function HelpBadge({ help }: { help: string }) {
return (
<span
className="inline-flex size-4 items-center justify-center rounded-full border text-[10px] text-muted-foreground"
aria-label={help}
title={help}
>
?
</span>
);
}
export function FieldMeta({
id,
error,
defaultBadge,
errorTone = "warning",
}: {
id?: string;
error?: string;
defaultBadge?: string;
errorTone?: "warning" | "destructive";
}) {
Iif (!error && !defaultBadge) return null;
return (
<div id={id} className="flex flex-wrap items-center gap-2 text-xs">
{defaultBadge && <DefaultBadge label={defaultBadge} />}
{error && (
<span
className={
errorTone === "destructive"
? "text-destructive"
: "text-orange-700 dark:text-orange-300"
}
role={errorTone === "destructive" ? "alert" : "status"}
>
{error}
</span>
)}
</div>
);
}
export function ValueWithFixedUnitField({
id,
unitId = `${id}-unit`,
label,
help,
unit,
value,
disabled,
error,
errorTone,
invalid,
messageId: providedMessageId,
renderError = true,
labelClassName,
layout = "default",
unitDisplay = "field",
valueAriaLabel,
unitAriaLabel,
defaultToZero,
onValueChange,
}: ValueWithUnitBaseProps & {
unitDisplay?: "field" | "compact-field" | "suffix";
}) {
const helpId = help ? `${id}-help` : undefined;
const messageId = error ? (providedMessageId ?? `${id}-message`) : undefined;
const describedBy =
[helpId, messageId].filter(Boolean).join(" ") || undefined;
return (
<div className={cn("min-w-0", fieldLayoutClass(layout))}>
<FieldLabel
htmlFor={id}
label={label}
help={help}
labelClassName={labelClassName}
wrapperClassName={fieldLabelWrapperClass(layout)}
/>
<div
className={cn(
unitDisplay === "suffix" && "relative",
unitDisplay === "field" &&
"grid grid-cols-[minmax(0,1fr)_8rem] gap-2",
unitDisplay === "compact-field" &&
"grid grid-cols-[minmax(0,1fr)_4rem] gap-2",
"min-w-0",
)}
>
<FormattedNumericInput
id={id}
value={value}
unit={unit}
numericFormat="auto-unit"
defaultToZero={defaultToZero}
disabled={disabled}
className={cn(
unitDisplay === "suffix" ? "pr-10" : undefined,
invalidFieldClass(errorTone),
)}
aria-label={valueAriaLabel}
aria-invalid={invalid ?? Boolean(error)}
aria-describedby={describedBy}
onValueChange={onValueChange}
/>
{unitDisplay === "suffix" ? (
<span
id={unitId}
className="pointer-events-none absolute inset-y-0 right-3 flex items-center text-sm text-muted-foreground"
aria-label={unitAriaLabel ?? `${label} unit`}
>
{unit}
</span>
) : (
<Input
id={unitId}
value={unit}
disabled
aria-label={unitAriaLabel ?? `${label} unit`}
/>
)}
</div>
{help && (
<div id={helpId} className="sr-only">
{help}
</div>
)}
{renderError && (
<FieldMeta id={messageId} error={error} errorTone={errorTone} />
)}
</div>
);
}
export function ValueWithUnitTextField({
id,
unitId = `${id}-unit`,
label,
help,
value,
unit,
disabled,
error,
errorTone,
labelClassName,
layout = "default",
valueAriaLabel,
unitAriaLabel,
onValueChange,
onUnitChange,
}: ValueWithUnitBaseProps & {
onUnitChange: (unit: string) => void;
}) {
const helpId = help ? `${id}-help` : undefined;
const messageId = error ? `${id}-message` : undefined;
const describedBy =
[helpId, messageId].filter(Boolean).join(" ") || undefined;
const handleUnitInput = (event: ChangeEvent<HTMLInputElement>) =>
onUnitChange(event.currentTarget.value);
return (
<div className={fieldLayoutClass(layout)}>
<FieldLabel
htmlFor={id}
label={label}
help={help}
labelClassName={labelClassName}
wrapperClassName={fieldLabelWrapperClass(layout)}
/>
<div className="grid grid-cols-[minmax(0,1fr)_8rem] gap-2">
<FormattedNumericInput
id={id}
value={value}
unit={unit}
numericFormat="auto-unit"
disabled={disabled}
className={invalidFieldClass(errorTone)}
aria-label={valueAriaLabel}
aria-invalid={Boolean(error)}
aria-describedby={describedBy}
onValueChange={onValueChange}
/>
<Input
id={unitId}
value={unit}
disabled={disabled}
aria-label={unitAriaLabel ?? `${label} unit`}
aria-describedby={describedBy}
onInput={handleUnitInput}
onChange={handleUnitInput}
/>
</div>
{help && (
<div id={helpId} className="sr-only">
{help}
</div>
)}
<FieldMeta id={messageId} error={error} errorTone={errorTone} />
</div>
);
}
export function ValueWithUnitSelectField<TUnit extends string>({
id,
unitId = `${id}-unit`,
label,
help,
value,
unit,
units = [],
disabled,
error,
errorTone,
labelClassName,
layout = "default",
valueAriaLabel,
unitAriaLabel,
onValueChange,
onUnitChange,
}: ValueWithUnitBaseProps & {
unit: TUnit;
units?: readonly (TUnit | UnitSelectOption<TUnit>)[];
onUnitChange: (unit: TUnit) => void;
}) {
const helpId = help ? `${id}-help` : undefined;
const messageId = error ? `${id}-message` : undefined;
const describedBy =
[helpId, messageId].filter(Boolean).join(" ") || undefined;
const unitOptions = units.map((option) =>
typeof option === "string" ? { value: option, label: option } : option,
);
const selectedUnitLabel =
unitOptions.find((option) => option.value === unit)?.label ?? unit;
return (
<div className={fieldLayoutClass(layout)}>
<FieldLabel
htmlFor={id}
label={label}
help={help}
labelClassName={labelClassName}
wrapperClassName={fieldLabelWrapperClass(layout)}
/>
<div className="grid grid-cols-[minmax(0,1fr)_8rem] gap-2">
<FormattedNumericInput
id={id}
value={value}
unit={unit}
numericFormat="auto-unit"
disabled={disabled}
className={invalidFieldClass(errorTone)}
aria-label={valueAriaLabel}
aria-invalid={Boolean(error)}
aria-describedby={describedBy}
onValueChange={onValueChange}
/>
<Select
value={unit}
disabled={disabled}
onValueChange={(nextUnit) => onUnitChange(nextUnit as TUnit)}
>
<SelectTrigger
id={unitId}
aria-label={unitAriaLabel ?? `${label} unit`}
className={invalidFieldClass(errorTone)}
>
<SelectValue placeholder="Not selected">
{selectedUnitLabel}
</SelectValue>
</SelectTrigger>
<SelectContent>
{unitOptions.map((option) => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
{help && (
<div id={helpId} className="sr-only">
{help}
</div>
)}
<FieldMeta id={messageId} error={error} errorTone={errorTone} />
</div>
);
}
export function UnitSelectField<TUnit extends string>({
id,
label,
help,
unit,
units = [],
disabled,
error,
errorTone,
placeholder = "Not selected",
onUnitChange,
}: {
id: string;
label: string;
help?: string;
unit: TUnit;
units?: readonly (TUnit | UnitSelectOption<TUnit>)[];
disabled?: boolean;
error?: string;
errorTone?: "warning" | "destructive";
placeholder?: string;
onUnitChange: (unit: TUnit) => void;
}) {
const messageId = error ? `${id}-message` : undefined;
const unitOptions = units.map((option) =>
typeof option === "string" ? { value: option, label: option } : option,
);
const selectedUnitLabel =
unitOptions.find((option) => option.value === unit)?.label ?? unit;
return (
<div className="space-y-2">
<FieldLabel htmlFor={id} label={label} help={help} />
<Select
value={unit}
disabled={disabled}
onValueChange={(nextUnit) => onUnitChange(nextUnit as TUnit)}
>
<SelectTrigger
id={id}
aria-label={`${label} unit`}
aria-invalid={Boolean(error)}
aria-describedby={messageId}
className={invalidFieldClass(errorTone)}
>
<SelectValue placeholder={placeholder}>
{selectedUnitLabel}
</SelectValue>
</SelectTrigger>
<SelectContent>
{unitOptions.map((option) => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
<FieldMeta id={messageId} error={error} errorTone={errorTone} />
</div>
);
}
function invalidFieldClass(errorTone?: "warning" | "destructive") {
return errorTone === "destructive"
? DESTRUCTIVE_INVALID_CLASS
: WARNING_INVALID_CLASS;
}
function fieldLayoutClass(layout: "default" | "compact" | "mapping") {
if (layout === "mapping") return "grid gap-2";
return layout === "compact" ? "grid gap-1" : "space-y-2";
}
function fieldLabelWrapperClass(layout: "default" | "compact" | "mapping") {
if (layout === "mapping") return "flex items-center gap-2";
return layout === "compact"
? "flex items-center gap-1"
: "flex min-h-6 items-center gap-2";
}
export function DefaultBadge({ label }: { label: string }) {
return (
<Badge variant="secondary" size="xs" borderRadius="round">
{label}
</Badge>
);
}
export type TextInputProps = Omit<
ComponentProps<typeof Input>,
"id" | "value" | "onChange"
>;
|