-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimental SimpleRichTextEditor
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* @flow */ | ||
import React, {Component} from 'react'; | ||
import RichTextEditor, {createEmptyValue} from './RichTextEditor'; | ||
import autobind from 'class-autobind'; | ||
|
||
import type {EditorValue} from './RichTextEditor'; | ||
|
||
type Props = { | ||
value: string; | ||
format: string; | ||
onChange: (value: string) => any; | ||
}; | ||
type State = { | ||
editorValue: EditorValue; | ||
}; | ||
|
||
export default class SimpleRichTextEditor extends Component { | ||
props: Props; | ||
state: State; | ||
|
||
constructor() { | ||
super(...arguments); | ||
autobind(this); | ||
this.state = { | ||
editorValue: createEmptyValue(), | ||
}; | ||
} | ||
|
||
componentWillMount() { | ||
this._updateStateFromProps(this.props); | ||
} | ||
|
||
componentWillReceiveProps(newProps: Props) { | ||
this._updateStateFromProps(newProps); | ||
} | ||
|
||
_updateStateFromProps(props: Props) { | ||
let {value, format} = props; | ||
let {editorValue} = this.state; | ||
this.setState({ | ||
editorValue: editorValue.setContentFromString(value, format), | ||
}); | ||
} | ||
|
||
render(): React.Element { | ||
let {value, format, onChange, ...otherProps} = this.props; // eslint-disable-line no-unused-vars | ||
return ( | ||
<RichTextEditor | ||
{...otherProps} | ||
value={this.state.editorValue} | ||
onChange={this._onChange} | ||
/> | ||
); | ||
} | ||
|
||
_onChange(editorValue: EditorValue) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
let {format, onChange} = this.props; | ||
if (onChange != null) { | ||
let stringValue = editorValue.toString(format); | ||
onChange(stringValue); | ||
This comment has been minimized.
Sorry, something went wrong.
ForbesLindesay
Collaborator
|
||
} | ||
} | ||
} |
1 comment
on commit 06ed718
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is related to a feature request at #62
Doesn't this method also need to call
this.setState({editorValue});
before passing on the change? otherwise changes to things like cursor position won't be tracked.