Form component is used to ask people for information, such as their contact details or a delivery address.

Open in new tab
import { Button, Form, Input, Textarea } from "@kesko/components";

export default () => (
  <>
    <h2>Contact us</h2>
    <Form onSubmit={event => event.preventDefault()}>
      <Input label="Name" name="name" autoComplete="name" required hideRequired />
      <Input label="Email" name="email" type="email" autoComplete="email" required hideRequired />
      <Textarea label="Message" name="message" required hideRequired />
      <Button type="submit">Send</Button>
    </Form>
  </>
);
import { Button, Form, Input, Stack } from "@kesko/components";

export default () => (
  <>
    <h2>Store details</h2>
    <Form onSubmit={event => event.preventDefault()}>
      <Input label="Store name" name="store" required hideRequired />
      <Input label="Phone number" name="phone" type="tel" autoComplete="tel" />
      <Stack direction="row" gap="xs">
        <Button type="submit">Save</Button>
        <Button type="reset" appearance="outline">Reset</Button>
      </Stack>
    </Form>
  </>
);
import { Button, Checkbox, Fieldset, Form, Grid, Input, Select, Textarea } from "@kesko/components";

export default () => (
  <>
    <h2>Contact us</h2>
    <Form gap="xl" onSubmit={event => event.preventDefault()}>
      <Fieldset label="How can we help?">
        <Grid style={{ "--k-grid-min": "max(16rem, 34%)" }}>
          <Checkbox label="I want a test drive" name="interest" value="test-drive" defaultChecked />
          <Checkbox label="I want an offer" name="interest" value="offer" />
          <Checkbox label="I want more information" name="interest" value="information" />
          <Checkbox label="I want a price for private leasing" name="interest" value="leasing" />
        </Grid>
      </Fieldset>
      <Fieldset label="My details">
        <Grid style={{ "--k-grid-min": "max(16rem, 34%)" }}>
          <Input label="First name" name="first-name" autoComplete="given-name" required />
          <Input label="Last name" name="last-name" autoComplete="family-name" required />
          <Input label="Phone number" name="phone" type="tel" autoComplete="tel" />
          <Input label="Email" name="email" type="email" autoComplete="email" required />
        </Grid>
      </Fieldset>
      <Fieldset label="More information">
        <Select label="Location" name="location" placeholder="Choose a location" required>
          <option value="helsinki">Helsinki</option>
          <option value="tampere">Tampere</option>
          <option value="turku">Turku</option>
        </Select>
        <Textarea label="Anything else we should know?" name="message" />
      </Fieldset>
      <Button type="submit">Send contact request</Button>
    </Form>
  </>
);
import { Button, Form, Input } from "@kesko/components";

export default () => (
  <>
    <h2>Your name</h2>
    <Form gap="sm" onSubmit={event => event.preventDefault()}>
      <Input label="First name" name="first-name" autoComplete="given-name" required hideRequired />
      <Input label="Last name" name="last-name" autoComplete="family-name" required hideRequired />
      <Button type="submit">Save</Button>
    </Form>
  </>
);
import { Button, Fieldset, Form, Input, Radio } from "@kesko/components";

export default () => (
  <>
    <h2>Delivery</h2>
    <Form gap="xl" onSubmit={event => event.preventDefault()}>
      <Fieldset label="Delivery address">
        <Input label="Street address" name="street" autoComplete="street-address" required hideRequired />
        <Input label="Postal code" name="postal" inputMode="numeric" autoComplete="postal-code" required hideRequired />
      </Fieldset>
      <Fieldset label="If nobody is home">
        <Radio label="Leave the order at the door" name="absent" value="door" defaultChecked />
        <Radio label="Take the order back to the store" name="absent" value="store" />
      </Fieldset>
      <Button type="submit">Continue</Button>
    </Form>
  </>
);
import { Button, Form, Input, VisuallyHidden } from "@kesko/components";
import { IconShow } from "@kesko/iconography/react";

export default () => (
  <>
    <h2>Log in</h2>
    <Form onSubmit={event => event.preventDefault()}>
      <Input label="Email" name="email" type="email" defaultValue="matti.meikalainen@example.com" disabled />
      <Input label="Password" name="password" type="password" defaultValue="password" disabled>
        <Button slot="end" appearance="text" square size="sm" disabled>
          <IconShow />
          <VisuallyHidden>Show password</VisuallyHidden>
        </Button>
      </Input>
      <Button type="submit" loading style={{ inlineSize: "8rem" }}>
        Continue
      </Button>
    </Form>
  </>
);
import { useState } from "react";
import { Button, Form, Input, Stack, 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 (
    <>
      <h2 id="login">Log in</h2>
      <Form aria-labelledby="login" onSubmit={event => event.preventDefault()}>
        <Input label="Email" name="email" type="email" autoComplete="username" required hideRequired />
        <Input
          label="Password"
          name="password"
          type={shown ? "text" : "password"}
          autoComplete="current-password"
          required
          hideRequired
        >
          <Button
            slot="end"
            appearance="text"
            square
            size="sm"
            aria-describedby="password-toggle"
            onClick={() => setShown(!shown)}
          >
            {shown ? <IconHide /> : <IconShow />}
            <VisuallyHidden>{toggleLabel}</VisuallyHidden>
          </Button>
        </Input>
        <Stack direction="row" gap="xs" alignment="center">
          <Button type="submit" style={{ inlineSize: "8rem" }}>
            Continue
          </Button>
          <Button appearance="plain" href="#forgot-password">
            Forgot password?
          </Button>
        </Stack>
      </Form>
      <Tooltip id="password-toggle">{toggleLabel}</Tooltip>
    </>
  );
};
import { Button, Fieldset, Form, Grid, Input, Radio, Select, Stack, Switch, Textarea } from "@kesko/components";

