Input component allows users to enter text that fits on a single line. For longer text, use the Textarea component. When placed next to a Button, give both the same size so their heights match.

Open in new tab
import { Input } from "@kesko/components";

export default () => <Input label="Label" name="input" placeholder="Placeholder" />;
import { Input } from "@kesko/components";

export default () => (
  <Input
    label="K-Plussa card number"
    name="card"
    inputMode="numeric"
    autoComplete="off"
    description="The 16 digits printed on the front of the card."
  />
);
import { Input } from "@kesko/components";

export default () => <Input label="Email" type="email" name="email" autoComplete="email" />;
import { Input } from "@kesko/components";

const subtle = {
  "--k-label-text": "var(--k-color-text-dim)",
  "--k-label-weight": "var(--k-font-weight-medium)",
  "--k-label-required": "var(--k-color-status-error)",
};

export default () => <Input label="Custom label" name="custom" required style={subtle} />;
import { Input, Stack } from "@kesko/components";

export default () => (
  <Stack direction="row">
    <Input
      label="Postal code"
      inputMode="numeric"
      autoComplete="postal-code"
      style={{ "--k-input-inline-size": "9rem" }}
    />
    <Input label="Year" inputMode="numeric" style={{ "--k-input-inline-size": "6rem" }} />
    <Input label="City" autoComplete="address-level2" style={{ "--k-input-inline-size": "16rem" }} />
  </Stack>
);
import { Input, Stack } from "@kesko/components";
import { IconLockClosed, IconSearch } from "@kesko/iconography/react";

export default () => (
  <Stack>
    <Input label="Store" name="store" defaultValue="K-Citymarket Sello" disabled />
    <Input label="Search products" type="search" placeholder="Search" disabled>
      <IconSearch slot="start" />
    </Input>
    <Input label="Customer number" name="customer" defaultValue="1234567" disabled>
      <IconLockClosed slot="start" />
    </Input>
  </Stack>
);
import { Input } from "@kesko/components";
import { IconSearch } from "@kesko/iconography/react";

export default () => (
  <Input label="Search products" hideLabel type="search" name="q" placeholder="Search products">
    <IconSearch slot="start" />
  </Input>
);
import { Input, Stack } from "@kesko/components";
import { IconCalendar, IconLockClosed, IconMail, IconSearch } from "@kesko/iconography/react";

export default () => (
  <Stack>
    <Input label="Search products" type="search" placeholder="Search">
      <IconSearch slot="start" />
    </Input>
    <Input label="Password" type="password" autoComplete="current-password">
      <IconLockClosed slot="start" />
    </Input>
    <Input label="Email" type="email" autoComplete="email" placeholder="name@kesko.fi">
      <IconMail slot="end" />
    </Input>
    <Input label="Delivery date" placeholder="DD.MM.YYYY" inputMode="numeric">
      <IconCalendar slot="end" />
    </Input>
  </Stack>
);
import { useEffect, useRef } from "react";
import { Input } from "@kesko/components";
import { IconPaymentCreditCard } from "@kesko/iconography/react";

const cdn = "https://cdn.jsdelivr.net/npm";
const digit = /\d/;
const cardMask = [digit, digit, digit, digit, " ", digit, digit, digit, digit, " ", digit, digit, digit, digit, " ", digit, digit, digit, digit];

export default () => {
  const input = useRef<HTMLInputElement>(null);

  useEffect(() => {
    let cancelled = false;
    let mask: { destroy(): void } | undefined;
    import(/* @vite-ignore */ `${cdn}/@maskito/core@5.5.0/+esm`)
      .then(({ Maskito }) => {
        if (!cancelled && input.current) {
          mask = new Maskito(input.current, { mask: cardMask });
        }
      })
      .catch(() => {});
    return () => {
      cancelled = true;
      mask?.destroy();
    };
  }, []);

  return (
    <Input
      ref={input}
      label="K-Plussa card number"
      name="plussa"
      inputMode="numeric"
      autoComplete="off"
      placeholder="0000 0000 0000 0000"
      description="The 16 digits printed on the front of the card."
    >
      <IconPaymentCreditCard slot="end" />
    </Input>
  );
};
import { useState } from "react";
import { Button, Input, Tooltip, VisuallyHidden } from "@kesko/components";
import { IconHide, IconShow } from "@kesko/iconography/react";

