Pigment.v0.1.0

TextField

A text input that allow users to input custom text entries with a keyboard.

Import

tsx
import { TextField } from "@kobalte/pigment";
tsx
import { TextField } from "@kobalte/pigment";

Basic usage

tsx
<TextField placeholder="E-mail" />
tsx
<TextField placeholder="E-mail" />

Default value

An initial, uncontrolled value can be provided using the defaultValue prop.

tsx
<TextField defaultValue="example@acme.com" placeholder="E-mail" />
tsx
<TextField defaultValue="example@acme.com" placeholder="E-mail" />

Controlled value

The value prop can be used to make the value controlled. The onChange event is fired when the user type into the input.

Value: example@acme.com

tsx
import { TextField } from "@kobalte/pigment";
import { createSignal } from "solid-js";
function ControlledExample() {
const [value, setValue] = createSignal("example@acme.com");
return (
<div>
<TextField value={value()} onChange={setValue} placeholder="E-mail" />
<p class="text-sm text-content-subtle mt-2">
Value: <span class="font-medium italic">{value()}</span>
</p>
</div>
);
}
tsx
import { TextField } from "@kobalte/pigment";
import { createSignal } from "solid-js";
function ControlledExample() {
const [value, setValue] = createSignal("example@acme.com");
return (
<div>
<TextField value={value()} onChange={setValue} placeholder="E-mail" />
<p class="text-sm text-content-subtle mt-2">
Value: <span class="font-medium italic">{value()}</span>
</p>
</div>
);
}

Multiline

Use the multiline prop to create a multiline text-field. Internally an HTML textarea will be rendered instead of an input.

tsx
<TextField multiline placeholder="Tell us more about you..." />
tsx
<TextField multiline placeholder="Tell us more about you..." />

Customization

Sizes

The TextField component comes in three sizes: sm, md (default) and lg.

tsx
<TextField size="sm" placeholder="Small" />
<TextField size="md" placeholder="Medium" />
<TextField size="lg" placeholder="Large" />
tsx
<TextField size="sm" placeholder="Small" />
<TextField size="md" placeholder="Medium" />
<TextField size="lg" placeholder="Large" />

Label

Use the label prop to provide an accessible label for the text-field.

tsx
<TextField label="E-mail" placeholder="example@acme.com" />
tsx
<TextField label="E-mail" placeholder="example@acme.com" />

Description

Use the description prop to associate additional help text with a text-field.

We'll never share your email
tsx
<TextField
label="E-mail"
description="We'll never share your email"
placeholder="example@acme.com"
/>
tsx
<TextField
label="E-mail"
description="We'll never share your email"
placeholder="example@acme.com"
/>

Icons

Use the leadingIcon and trailingIcon props to append icons to either side of the text-field's input.

tsx
<TextField
label="E-mail"
placeholder="example@acme.com"
leadingIcon={<MailIcon />}
/>
<TextField
label="Account number"
placeholder="000-00-0000"
trailingIcon={<QuestionCircleIcon />}
/>
tsx
<TextField
label="E-mail"
placeholder="example@acme.com"
leadingIcon={<MailIcon />}
/>
<TextField
label="Account number"
placeholder="000-00-0000"
trailingIcon={<QuestionCircleIcon />}
/>

If you want to create your own icon components check out the Icon documentation.

Sections

Use the leadingSection and trailingSection props to append more complex elements than icons to either side of the text-field's input.

Additionally, the leadingSectionWidth and trailingSectionWidth can be used to adjust the text-field's input padding to prevent overlapping with the input value.

tsx
<TextField
placeholder="Search..."
leadingSectionWidth={32}
leadingSection={
<IconButton variant="text" size="sm" class="my-1 ms-1" aria-label="Perform search">
<TablerSearchIcon />
</IconButton>
}
/>
<TextField
placeholder="E-mail"
trailingSectionWidth={94}
trailingSection={
<Button variant="solid" size="sm" class="my-1 me-1">
Subscribe
</Button>
}
/>
tsx
<TextField
placeholder="Search..."
leadingSectionWidth={32}
leadingSection={
<IconButton variant="text" size="sm" class="my-1 ms-1" aria-label="Perform search">
<TablerSearchIcon />
</IconButton>
}
/>
<TextField
placeholder="E-mail"
trailingSectionWidth={94}
trailingSection={
<Button variant="solid" size="sm" class="my-1 me-1">
Subscribe
</Button>
}
/>

Addons

The leadingAddon and trailingAddon can be used to append elements to either side of the text-field, outside the input.

@
inch
$
USD
tsx
<TextField placeholder="Twitter" leadingAddon="@" />
<TextField placeholder="Height" trailingAddon="inch" />
<TextField placeholder="Amount" leadingAddon="$" trailingAddon="USD" />
tsx
<TextField placeholder="Twitter" leadingAddon="@" />
<TextField placeholder="Height" trailingAddon="inch" />
<TextField placeholder="Amount" leadingAddon="$" trailingAddon="USD" />

If your addons are more complex than text, you can pass JSX elements as value for each prop.

Use Pigment's InputAddon component to get the same look and feel of "string addons":

