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
20 changes: 20 additions & 0 deletions docs/data/joy/components/textarea/TextareaRef.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as React from 'react';
import Box from '@mui/joy/Box';
import Textarea from '@mui/joy/Textarea';
import { useRef } from 'react';

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

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

return (
<Box>
danilo-leal marked this conversation as resolved.
Show resolved Hide resolved
<Textarea ref={textareaRef} placeholder="Textarea" />
<button onClick={handleTextareaFocus}>Focus Textarea</button>
danilo-leal marked this conversation as resolved.
Show resolved Hide resolved
</Box>
);
}

20 changes: 20 additions & 0 deletions docs/data/joy/components/textarea/TextareaRef.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as React from 'react';
import Box from '@mui/joy/Box';
import Textarea from '@mui/joy/Textarea';
import { useRef } from 'react';

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

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

return (
<Box>
<Textarea ref={textareaRef} placeholder="Textarea" />
<button onClick={handleTextareaFocus}>Focus Textarea</button>
</Box>
);
}

15 changes: 15 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,21 @@ It's usually more common to see textarea components using decorators at the top

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

### Textarea ref
danilo-leal marked this conversation as resolved.
Show resolved Hide resolved

`useRef` in React is typically used to reference a DOM element or a component. In the case of form inputs like Input or TextArea in Joy UI, you can use useRef to access and interact with the underlying DOM node directly without causing re-renders.
danilo-leal marked this conversation as resolved.
Show resolved Hide resolved

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

Input:
```js
<Input slotProps={{ input: { ref } }} />
```
Textarea:
```js
<Textarea slotProps={{ input: { ref } }} />
```
danilo-leal marked this conversation as resolved.
Show resolved Hide resolved

## Accessibility

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