Skip to content

Commit

Permalink
docs(forms): join forms documentation pages (#1754)
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusps authored Jul 24, 2024
2 parents 93e423a + c6a5434 commit dd40f14
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 136 deletions.
137 changes: 137 additions & 0 deletions packages/docs/pages/guides/forms.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# Forms

## Form Fields

Single input components in Shoreline don't have labels or help messages by default, in order to build a field to be used on a form you must use the [Field](/components/field/api-reference) compound components set.

### Labels and help text

In order to be accessible, fields in a form should have labels, and in use cases where we want to provide more information fields must have helper text. In Shoreline you can use `Label` and `FieldDescription` to add those elements to a field.

```tsx
<Field>
<Label>Label</Label>
<InputElement />
<FieldDescription>Short description</FieldDescription>
</Field>
```

### Indicating error

Some fields might have error states when a value is invalid. You can use `FieldError` and the `error` prop in `Field` to indicate a form error.

Since `Field` provides context to its children components `FieldError` won't be rendered when the error prop is set to false.

```tsx
<Field error>
<Label>Label</Label>
<InputElement />
<FieldError>Short description</FieldError>
</Field>
```

## State

Shoreline doesn't currently offer state management for forms, you may choose your prefered method of state handling and validation.

### Controlled

Here's an example of a simple controlled form.

```tsx
export function Controlled() {
const [value, setValue] = useState('')
const [error, setError] = useState(false)

let onSubmit = (e) => {
e.preventDefault()
setError(false)


if (value === ''){
setError(true)
}

// perform submit action on backend
}

return (
<Stack>
<form onSubmit={onSubmit}>
<Field error={error}>
<Label>Poem</Label>
<Textarea name="poem" value={value} onChange={setValue} />
<FieldDescription>Write a poem</FieldDescription>
<FieldError>You must write something</FieldError>
</Field>
<Button type="submit" variant="primary">
Submit
</Button>
</form>
</Stack>
)
}

```

### Uncontrolled

```tsx
export function Uncontrolled() {

return (
<Stack>
<form>
<Field>
<Label>Poem</Label>
<Textarea name="poem" />
<FieldDescription>Write a poem</FieldDescription>
</Field>
<Button type="submit" variant="primary">
Submit
</Button>
</form>
</Stack>
)
}
```

## Examples

Examples of component structure for some common field use cases.

### Text field

```tsx
<Field>
<Label>Text</Label>
<Input />
<FieldDescription>Short description</FieldDescription>
<FieldError>Error Message</FieldError>
</Field>
```

### TextArea field

```tsx
<Field>
<Label>Multiline input</Label>
<Textarea />
<FieldDescription>Short description</FieldDescription>
<FieldError>Error Message</FieldError>
</Field>
```

### TextArea with char counter

```tsx
<Field>
<Label>Textarea with counter</Label>
<Textarea value={value} onChange={setValue} maxLength={120} />
<Flex justify="space-between">
<FieldDescription>Short description</FieldDescription>
<FieldCharCounter count={String(value).length} limit={120} />
</Flex>
<FieldError>Error Message</FieldError>
</Field>
```
5 changes: 0 additions & 5 deletions packages/docs/pages/guides/forms/_meta.json

This file was deleted.

39 changes: 0 additions & 39 deletions packages/docs/pages/guides/forms/field-examples.mdx

This file was deleted.

63 changes: 0 additions & 63 deletions packages/docs/pages/guides/forms/form-control.mdx

This file was deleted.

29 changes: 0 additions & 29 deletions packages/docs/pages/guides/forms/form-fields.mdx

This file was deleted.

0 comments on commit dd40f14

Please sign in to comment.