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

94.11% Statements 64/68
65.17% Branches 73/112
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186                                    744x           744x             744x                                   744x     824x 744x                                                       25x     11160x 824x 2976x       186x               186x                             186x     206x       186x 558x     10x     186x 186x     196x 236x 186x 10x   206x 186x 1824x     558x 558x 1116x 206x 744x       186x           186x                             206x 186x         186x 186x 196x 372x 1674x 206x 744x      
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,
  type NumericInputFormat,
} 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,
  numericFormat,
  trimTrailingZeros,
  maxLength,
  placeholder,
  className,
  disabled = false,
  onBlur,
}: {
  id: string;
  label: string;
  value: string;
  error?: string;
  onChange: (value: string) => void;
  type?: string;
  inputMode?: "decimal";
  numericFormat?: NumericInputFormat;
  trimTrailingZeros?: boolean;
  maxLength?: number;
  placeholder?: string;
  className?: string;
  disabled?: boolean;
  onBlur?: () => void;
}) {
  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}
          numericFormat={numericFormat}
          trimTrailingZeros={trimTrailingZeros}
          maxLength={maxLength}
          disabled={disabled}
          aria-invalid={Boolean(error)}
          aria-describedby={error ? `${id}-error` : undefined}
          className={cn(WARNING_INVALID_CLASS, className)}
          onValueChange={onChange}
          onBlur={onBlur}
        />
      ) : (
        <Input
          id={id}
          value={value}
          type={type}
          inputMode={inputMode}
          maxLength={maxLength}
          placeholder={placeholder}
          disabled={disabled}
          aria-invalid={Boolean(error)}
          aria-describedby={error ? `${id}-error` : undefined}
          className={cn(WARNING_INVALID_CLASS, className)}
          onChange={(event) => onChange(event.target.value)}
          onBlur={onBlur}
        />
      )}
      <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>
  );
}