Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs][joy-ui] Add ref usage instructions on the Textarea page #39277

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/data/joy/components/textarea/TextareaRef.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as React from 'react';
import Button from '@mui/joy/Button';
import Textarea from '@mui/joy/Textarea';
import Stack from '@mui/joy/Stack';

export default function TextareaRef() {
const textareaRef = React.useRef(null);

const handleTextareaFocus = () => {
textareaRef.current?.focus();
};

return (
<Stack direction="row" gap={1}>
<Textarea
placeholder="Placeholder"
slotProps={{ textarea: { ref: textareaRef } }}
/>
<Button onClick={handleTextareaFocus}>Focus textarea element</Button>
</Stack>
);
}
22 changes: 22 additions & 0 deletions docs/data/joy/components/textarea/TextareaRef.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as React from 'react';
import Button from '@mui/joy/Button';
import Textarea from '@mui/joy/Textarea';
import Stack from '@mui/joy/Stack';

export default function TextareaRef() {
const textareaRef = React.useRef<HTMLTextAreaElement | null>(null);

const handleTextareaFocus = () => {
textareaRef.current?.focus();
};

return (
<Stack direction="row" gap={1}>
<Textarea
placeholder="Placeholder"
slotProps={{ textarea: { ref: textareaRef } }}
/>
<Button onClick={handleTextareaFocus}>Focus textarea element</Button>
</Stack>
);
}
5 changes: 5 additions & 0 deletions docs/data/joy/components/textarea/TextareaRef.tsx.preview
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Textarea
placeholder="Placeholder"
slotProps={{ textarea: { ref: textareaRef } }}
/>
<Button onClick={handleTextareaFocus}>Focus textarea element</Button>
6 changes: 6 additions & 0 deletions docs/data/joy/components/textarea/textarea.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ It's usually more common to see textarea components using decorators at the top

{{"demo": "TextareaDecorators.js"}}

### HTML textarea ref

Use the `slotProps.textarea` attribute to pass props to the `ref` and other [supported HTML attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea#attributes) to the textarea element.

{{"demo": "TextareaRef.js"}}

## Accessibility

In order for the textarea to be accessible, **it should be linked to a label**.
Expand Down