TextField
A single or multi-line text input for collecting typed data, with support for labels, help text, validation errors, prefixes, suffixes and connected actions.
Preview
Code
Example.jsx
import { useState, useCallback } from 'react';
import { Card, BlockStack, FormLayout, TextField, Button, Text, Badge, InlineStack } from '@shopify/polaris';
const SKU_PATTERN = /^[A-Z]{2,4}-\d{3,5}$/;
const TAKEN_HANDLES = ['aurora-ceramic-mug', 'ceramic-mug'];
export const Example = () => {
const [title, setTitle] = useState('Aurora ceramic mug');
const [description, setDescription] = useState('Hand-glazed stoneware mug that holds 350 ml. Dishwasher and microwave safe.');
const [price, setPrice] = useState('24.00');
const [weight, setWeight] = useState('420');
const [sku, setSku] = useState('mug-01');
const [quantity, setQuantity] = useState('12');
const [handle, setHandle] = useState('aurora-ceramic-mug');
const [handleStatus, setHandleStatus] = useState(null);
const skuError = SKU_PATTERN.test(sku) ? undefined : 'Use a SKU like MUG-001 so it sorts correctly in exports.';
const checkHandle = useCallback(() => {
setHandleStatus(TAKEN_HANDLES.includes(handle.trim()) ? 'taken' : 'available');
}, [handle]);
const changeHandle = useCallback((value) => {
setHandle(value);
setHandleStatus(null);
}, []);
return (
<Card>
<BlockStack gap="400">
<Text as="h2" variant="headingMd">
Product details
</Text>
<FormLayout>
<TextField
label="Title"
value={title}
onChange={setTitle}
autoComplete="off"
maxLength={70}
showCharacterCount
helpText="Shown to customers on the product page and in search results."
/>
<TextField
label="Description"
value={description}
onChange={setDescription}
autoComplete="off"
multiline={4}
/>
<FormLayout.Group>
<TextField
label="Price"
value={price}
onChange={setPrice}
autoComplete="off"
type="currency"
prefix="$"
suffix="USD"
helpText="Customers see this before tax."
/>
<TextField
label="Weight"
value={weight}
onChange={setWeight}
autoComplete="off"
type="number"
suffix="g"
/>
</FormLayout.Group>
<FormLayout.Group>
<TextField
label="SKU"
value={sku}
onChange={setSku}
autoComplete="off"
error={skuError}
/>
<TextField
label="Quantity"
value={quantity}
onChange={setQuantity}
autoComplete="off"
type="number"
min={0}
max={999}
helpText="Available at Main warehouse."
/>
</FormLayout.Group>
<TextField
label="Handle"
value={handle}
onChange={changeHandle}
autoComplete="off"
prefix="/products/"
connectedRight={<Button onClick={checkHandle}>Check availability</Button>}
/>
</FormLayout>
{handleStatus && (
<InlineStack gap="200" blockAlign="center">
<Badge tone={handleStatus === 'taken' ? 'critical' : 'success'}>
{handleStatus === 'taken' ? 'Taken' : 'Available'}
</Badge>
<Text as="span" tone="subdued">
{handleStatus === 'taken'
? 'Another product already uses this handle.'
: 'This handle is free to use.'}
</Text>
</InlineStack>
)}
</BlockStack>
</Card>
);
};