import React from 'react';

var id = 0;

const propTypes = {
    type: React.PropTypes.oneOf(['checkbox', 'radio']),
    label: React.PropTypes.string,
    onCheck: React.PropTypes.func,
    onUncheck: React.PropTypes.func
};

export default class Tick extends React.Component {

    static get defaultProps(){
        return {
            type: 'checkbox',
            label: null,
            onCheck: () => {},
            onUncheck: () => {}
        };
    }

    constructor(props) {
        super(props);
        this.id = 'tick-' + (id++);
        this.onChange = this.onChange.bind(this);
    }

    onChange(event){
        var checked = event.target.checked;
        checked ? this.props.onCheck() : this.props.onUncheck();
    }

    render() {
        return (
            <div className="tick">
                <input
                    id={this.id}
                    type={this.props.type}
                    onChange={this.onChange} />
                <label htmlFor={this.id}>
                    {this.props.label}
                </label>
            </div>
        );
    }
}

Tick.propTypes = propTypes;