-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(ffe-form-react): Document how to control focus with refs
- Loading branch information
Kristofer Selbekk
committed
Oct 2, 2018
1 parent
cdc58c6
commit 998af6c
Showing
2 changed files
with
78 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,47 @@ | ||
```js | ||
<React.Fragment> | ||
<Label htmlFor="textarea-example"> | ||
Beskriv skaden | ||
</Label> | ||
<Label htmlFor="textarea-example">Beskriv skaden</Label> | ||
<TextArea | ||
defaultValue="Dette er et tekstfelt for lengre tekster." | ||
id="textarea-example" | ||
rows="8" | ||
/> | ||
</React.Fragment> | ||
``` | ||
|
||
## Styr fokus med `ref` | ||
|
||
Du kan få en referanse til textarea-feltet ved å sende inn en `ref`-prop. Brukes typisk til å fokusere et felt programmatisk. | ||
|
||
```js | ||
const { TextArea } = require('.'); | ||
const { ButtonGroup, PrimaryButton } = require('../../ffe-buttons-react'); | ||
|
||
class Example extends React.Component { | ||
constructor() { | ||
super(); | ||
this.inputRef = React.createRef(); | ||
this.handleClick = this.handleClick.bind(this); | ||
} | ||
handleClick() { | ||
this.inputRef.current.focus(); | ||
} | ||
render() { | ||
return ( | ||
<React.Fragment> | ||
<TextArea | ||
ref={this.inputRef} | ||
placeholder="Trykk på knappen for fokus" | ||
/> | ||
<ButtonGroup> | ||
<PrimaryButton onClick={this.handleClick}> | ||
Klikk for å fokusere input | ||
</PrimaryButton> | ||
</ButtonGroup> | ||
</React.Fragment> | ||
); | ||
} | ||
} | ||
|
||
<Example />; | ||
``` |