-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# TextareaAutosize | ||
|
||
A textarea component that auto-resizes. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import { TextareaAutosize } from '@discussify/styleguide'; | ||
|
||
<TextareaAutosize /> | ||
``` | ||
|
||
## Props | ||
|
||
| name | type | default | description | | ||
| ---- | ---- | ------- | ----------- | | ||
| maxRows | number | | The number of max rows of the textarea | | ||
|
||
Any other properties supplied will be spread to the root element. One useful textarea property is `rows`, which defines the minimum number of rows within the textarea. |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.textareaAutosize { | ||
overflow: hidden; | ||
resize: none; | ||
transition: height 0.2s ease, max-height 0.2s ease; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import React, { Component } from 'react'; | ||
import { findDOMNode } from 'react-dom'; | ||
import PropTypes from 'prop-types'; | ||
import classNames from 'classnames'; | ||
import growElementFn from 'grow-element-fn'; | ||
import { debounce } from 'lodash'; | ||
import styles from './TextareaAutosize.css'; | ||
|
||
export default class TextareaAutosize extends Component { | ||
static propTypes = { | ||
rows: PropTypes.number, | ||
maxRows: PropTypes.number, | ||
onFocus: PropTypes.func, | ||
onBlur: PropTypes.func, | ||
className: PropTypes.string, | ||
}; | ||
|
||
static defaultProps = { | ||
rows: 1, | ||
}; | ||
|
||
componentDidMount() { | ||
this.updateSize(); | ||
window.addEventListener('resize', this.handleResize); | ||
} | ||
|
||
componentDidUpdate() { | ||
this.updateSize(); | ||
} | ||
|
||
componentWillUnmount() { | ||
window.removeEventListener('resize', this.handleResize); | ||
} | ||
|
||
render() { | ||
const { className, rest } = this.props; | ||
|
||
return ( | ||
<textarea | ||
ref={ this.storeNode } | ||
onFocus={ this.handleFocus } | ||
onBlur={ this.handleBlur } | ||
{ ...rest } | ||
className={ classNames(styles.textareaAutosize, className) } /> | ||
); | ||
} | ||
|
||
storeNode = (ref) => { | ||
this.node = findDOMNode(ref); | ||
|
||
if (this.node) { | ||
this.node.addEventListener('focus', this.updateSize); | ||
this.node.addEventListener('blur', this.updateSize); | ||
this.node.addEventListener('input', this.updateSize); | ||
} | ||
}; | ||
|
||
updateSize = () => { | ||
growElementFn({ | ||
el: this.node, | ||
minLines: this.props.rows, | ||
maxLines: this.props.maxRows, | ||
extraLine: this.focused, | ||
}); | ||
}; | ||
|
||
handleFocus = () => { | ||
this.focused = true; | ||
this.updateSize(); | ||
this.props.onFocus && this.props.onFocus(); | ||
}; | ||
|
||
handleBlur = () => { | ||
this.focused = false; | ||
this.updateSize(); | ||
this.props.onBlur && this.props.onBlur(); | ||
}; | ||
|
||
handleResize = debounce(() => this.updateSize(), 500); | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './TextareaAutosize.js'; |
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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
import { withKnobs, boolean, number } from '@storybook/addon-knobs'; | ||
import { action } from '@storybook/addon-actions'; | ||
import { withReadme } from 'storybook-readme'; | ||
import { TextareaAutosize } from '../src'; | ||
import readme from '../src/components/textarea-autosize/README.md'; | ||
|
||
storiesOf('TextareaAutosize', module) | ||
.addDecorator(withReadme(readme)) | ||
.addDecorator(withKnobs) | ||
.add('Default', () => ( | ||
<TextareaAutosize /> | ||
)) | ||
.add('5 max rows ', () => ( | ||
<TextareaAutosize maxRows={ 5 } /> | ||
)) | ||
.add('2 rows, 5 max rows ', () => ( | ||
<TextareaAutosize rows={ 2 } maxRows={ 5 } /> | ||
)) | ||
.add('Knobs playground ⚽', () => { | ||
const rows = number('rows', 1); | ||
const maxRows = number('maxRows', 5); | ||
const disabled = boolean('disabled'); | ||
|
||
return ( | ||
<TextareaAutosize | ||
rows={ rows } | ||
maxRows={ maxRows } | ||
disabled={ disabled } | ||
onHeightChange={ action('height changed') } /> | ||
); | ||
}); |