Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Components receive a single callback prop valueChanged #2

Merged
merged 4 commits into from
Jun 30, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 16 additions & 7 deletions src/components/EditableDiv.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ const baseStyles = {
export default class EditableDiv extends Component {

constructor(props) {
super(props)
this.state = {inEditMode: false}
super(props);

this.state = {
inEditMode: false
};
}

componentDidUpdate() {
if (this.state.inEditMode) ReactDOM.findDOMNode(this.refs.input).focus()
if (this.state.inEditMode) {
ReactDOM.findDOMNode(this.refs.input).focus();
}
}

handleChange(text) {
this.props.valueChanged({text});
}

render() {
Expand All @@ -39,7 +48,7 @@ export default class EditableDiv extends Component {
this.props.style
])}
value={this.props.text}
onChange={(e) => this.props.updateProps({text: e.target.value})}
onChange={(e) => this.handleChange(e.target.value)}
onBlur={() => this.setState({inEditMode: false})}
/>
</div>
Expand Down Expand Up @@ -80,12 +89,12 @@ EditableDiv.propTypes = {

/**
* Function that updates the state tree.
* Passed in from renderer.
*/
updateProps: PropTypes.func.isRequired
valueChanged: PropTypes.func
};

EditableDiv.defaultProps = {
style: {},
editable: false
editable: false,
valueChanged: () => {}
};
21 changes: 8 additions & 13 deletions src/components/InputControl.react.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {Component, PropTypes} from 'react';

/*
* A controlled input that calls `notifyObservers` on changes.
* A controlled input that calls `valueChanged` on changes.
*/
export default class InputControl extends Component {
constructor() {
Expand All @@ -12,14 +12,8 @@ export default class InputControl extends Component {
}

handleChange(value) {
this.setState({value})
/**
* TODO (#22): Remove conditional. Always pass a callback function
* to components that can change value.
*/
if (this.props.notifyObservers) {
this.props.notifyObservers({value})
}
this.setState({value});
this.props.valueChanged({value});
}

render() {
Expand All @@ -37,9 +31,10 @@ InputControl.propTypes = {

/**
* Function that updates the state tree.
* Passed in from renderer IF the component has observers.
* TODO (#22): Always pass a callback function to components that can change
* value.
*/
notifyObservers: PropTypes.func
valueChanged: PropTypes.func
};

InputControl.defaultProps = {
valueChanged: () => {}
};