All files / src/pages/flowsheet-page/economics/cost-curves/ui CostCurveFields.tsx

94.11% Statements 64/68
66.35% Branches 71/107
63.63% Functions 7/11
100% Lines 42/42

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                              112x           112x     112x                       112x     126x 112x                                               4x   1120x 126x 448x       32x               32x                             32x     36x       32x 96x     2x     32x 32x     34x 42x 32x 2x   36x 32x 314x     96x 96x 192x 36x 128x       48x           48x                             54x 48x         48x 48x 51x 96x 432x 54x 192x      
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 { Textarea } from "@/ahuora-design-system/ui/textarea";
import { cn } from "@/lib/utils";
import { FormattedNumericInput } from "../../shared/ui/EconomicsFormFields";
import { WARNING_INVALID_CLASS } from "./constants";
import { FieldError } from "./FieldError";
 
export function TextField({
  id,
  label,
  value,
  error,
  onChange,
  type = "text",
  inputMode,
  maxLength,
  disabled = false,
}: {
  id: string;
  label: string;
  value: string;
  error?: string;
  onChange: (value: string) => void;
  type?: string;
  inputMode?: "decimal";
  maxLength?: number;
  disabled?: boolean;
}) {
  const numeric = inputMode === "decimal";
  return (
    <div className="grid gap-2">
      <Label htmlFor={id}>{label}</Label>
      {numeric ? (
        <FormattedNumericInput
          id={id}
          value={value}
          type={type}
          inputMode={inputMode}
          maxLength={maxLength}
          disabled={disabled}
          aria-invalid={Boolean(error)}
          aria-describedby={error ? `${id}-error` : undefined}
          className={WARNING_INVALID_CLASS}
          onValueChange={onChange}
        />
      ) : (
        <Input
          id={id}
          value={value}
          type={type}
          inputMode={inputMode}
          maxLength={maxLength}
          disabled={disabled}
          aria-invalid={Boolean(error)}
          aria-describedby={error ? `${id}-error` : undefined}
          className={WARNING_INVALID_CLASS}
          onChange={(event) => onChange(event.target.value)}
        />
      )}
      <FieldError id={`${id}-error`} message={error} />
    </div>
  );
}
 
export function PickerField({
  id,
  label,
  value,
  options,
  optionLabels,
  placeholder,
  emptyValue,
  disabled = false,
  error,
  onChange,
}: {
  id: string;
  label: string;
  value: string;
  options: readonly string[];
  optionLabels?: ReadonlyMap<string, string>;
  placeholder: string;
  emptyValue?: string;
  disabled?: boolean;
  error?: string;
  onChange: (value: string) => void;
}) {
  const selectValue = value || emptyValue || "";
  return (
    <div className="grid gap-2">
      <Label htmlFor={id}>{label}</Label>
      <Select
        value={selectValue}
        disabled={disabled}
        onValueChange={(nextValue) =>
          onChange(nextValue === emptyValue ? "" : nextValue)
        }
      >
        <SelectTrigger
          id={id}
          aria-label={label}
          aria-invalid={Boolean(error)}
          aria-describedby={error ? `${id}-error` : undefined}
          className={WARNING_INVALID_CLASS}
        >
          <SelectValue placeholder={placeholder} />
        </SelectTrigger>
        <SelectContent>
          {emptyValue && (
            <SelectItem value={emptyValue}>{placeholder}</SelectItem>
          )}
          {options.map((option) => (
            <SelectItem key={option} value={option}>
              {optionLabels?.get(option) ?? option}
            </SelectItem>
          ))}
        </SelectContent>
      </Select>
      <FieldError id={`${id}-error`} message={error} />
    </div>
  );
}
 
export function TextAreaField({
  id,
  label,
  value,
  placeholder,
  className,
  disabled = false,
  error,
  onChange,
}: {
  id: string;
  label: string;
  value: string;
  placeholder?: string;
  className?: string;
  disabled?: boolean;
  error?: string;
  onChange: (value: string) => void;
}) {
  return (
    <div className="grid gap-2">
      <Label htmlFor={id}>{label}</Label>
      <Textarea
        id={id}
        value={value}
        placeholder={placeholder}
        disabled={disabled}
        aria-invalid={Boolean(error)}
        aria-describedby={error ? `${id}-error` : undefined}
        className={cn(className, WARNING_INVALID_CLASS)}
        onChange={(event) => onChange(event.target.value)}
      />
      <FieldError id={`${id}-error`} message={error} />
    </div>
  );
}