Form
A wrapper that turns a group of fields into a real HTML form, so Enter submits it and onSubmit runs your handler instead of reloading the page.
Preview
Code
Example.jsx
import { useState, useCallback } from 'react';
import { Card, BlockStack, InlineStack, Form, FormLayout, TextField, Select, Checkbox, Button, Banner, Text } from '@shopify/polaris';
const REASONS = [
{ label: 'Damaged on arrival', value: 'damaged' },
{ label: 'Wrong item shipped', value: 'wrong-item' },
{ label: 'Changed my mind', value: 'changed-mind' },
];
export const Example = () => {
const [orderNumber, setOrderNumber] = useState('#1042');
const [email, setEmail] = useState('casey@example.com');
const [reason, setReason] = useState('damaged');
const [restock, setRestock] = useState(true);
const [submission, setSubmission] = useState(null);
const handleSubmit = useCallback(() => {
setSubmission({ orderNumber, email, reason, restock });
}, [orderNumber, email, reason, restock]);
const reset = useCallback(() => setSubmission(null), []);
return (
<Card>
<BlockStack gap="400">
<Text as="h2" variant="headingMd">
Start a return
</Text>
{submission && (
<Banner tone="success" title="Return request created" onDismiss={reset}>
<Text as="p">
Return for order {submission.orderNumber} logged as
{' '}
{REASONS.find((item) => item.value === submission.reason).label.toLowerCase()}.
A label was emailed to {submission.email}.
{submission.restock ? ' The item will be restocked on arrival.' : ' The item will not be restocked.'}
</Text>
</Banner>
)}
<Form onSubmit={handleSubmit}>
<FormLayout>
<FormLayout.Group>
<TextField
label="Order number"
value={orderNumber}
onChange={setOrderNumber}
autoComplete="off"
/>
<TextField
label="Customer email"
type="email"
value={email}
onChange={setEmail}
autoComplete="email"
helpText="The return label is sent here."
/>
</FormLayout.Group>
<Select label="Reason for return" options={REASONS} value={reason} onChange={setReason} />
<Checkbox label="Restock the item when it arrives" checked={restock} onChange={setRestock} />
<InlineStack gap="200">
<Button submit variant="primary">
Create return
</Button>
<Button onClick={reset} disabled={!submission}>
Clear
</Button>
</InlineStack>
</FormLayout>
</Form>
</BlockStack>
</Card>
);
};