tsx
<TextField
placeholder="Twitter"
leadingAddon={
<InputAddon class="px-2">
<TablerTwitterIcon class="text-xl" />
</InputAddon>
}
/>
<TextField
placeholder="Discord"
trailingAddon={
<InputAddon placement="trailing" class="px-2">
<TablerDiscordIcon class="text-xl" />
</InputAddon>
}
/>
tsx
<TextField
placeholder="Twitter"
leadingAddon={
<InputAddon class="px-2">
<TablerTwitterIcon class="text-xl" />
</InputAddon>
}
/>
<TextField
placeholder="Discord"
trailingAddon={
<InputAddon placement="trailing" class="px-2">
<TablerDiscordIcon class="text-xl" />
</InputAddon>
}
/>

Or use any component you like for a custom result:

tsx
<TextField
placeholder="E-mail"
trailingAddon={
<Button variant="solid" class="rounded-s-none -ms-px">
Subscribe
</Button>
}
/>
tsx
<TextField
placeholder="E-mail"
trailingAddon={
<Button variant="solid" class="rounded-s-none -ms-px">
Subscribe
</Button>
}
/>

Invalid state

Use the invalid prop to mark the text-field as invalid. Additionally, you can pass an extra error message using the errorMessage prop.

Please enter a valid email address
tsx
<TextField
placeholder="E-mail"
defaultValue="example@acme"
errorMessage="Please enter a valid email address"
invalid
/>
tsx
<TextField
placeholder="E-mail"
defaultValue="example@acme"
errorMessage="Please enter a valid email address"
invalid
/>

Disabled state

Use the disabled prop to disable the text-field.

We'll never share your email
tsx
<TextField
placeholder="example@acme.com"
label="E-mail"
description="We'll never share your email"
disabled
/>
tsx
<TextField
placeholder="example@acme.com"
label="E-mail"
description="We'll never share your email"
disabled
/>

HTML Form integration

[TODO]

API Reference

Component identifier

The name TextField can be used when providing default props and slot classes in the theme.

Props

PropDescription
refRef<HTMLInputElement | HTMLTextAreaElement>
A ref to the inner <input> or <textarea> element.
inputPropsComponentProps<"input"> | ComponentProps<"textarea">
Additional props to be spread on the inner <input> or <textarea> element.
type"text" | "email" | "tel" | "password" | "url" | "number" | "date"
When not multiline, the type of content handled by the text field.
placeholderstring
The placeholder displayed when the text-field is empty.
valuestring
The controlled value of the text-field.
defaultValuestring
The default value when initially rendered. Useful when you do not need to control the value.
onChange(value: string) => void
Event handler called when the value of the text-field changes.
namestring
The name of the text-field, used when submitting an HTML form. See MDN.
requiredboolean
default: false
Whether the user must fill the text-field before the owning form can be submitted.
disabledboolean
default: false
Whether the text-field is disabled.
readOnlyboolean
default: false
Whether the text-field can be checked but not changed by the user.
invalidboolean
default: false
Whether the text-field is invalid regarding the validation rules.
multilineboolean
default: false
Whether the text field should render a <textarea> instead of an <input>.
size"sm" | "md" | "lg"
default: "md"
The size of the text-field.
labelJSX.Element | (() => JSX.Element)
The label that gives the user information on the text-field.
descriptionJSX.Element | (() => JSX.Element)
The description that gives the user more information on the text-field.
errorMessageJSX.Element | (() => JSX.Element)
The error message that gives the user information about how to fix a validation error on the text-field.
errorIconJSX.Element | (() => JSX.Element)
The icon to show next to the error message.
leadingIconJSX.Element | (() => JSX.Element)
The icon to show before the input value.
trailingIconJSX.Element | (() => JSX.Element)
The icon to show after the input value.
leadingSectionJSX.Element | (() => JSX.Element)
The element to show before the input value, in place of the leadingIcon.
trailingSectionJSX.Element | (() => JSX.Element)
The element to show after the input value, in place of the trailingIcon.
leadingSectionWidthnumber
Width of leading section (in pixel), used to calculate the input padding-inline-start.
trailingSectionWidthnumber
Width of trailing section (in pixel), used to calculate the input padding-inline-end.
leadingAddonJSX.Element | (() => JSX.Element)
The element to show before the input element.
trailingAddonJSX.Element | (() => JSX.Element)
The element to show after the input element.
slotClassesPartial<Record<TextFieldSlots, string>>
Addiitonal CSS classes to be passed to the component slots.

Slots

NameCSS selectorRendered elementDescription
root.pg-text-field-rootdivRoot element
label.pg-text-field-labellabelLabel element
input.pg-text-field-inputinput | textareaInput element
leadingIcon.pg-text-field-leading-icondivLeading icon wrapper
trailingIcon.pg-text-field-trailing-icondivTrailing icon wrapper
leadingSection.pg-text-field-leading-sectiondivLeading section wrapper
trailingSection.pg-text-field-trailing-sectiondivTrailing section wrapper
description.pg-text-field-descriptiondivDescription/help text element
errorMessage.pg-text-field-error-messagedivError message element
errorIcon.pg-text-field-error-icondivError icon wrapper
Made in 🇫🇷 by Fabien MARIE-LOUISE.
Community