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

feat(TextArea): add minHeight property, docs example #1679

Merged
merged 5 commits into from
Jun 7, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react'
import { Form, TextArea } from 'semantic-ui-react'

const TextAreaExample = () => (
const TextAreaExampleTextArea = () => (
<Form>
<TextArea placeholder='Tell us more' />
</Form>
)

export default TextAreaExample
export default TextAreaExampleTextArea
16 changes: 16 additions & 0 deletions docs/app/Examples/addons/TextArea/Types/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react'

import ComponentExample from 'docs/app/Components/ComponentDoc/ComponentExample'
import ExampleSection from 'docs/app/Components/ComponentDoc/ExampleSection'

const TextAreaTypesExamples = () => (
<ExampleSection title='Types'>
<ComponentExample
title='TextArea'
description='A default TextArea.'
examplePath='addons/TextArea/Types/TextAreaExampleTextArea'
/>
</ExampleSection>
)

export default TextAreaTypesExamples
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Form, TextArea } from 'semantic-ui-react'

const TextAreaExampleAutoHeight = () => (
<Form>
<TextArea placeholder='Try adding multiple lines' autoHeight />
<TextArea autoHeight placeholder='Try adding multiple lines' />
</Form>
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'
import { Form, TextArea } from 'semantic-ui-react'

const TextAreaExampleAutoHeightMinHeight = () => (
<Form>
<TextArea autoHeight minHeight={100} placeholder='Try adding multiple lines' />
</Form>
)

export default TextAreaExampleAutoHeightMinHeight
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'
import { Form, TextArea } from 'semantic-ui-react'

const TextAreaExampleAutoHeightRows = () => (
<Form>
<TextArea autoHeight placeholder='Try adding multiple lines' rows={1} />
</Form>
)

export default TextAreaExampleAutoHeightRows
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'
import { Form, TextArea } from 'semantic-ui-react'

const TextAreaExampleMinHeight = () => (
<Form>
<TextArea minHeight={100} placeholder='Tell us more' />
</Form>
)

export default TextAreaExampleMinHeight
10 changes: 10 additions & 0 deletions docs/app/Examples/addons/TextArea/Usage/TextAreaExampleRows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'
import { Form, TextArea } from 'semantic-ui-react'

const TextAreaExampleRows = () => (
<Form>
<TextArea rows={2} placeholder='Tell us more' />
</Form>
)

export default TextAreaExampleRows
26 changes: 21 additions & 5 deletions docs/app/Examples/addons/TextArea/Usage/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import React from 'react'

import ComponentExample from 'docs/app/Components/ComponentDoc/ComponentExample'
import ExampleSection from 'docs/app/Components/ComponentDoc/ExampleSection'

const TextAreaTypesExamples = () => (
const TextAreaUsageExamples = () => (
<ExampleSection title='Usage'>
<ComponentExample
title='TextArea'
description='A default TextArea.'
examplePath='addons/TextArea/Usage/TextAreaExample'
title='Min Height'
description='A TextArea can have a minimum height.'
examplePath='addons/TextArea/Usage/TextAreaExampleMinHeight'
/>

<ComponentExample
title='Rows'
description='A TextArea can have a minimum number of rows.'
examplePath='addons/TextArea/Usage/TextAreaExampleRows'
/>

<ComponentExample
title='Auto Height'
description='A TextArea can adjust its height to fit its contents.'
examplePath='addons/TextArea/Usage/TextAreaExampleAutoHeight'
/>
<ComponentExample
description='A TextArea can adjust its height to fit its contents and depend on minHeight value.'
examplePath='addons/TextArea/Usage/TextAreaExampleAutoHeightMinHeight'
/>
<ComponentExample
description='A TextArea can adjust its height to fit its contents and depend on rows value.'
examplePath='addons/TextArea/Usage/TextAreaExampleAutoHeightRows'
/>
</ExampleSection>
)

export default TextAreaTypesExamples
export default TextAreaUsageExamples
3 changes: 3 additions & 0 deletions docs/app/Examples/addons/TextArea/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React from 'react'

import Types from './Types'
import Usage from './Usage'

const TextAreaExamples = () => (
<div>
<Types />
<Usage />
</div>
)
Expand Down
10 changes: 10 additions & 0 deletions src/addons/TextArea/TextArea.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@ export interface TextAreaProps {
/** Indicates whether height of the textarea fits the content or not. */
autoHeight?: boolean;

/** Indicates a minimum height for textarea when using autoHeight. */
minHeight?: number | string;

/**
* Called on change.
*
* @param {SyntheticEvent} event - The React SyntheticEvent object
* @param {object} data - All props and the event value.
*/
onChange?: (event: React.FormEvent<HTMLTextAreaElement>, data: TextAreaOnChangeData) => void;

/** Indicates row count for a TextArea. */
rows?: number;

/** Custom TextArea style. */
style?: Object;

/** The value of the textarea. */
value?: string;
}
Expand Down
50 changes: 37 additions & 13 deletions src/addons/TextArea/TextArea.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import _ from 'lodash'
import PropTypes from 'prop-types'
import React, { Component } from 'react'

import {
customPropTypes,
getElementType,
Expand All @@ -24,19 +26,33 @@ class TextArea extends Component {
/** Indicates whether height of the textarea fits the content or not. */
autoHeight: PropTypes.bool,

/** Indicates a minimum height for textarea. */
minHeight: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string,
]),

/**
* Called on change.
* @param {SyntheticEvent} event - The React SyntheticEvent object
* @param {object} data - All props and the event value.
*/
onChange: PropTypes.func,

/** Indicates row count for a TextArea. */
rows: PropTypes.number,

/** Custom TextArea style. */
style: PropTypes.object,

/** The value of the textarea. */
value: PropTypes.string,
}

static defaultProps = {
as: 'textarea',
minHeight: 0,
rows: 3,
}

componentDidMount() {
Expand All @@ -56,45 +72,53 @@ class TextArea extends Component {

focus = () => (this.ref.focus())

handleChange = (e) => {
const { onChange } = this.props
if (onChange) onChange(e, { ...this.props, value: e.target && e.target.value })
handleChange = e => {
const value = _.get(e, 'target.value')

this.updateHeight(e.target)
_.invoke(this.props, 'onChange', e, { ...this.props, value })
this.updateHeight()
}

handleRef = c => (this.ref = c)

removeAutoHeightStyles = () => {
this.ref.removeAttribute('rows')
this.ref.style.height = null
this.ref.style.minHeight = null
this.ref.style.resize = null
}

updateHeight = () => {
if (!this.ref) return

const { autoHeight } = this.props
if (!autoHeight) return
if (!this.ref || !autoHeight) return

let { borderTopWidth, borderBottomWidth } = window.getComputedStyle(this.ref)
borderTopWidth = parseInt(borderTopWidth, 10)
borderBottomWidth = parseInt(borderBottomWidth, 10)

this.ref.rows = '1'
this.ref.style.minHeight = '0'
this.ref.style.resize = 'none'
this.ref.style.height = 'auto'
this.ref.style.height = (this.ref.scrollHeight + borderTopWidth + borderBottomWidth) + 'px'
}

render() {
const { value } = this.props
const {
minHeight,
rows,
style,
value,
} = this.props
const rest = getUnhandledProps(TextArea, this.props)
const ElementType = getElementType(TextArea, this.props)

return <ElementType{...rest} onChange={this.handleChange} ref={this.handleRef} value={value} />
return (
<ElementType
{...rest}
onChange={this.handleChange}
ref={this.handleRef}
rows={rows}
style={{ ...style, minHeight }}
value={value}
/>
)
}
}

Expand Down
Loading