export default () => (
  <>
    <h2>Request an offer</h2>
    <Form gap="xl" onSubmit={event => event.preventDefault()}>
      <Switch label="I want to trade in my car" name="trade-in" />
      <Fieldset label="My details">
        <Grid style={{ "--k-grid-min": "max(16rem, 34%)" }}>
          <Input label="First name" name="first-name" autoComplete="given-name" required hideRequired />
          <Input label="Last name" name="last-name" autoComplete="family-name" required hideRequired />
          <Input label="Phone number" name="phone" type="tel" autoComplete="tel" required hideRequired />
          <Input label="Email" name="email" type="email" autoComplete="email" required hideRequired />
        </Grid>
      </Fieldset>
      <Fieldset label="Contact me primarily by">
        <Fieldset label="Contact method" hideLabel>
          <Stack direction="row" gap="lg">
            <Radio label="Phone" name="contact" value="by-phone" required hideRequired />
            <Radio label="Email" name="contact" value="by-email" required hideRequired />
          </Stack>
        </Fieldset>
        <Select label="Location" name="location" placeholder="Choose a location" required hideRequired>
          <option value="helsinki">Helsinki</option>
          <option value="tampere">Tampere</option>
          <option value="turku">Turku</option>
        </Select>
      </Fieldset>
      <Fieldset label="More information">
        <Textarea label="Anything else we should know?" name="message" />
      </Fieldset>
      <Button type="submit">Send request</Button>
    </Form>
  </>
);
import { Button, Form, Input } from "@kesko/components";

export default () => (
  <>
    <h2>Create account</h2>
    <Form onSubmit={event => event.preventDefault()}>
      <Input label="Work email" name="email" type="email" required hideRequired />
      <Input label="Username" name="username" defaultValue="matti.meikalainen" error="That username is already taken." />
      <Button type="submit">Create account</Button>
    </Form>
  </>
);

Properties #

NameDescriptionTypeDefault
gap

Gap between the fields, mapped to the --k-space- scale.

"3xs" | "2xs" | "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl""md"
layout

How the form adapts to its width. responsive makes the buttons fill the width when the form is narrower than --k-form-breakpoint, fixed keeps the same layout at every size.

"responsive" | "fixed""responsive"
noValidate

Switches off the browser's own validation, so the form submits without checking required, type and the other constraints on its fields.

boolean—
action

Where the form sends its data. Leave it out when onSubmit handles the data.

FormHTMLAttributes<HTMLFormElement>["action"]—
method

The HTTP method used with action.

"get" | "post" | "dialog"—
encType

How the data is encoded when it is sent to action. A form with a file upload needs "multipart/form-data".

"application/x-www-form-urlencoded" | "multipart/form-data" | "text/plain"—
target

Where the response to action opens, such as "_blank" for a new tab. Also takes the name of a frame or window.

"_self" | "_blank" | "_parent" | "_top" | string—
autoComplete

Set to "off" to stop the browser from offering saved values for the fields. Each field can still set its own autoComplete.

"on" | "off"—
name

The name of the form, for finding it through document.forms.

string—
role

Set to "search" when the form searches the site or the page, so assistive technology lists it as the search landmark.

FormHTMLAttributes<HTMLFormElement>["role"]—
aria-labelledby

id of the heading that names this form. A named form becomes a landmark, which helps when a page has more than one form.

string—
aria-label

Accessible name for the form. Use it only when there is no visible heading to point aria-labelledby at.

string—

Events #

NameDescriptionType
onSubmit

Called when the form is submitted. Unless noValidate is set, that happens only once every field is valid. Call event.preventDefault() to handle the data yourself.

(event: SyntheticEvent<HTMLFormElement>) => void
onReset

Called when the form is reset, for example by a button with type="reset".

(event: SyntheticEvent<HTMLFormElement>) => 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-form-gap

Overrides the space between the fields.

var(--k-space-md)
--k-form-breakpoint

Overrides the form width below which the buttons fill the width.

30rem

Accessibility #

Form is built on a native <form> element, so people can send it with Enter, and the browser validates the fields and offers to fill them in.

  • Give the form a heading. Put one above it, or rely on the title of the dialog it sits in, so people know what they are filling in. When the form is a part of a longer page, use aria-labelledby so screen reader users can find it among the page’s landmarks.
  • Group related fields with Fieldset. Form holds everything people fill in and send together. Fieldset names the parts inside it that share one meaning, such as an address or one choice.
  • Keep messages on the fields. Form has no errors of its own. An error about one field goes in that field’s error, and an error about a group goes on its Fieldset, so each one is read with the thing it is about.
  • Let the browser validate. Fields marked required or given a type are checked on submit, and the first one that fails gets focus and shows its message in the page’s language. A standard form needs no form library and no wrapper components, see Forms. Pass noValidate only when you check the values yourself.
  • Always include a submit button. A Button with type="submit" lets people send the form with Enter from a text field.

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


Usage #

There are two necessary parts to using the Form 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 Form:

import { Form } 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 #