add preview support
This commit is contained in:
@@ -6,8 +6,13 @@ export default class Input extends Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
preview: null,
|
||||
};
|
||||
|
||||
this.autoResize = this.autoResize.bind(this);
|
||||
this.send = this.send.bind(this);
|
||||
this.getPreview = this.getPreview.bind(this);
|
||||
this.onKeyDown = this.onKeyDown.bind(this);
|
||||
}
|
||||
|
||||
@@ -46,7 +51,7 @@ export default class Input extends Component {
|
||||
}
|
||||
|
||||
this.fieldNode.value = '';
|
||||
this.setState({ height: null });
|
||||
this.setState({ height: null, preview: null });
|
||||
})
|
||||
.catch(() => {
|
||||
// TODO: do smth?
|
||||
@@ -54,7 +59,19 @@ export default class Input extends Component {
|
||||
.finally(() => this.setState({ isFieldDisabled: false }));
|
||||
}
|
||||
|
||||
render(props, { height, isFieldDisabled }) {
|
||||
getPreview() {
|
||||
const text = this.fieldNode.value;
|
||||
|
||||
if (!text || !text.trim()) return;
|
||||
|
||||
api.getPreview({ text })
|
||||
.then(preview => this.setState({ preview }))
|
||||
.catch(() => {
|
||||
// TODO: do smth?
|
||||
});
|
||||
}
|
||||
|
||||
render(props, { height, isFieldDisabled, preview }) {
|
||||
return (
|
||||
<form className={b('input', props)} onSubmit={this.send}>
|
||||
<textarea
|
||||
@@ -71,9 +88,28 @@ export default class Input extends Component {
|
||||
</textarea>
|
||||
|
||||
<div className="input__buttons">
|
||||
<button className={b('input__button', {}, { type: 'preview' })} type="button">Preview</button>
|
||||
<button className={b('input__button', {}, { type: 'send' })} type="submit">Send</button>
|
||||
<button
|
||||
className={b('input__button', {}, { type: 'preview' })}
|
||||
type="button"
|
||||
onClick={this.getPreview}
|
||||
>Preview</button>
|
||||
|
||||
<button
|
||||
className={b('input__button', {}, { type: 'send' })}
|
||||
type="submit"
|
||||
>Send</button>
|
||||
</div>
|
||||
|
||||
{
|
||||
// TODO: it can be more elegant;
|
||||
// for example it can render full comment component here (or above textarea on mobile)
|
||||
!!preview && (
|
||||
<div
|
||||
className={b('input__preview', { mix: 'raw-content' })}
|
||||
dangerouslySetInnerHTML={{ __html: preview }}
|
||||
/>
|
||||
)
|
||||
}
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user