puerto 0.23.0

Puerto — Rust full-stack DDD framework CLI. Scaffolds Clean Architecture projects with auto-wired DI, SQLx support, and zero manual wiring. Formerly puerto-framework.
import type { Esquemas } from "../../../api/cliente";
{%- if form_helpers %}
import {% if form_helpers_type_only %}type {% endif %}{ {{ form_helpers | join(sep=", ") }} } from "../../../compartido/formularios";
{%- endif %}
import { ErrorCampo } from "../../../compartido/ui/error-campo";
import type { Problema{{ pascal }} } from "../mensajes";

type {{ pascal }} = Esquemas["Dto{{ pascal }}"];
type Peticion{{ pascal }} = Esquemas["PeticionCrear{{ pascal }}"];

/** Lo que guardan los inputs: el texto de cada input, `checked` en una casilla. */
export interface ValoresFormulario{{ pascal }} {
{%- for f in fields %}
  {{ f.camel }}: {% if f.input == "checkbox" %}boolean{% else %}string{% endif %};
{%- endfor %}
}

/** Los inputs de `{{ ident }}`, vacíos cuando todavía no hay ninguno. */
{% if to_form_inline -%}
export function aFormulario{{ pascal }}({{ ident }}: {{ pascal }} | null): ValoresFormulario{{ pascal }} {
{%- else -%}
export function aFormulario{{ pascal }}(
  {{ ident }}: {{ pascal }} | null,
): ValoresFormulario{{ pascal }} {
{%- endif %}
  if ({{ ident }} === null) {
    return {
{%- for f in fields %}
      {{ f.camel }}: {{ f.empty_form }},
{%- endfor %}
    };
  }
  return {
{%- for f in fields %}
    {{ f.camel }}: {{ f.to_form }},
{%- endfor %}
  };
}

{% if targets -%}
/** Lo que ofrece el select de cada referencia. */
export interface OpcionesFormulario{{ pascal }} {
{%- for f in fields %}{% if f.input == "reference" %}
  {{ f.camel }}: OpcionReferencia[];
{%- endif %}{% endfor %}
}

{% endif -%}
/** Lo que mandan crear y actualizar: los mismos campos en los dos. */
{% if to_request_inline -%}
export function aPeticion{{ pascal }}(valores: ValoresFormulario{{ pascal }}): Peticion{{ pascal }} {
{%- else -%}
export function aPeticion{{ pascal }}(
  valores: ValoresFormulario{{ pascal }},
): Peticion{{ pascal }} {
{%- endif %}
  return {
{%- for f in fields %}
    {{ f.camel }}: {{ f.from_form }},
{%- endfor %}
  };
}

export function VistaFormulario{{ pascal }}({
  valores,
{%- if targets %}
  opciones,
{%- endif %}
  guardando,
  problema,
  alCambiar,
  alEnviar,
}: {
  valores: ValoresFormulario{{ pascal }};
{%- if targets %}
  opciones: OpcionesFormulario{{ pascal }};
{%- endif %}
  guardando: boolean;
  /** Lo que dijo la API del último envío: se muestra bajo su input. */
  problema: Problema{{ pascal }} | null;
  alCambiar: (valores: ValoresFormulario{{ pascal }}) => void;
  alEnviar: () => void;
}) {
  return (
    <form
      className="space-y-2"
      onSubmit={(event) => {
        event.preventDefault();
        alEnviar();
      }}
    >
{%- for f in fields %}
{%- if f.input == "checkbox" %}
      <label className="label">
        <input
          className="checkbox"
          type="checkbox"
          checked={valores.{{ f.camel }}}
          onChange={(event) => alCambiar({ ...valores, {{ f.camel }}: event.target.checked })}
        />
        {{ f.label }}
      </label>
{%- elif f.input == "select" %}
      <label className="fieldset">
        <span className="fieldset-legend">{{ f.label }}</span>
        <select
          className="select w-full"
          aria-invalid={problema?.campo === "{{ f.camel }}"}
{%- if f.required %}
          required
{%- endif %}
          value={valores.{{ f.camel }}}
          onChange={(event) => alCambiar({ ...valores, {{ f.camel }}: event.target.value })}
        >
          <option value="">{{ f.placeholder }}</option>
{%- for o in f.options %}
          <option value="{{ o.value }}">{{ o.label }}</option>
{%- endfor %}
        </select>
      </label>
      <ErrorCampo problema={problema} campo="{{ f.camel }}" />
{%- elif f.input == "reference" %}
      <label className="fieldset">
        <span className="fieldset-legend">{{ f.label }}</span>
        <select
          className="select w-full"
          aria-invalid={problema?.campo === "{{ f.camel }}"}
{%- if f.required %}
          required
{%- endif %}
          value={valores.{{ f.camel }}}
          onChange={(event) => alCambiar({ ...valores, {{ f.camel }}: event.target.value })}
        >
          <option value="">{{ f.placeholder }}</option>
          {opciones.{{ f.camel }}.map((opcion) => (
            <option key={opcion.id} value={opcion.id}>
              {opcion.etiqueta}
            </option>
          ))}
        </select>
      </label>
      <ErrorCampo problema={problema} campo="{{ f.camel }}" />
{%- elif f.input == "textarea" %}
      <label className="fieldset">
        <span className="fieldset-legend">{{ f.label }}</span>
        <textarea
          className="textarea w-full"
          aria-invalid={problema?.campo === "{{ f.camel }}"}
          placeholder="clave=valor, una por línea"
          value={valores.{{ f.camel }}}
          onChange={(event) => alCambiar({ ...valores, {{ f.camel }}: event.target.value })}
        />
      </label>
      <ErrorCampo problema={problema} campo="{{ f.camel }}" />
{%- else %}
      <label className="fieldset">
        <span className="fieldset-legend">{{ f.label }}</span>
        <input
          className="input w-full"
          type="{{ f.input }}"
          aria-invalid={problema?.campo === "{{ f.camel }}"}
{%- if f.decimal %}
          step="any"
{%- endif %}
{%- if f.required %}
          required
{%- endif %}
          value={valores.{{ f.camel }}}
          onChange={(event) => alCambiar({ ...valores, {{ f.camel }}: event.target.value })}
        />
      </label>
      <ErrorCampo problema={problema} campo="{{ f.camel }}" />
{%- endif %}
{%- endfor %}
      <button type="submit" className="btn btn-primary" disabled={guardando}>
        Guardar
      </button>
    </form>
  );
}