export default () => {
  const [shown, setShown] = useState(false);
  const toggleLabel = shown ? "Hide password" : "Show password";
  return (
    <>
      <Input
        label="Password"
        name="password"
        type={shown ? "text" : "password"}
        autoComplete="current-password"
        defaultValue="password"
      >
        <Button
          slot="end"
          appearance="text"
          square
          size="sm"
          aria-describedby="password-toggle"
          onClick={() => setShown(!shown)}
        >
          {shown ? <IconHide /> : <IconShow />}
          <VisuallyHidden>{toggleLabel}</VisuallyHidden>
        </Button>
      </Input>
      <Tooltip id="password-toggle">{toggleLabel}</Tooltip>
    </>
  );
};
import { Input, Stack } from "@kesko/components";
import { IconSearch } from "@kesko/iconography/react";

export default () => (
  <Stack>
    <Input label="Search products" type="search" placeholder="Search">
      <IconSearch slot="start" />
    </Input>
    <Input label="Weight (kg)" inputMode="decimal" placeholder="0">
      <span slot="end">kg</span>
    </Input>
    <Input label="Price (€)" inputMode="decimal" placeholder="0,00">
      <span slot="start">€</span>
    </Input>
  </Stack>
);
import { Input, Stack } from "@kesko/components";

export default () => (
  <Stack>
    <Input label="Customer number" name="customer" defaultValue="1234567" readOnly />
    <Input label="Store" name="store" defaultValue="K-Citymarket Sello" readOnly />
  </Stack>
);
import { Input, Stack } from "@kesko/components";

export default () => (
  <Stack>
    <Input label="First name" name="first-name" autoComplete="given-name" required />
    <Input label="Last name" name="last-name" autoComplete="family-name" required hideRequired />
  </Stack>
);
import { Input, Stack } from "@kesko/components";

export default () => (
  <Stack>
    <Input label="Large" size="lg" placeholder="Placeholder" />
    <Input label="Medium" placeholder="Placeholder" />
  </Stack>
);
import { useRef } from "react";
import { Button, Input, Stack } from "@kesko/components";

export default () => {
  const input = useRef<HTMLInputElement>(null);

  return (
    <Stack>
      <Input ref={input} label="Store" name="store" defaultValue="K-Citymarket Sello" />
      <Button
        size="sm"
        appearance="outline"
        onClick={() => {
          input.current?.select();
          input.current?.focus();
        }}
      >
        Select all
      </Button>
    </Stack>
  );
};
import { Button, Input, Stack } from "@kesko/components";

export default () => (
  <form onSubmit={event => event.preventDefault()}>
    <Stack>
      <Input label="Work email" name="email" type="email" required placeholder="name@kesko.fi" />
      <Input label="Username" name="username" defaultValue="matti.meikalainen" error="That username is already taken." />
      <Input label="Postal code" name="postal" defaultValue="00100" success="We deliver to this address." />
      <Button type="submit">Submit</Button>
    </Stack>
  </form>
);

Properties #

NameDescriptionTypeDefault
label

Label for the input. Always provide one, even when it is hidden with hideLabel.

ReactNode—
hideLabel

Hides the label visually while keeping it available to assistive technology.

booleanfalse
description

Assistive text shown below the control, such as the expected format.

ReactNode—
error

Optional error message for the input, shown instead of the built-in validation message. Also marks the control invalid for assistive technology.

ReactNode—
success

Optional success message for the input. Shown only while there is no error. A plain string is also announced to screen readers when it appears.

ReactNode—
size

The size of the input.

"md" | "lg""md"
type

The type of the input.

"text" | "email" | "password" | "tel" | "url" | "search""text"
name

The name the value is submitted under.

string—
value

The value of the control. Pair with onChange to control the field.

string—
defaultValue

The starting value of an uncontrolled field.

string—
form

The id of the form the control submits with. Only needed when the control sits outside that form.

string—
placeholder

Short hint shown while the control is empty. Not a substitute for a label.

