import React, { Component, PropTypes } from 'react'
import ReactDOM from 'react-dom'
import EditorBase from './EditorBase'

export default class ValidateEditor extends Component {

    static propTypes = {
        content: PropTypes.string.isRequired,
        onDone: PropTypes.func.isRequired,
        onInput: PropTypes.func,
        isValid: PropTypes.func,
        className: PropTypes.string,
    }

    constructor(props) {
        super(props)
        this.state = { currentContent: props.content }
        this.onInput = this.onInput.bind(this)
        this.onDone = this.onDone.bind(this)
    }

    componentWillReceiveProps(nextProps) {
        this.setState({ currentContent: nextProps.content })
    }

    onInput(currentContent) {
        this.setState({ currentContent })
        this.props.onInput && this.props.onInput(currentContent)
    }

    onDone(content) {
        if (this.props.isValid && !this.props.isValid(content)) {
            this.refs.editor.reset()
            content = this.props.content
        }
        this.props.onDone(content)
    }

    render() {
        let className = this.props.className || ''
        if (this.props.isValid) {
            if (this.props.isValid(this.state.currentContent)) {
                className += ' has-success'
            } else {
                className += ' has-warning'
            }
        }
        return (
            <EditorBase
                {...this.props}
                ref="editor"
                className={className}
                onDone={this.onDone}
                onInput={this.onInput}
            />
        )
    }
}
ography/commit/.gitignore?id=9977786b56294292fb7da9eabc0f3b94d638e8a7'>commit</a><a href='/cgit/cloud-email/cryptography/diff/.gitignore?id=9977786b56294292fb7da9eabc0f3b94d638e8a7'>diff</a><a href='/cgit/cloud-email/cryptography/stats/.gitignore'>stats</a></td><td class='form'><form class='right' method='get' action='/cgit/cloud-email/cryptography/log/.gitignore'>
<input type='hidden' name='id' value='9977786b56294292fb7da9eabc0f3b94d638e8a7'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/cgit/cloud-email/cryptography/tree/?id=9977786b56294292fb7da9eabc0f3b94d638e8a7'>root</a>/<a href='/cgit/cloud-email/cryptography/tree/.gitignore?id=9977786b56294292fb7da9eabc0f3b94d638e8a7'>.gitignore</a></div><div class='content'>blob: 60f90063b1241b0f92de16355a0befd22bef3cfd (<a href='/cgit/cloud-email/cryptography/plain/.gitignore?id=9977786b56294292fb7da9eabc0f3b94d638e8a7'>plain</a>)
<table summary='blob content' class='blob'>
<tr><td class='linenumbers'><pre><a id='n1' href='#n1'>1</a>
<a id='n2' href='#n2'>2</a>
<a id='n3' href='#n3'>3</a>
<a id='n4' href='#n4'>4</a>
<a id='n5' href='#n5'>5</a>
<a id='n6' href='#n6'>6</a>
<a id='n7' href='#n7'>7</a>
<a id='n8' href='#n8'>8</a>
<a id='n9' href='#n9'>9</a>
<a id='n10' href='#n10'>10</a>
<a id='n11' href='#n11'>11</a>
<a id='n12' href='#n12'>12</a>
<a id='n13' href='#n13'>13</a>
<a id='n14' href='#n14'>14</a>
</pre></td>
<td class='lines'><pre><code>