Input with prefix and suffix

An input with a fixed piece before or after it: a currency sign, a unit, a web address or a phone country code. The whole row is one well and takes the focus ring.

Project settings

$ USD
https:// .example.com
Prices are shown without tax.

Options

Screen
Colour

Code

import { useId } from "react";
import "./input-affix.css";

/* An input with a fixed piece before or after it.
   prefix and suffix are text; codes turns the prefix into a country code picker.

   <AffixInput label="Budget" prefix="$" suffix="USD" defaultValue="4,500" /> */
export function AffixInput({ label, prefix, suffix, codes, code, onCode, ...props }) {
  const id = useId();
  return (
    <div className="dh-affix">
      <label htmlFor={id}>{label}</label>
      <div>
        {codes ? (
          <select aria-label="Country code" value={code} onChange={(event) => onCode?.(event.target.value)}>
            {codes.map((item) => (
              <option key={item}>{item}</option>
            ))}
          </select>
        ) : (
          prefix && <b>{prefix}</b>
        )}
        <input id={id} type="text" {...props} />
        {suffix && <b>{suffix}</b>}
      </div>
    </div>
  );
}

export function Example() {
  return (
    <section className="dh-affix-card">
      <h3>Project settings</h3>
      <div className="dh-affix-stack">
        <AffixInput label="Budget" prefix="$" suffix="USD" inputMode="decimal" defaultValue="4,500" />
        <AffixInput label="Site address" prefix="https://" suffix=".example.com" placeholder="studio" />
        <AffixInput label="Phone" codes={["+44", "+1", "+49", "+61"]} type="tel" placeholder="7700 900123" />
      </div>
      <footer>
        <span>Prices are shown without tax.</span>
        <button type="button">Save</button>
      </footer>
    </section>
  );
}

Project settings

$ USD
https:// .example.com
Prices are shown without tax.