string—
inputMode

The on-screen keyboard to show on touch devices, such as decimal for weights and prices or numeric for card numbers.

"none" | "text" | "decimal" | "numeric" | "tel" | "search" | "email" | "url"—
pattern

A regular expression the whole value must match before the form submits.

string—
minLength

The fewest characters the value may have before the form submits.

number—
maxLength

The most characters the control accepts.

number—
required

Requires a value before the form submits. Draws the marker on the label and is announced to assistive technology.

booleanfalse
hideRequired

Hides the required marker on the label while keeping required on the control. Use it when the form says once that every field is required.

booleanfalse
readOnly

Stops the value being edited while keeping the control focusable and its value submitted. Reach for disabled to drop it from the form entirely.

boolean—
disabled

Disables the control. Blocks interaction and drops the value from the submitted data.

boolean—
autoComplete

Tells the browser what the value is for, so it can offer to fill it in. Worth setting on anything asking for personal or payment details.

string—

Events #

NameDescriptionType
onChange

Handler called as the value changes. Fires on every keystroke.

(event: ChangeEvent<HTMLInputElement>) => void
onInput

Handler called on the native input event. Fires alongside onChange; reach for it only when you need the raw event.

(event: SyntheticEvent<HTMLInputElement>) => void
onSelect

Handler called when the selected text changes.

(event: SyntheticEvent<HTMLInputElement>) => void
onFocus

Handler called when the control receives focus.

(event: FocusEvent<HTMLInputElement>) => void
onBlur

Handler called when the control loses focus.

(event: FocusEvent<HTMLInputElement>) => void

Styles #

CSS custom properties provide finer-grained control than properties. See Styling API for more details. Prefer the Properties API when it suits your need.

NameDescriptionDefault
--k-input-bg

Overrides the background color.

var(--k-color-bg-base)
--k-input-text

Overrides the text color.

var(--k-color-text-base)
--k-input-border

Overrides the border color in every state.

var(--k-color-border-control)
--k-input-radius

Overrides the border radius.

var(--k-radius-md)
--k-input-inline-size

Overrides the width of the field.

100%

Accessibility #

Input is built on a native <input> and <label>, so it behaves as expected with assistive technologies.

  • Always pass a label. A screen reader reads it as the name, and clicking it focuses the control. If there is no room for it, pass hideLabel rather than leaving it out.
  • placeholder is a hint, not a description. Don’t put anything people need in order to fill in the field in the placeholder. It disappears as soon as they type. Use description for that instead.
  • Say what the input accepts. Use description for format hints such as the number of digits, and autoComplete for anything the browser can fill in: names, addresses, emails, card numbers. Turn spelling suggestions off with spellCheck={false} only where they make no sense.
  • start and end slots are decorative. A unit or icon in a slot="start" or slot="end" is not read as part of the control’s name, and an icon should carry aria-hidden. Put units in the label, for example “Weight (kg)”, or reference them with aria-describedby.
  • Interactive content needs a name and a target. A Button in a start or end slot, such as a password toggle, needs an accessible name and a target of at least 24×24 pixels. The Button sizes already meet that.
  • Validation is built in. Set required, type, pattern or minLength and the field checks itself on submit, with a message in the page’s language. No form library is needed, see Forms.
  • Avoid disabled for validation. A disabled control leaves the tab order and cannot explain itself. Keep it enabled and let the message say what is wrong. Use readOnly for a value people need to see and copy but not edit.
  • Check contrast if you override colors. Text needs 4.5:1 against --k-input-bg, and --k-input-border needs 3:1 against the surface the field sits on. Verify with the WebAIM contrast checker.

See the accessibility guidelines and the A11Y checklist for more details.


Usage #

There are two necessary parts to using the Input component. The @kesko/components package and the CSS Framework are required. Everything else is optional:

npm install @kesko/components @kesko/css

Once installed, load the CSS Framework in your app’s entry point or root layout:

import "@kesko/css";

Then, you can import the Input:

import { Input } from "@kesko/components";

Projects without React can get the same styles from @kesko/css by importing the components stylesheet as well:

@import "@kesko/css";
@import "@kesko/css/components";

Resources #