Skip to content

Commit

Permalink
fix(TextArea): use onInput for updating height for IE compatibility (#…
Browse files Browse the repository at this point in the history
…1982)

* fix(TextArea): use onInput for updating height for IE compatibility

* fix(TextArea): cleanup onChange and onInput typings

* fix(typings): remove useless export
  • Loading branch information
nickpedersen authored and levithomason committed Sep 1, 2017
1 parent 1596486 commit 85a2b99
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export { default as Confirm, ConfirmProps } from './dist/commonjs/addons/Confirm
export { default as Portal, PortalProps } from './dist/commonjs/addons/Portal';
export { default as Radio, RadioProps } from './dist/commonjs/addons/Radio';
export { default as Select, SelectProps } from './dist/commonjs/addons/Select';
export { default as TextArea, TextAreaProps, TextAreaOnChangeData } from './dist/commonjs/addons/TextArea';
export { default as TextArea, TextAreaProps } from './dist/commonjs/addons/TextArea';

// Behaviors
export {
Expand Down
14 changes: 9 additions & 5 deletions src/addons/TextArea/TextArea.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ export interface TextAreaProps {
* @param {SyntheticEvent} event - The React SyntheticEvent object
* @param {object} data - All props and the event value.
*/
onChange?: (event: React.FormEvent<HTMLTextAreaElement>, data: TextAreaOnChangeData) => void;
onChange?: (event: React.FormEvent<HTMLTextAreaElement>, data: TextAreaProps) => void;

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

/** Indicates row count for a TextArea. */
rows?: number | string;
Expand All @@ -27,10 +35,6 @@ export interface TextAreaProps {
value?: number | string;
}

export interface TextAreaOnChangeData extends TextAreaProps {
value?: string;
}

declare class TextArea extends React.Component<TextAreaProps, {}> {
focus: () => void;
}
Expand Down
14 changes: 14 additions & 0 deletions src/addons/TextArea/TextArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class TextArea extends Component {
*/
onChange: PropTypes.func,

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

/** Indicates row count for a TextArea. */
rows: PropTypes.oneOfType([
PropTypes.number,
Expand Down Expand Up @@ -75,6 +82,12 @@ class TextArea extends Component {
const value = _.get(e, 'target.value')

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

handleInput = (e) => {
const value = _.get(e, 'target.value')

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

Expand Down Expand Up @@ -116,6 +129,7 @@ class TextArea extends Component {
<ElementType
{...rest}
onChange={this.handleChange}
onInput={this.handleInput}
ref={this.handleRef}
rows={rows}
style={{ resize, ...style }}
Expand Down
2 changes: 1 addition & 1 deletion src/addons/TextArea/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default, TextAreaProps, TextAreaOnChangeData } from './TextArea';
export { default, TextAreaProps } from './TextArea';
14 changes: 14 additions & 0 deletions test/specs/addons/TextArea/TextArea-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,20 @@ describe('TextArea', () => {
})
})

describe('onInput', () => {
it('is called with (e, data) on input', () => {
const spy = sandbox.spy()
const e = { target: { value: 'name' } }
const props = { 'data-foo': 'bar', onInput: spy }

wrapperShallow(<TextArea {...props} />)
wrapper.find('textarea').simulate('input', e)

spy.should.have.been.calledOnce()
spy.should.have.been.calledWithMatch(e, { ...props, value: e.target.value })
})
})

describe('rows', () => {
it('has default value', () => {
shallow(<TextArea />)
Expand Down

0 comments on commit 85a2b99

Please sign in to comment.