All files / src GreetingDetail.js

94.44% Statements 17/18
83.33% Branches 5/6
100% Functions 10/10
94.44% Lines 17/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70                            14x 14x   14x   2x 5x       3x         1x         1x             5x 5x 5x             1x 1x           1x 1x 1x             8x      
import React from 'react';
import PropTypes from 'prop-types';
 
export default class GreetingDetail extends React.Component {
 
    static propTypes = {
        greeting: PropTypes.shape({
            name: PropTypes.string.isRequired,
            greeting: PropTypes.string.isRequired
        }),
        onAdd: PropTypes.func.isRequired
    };
 
    render() {
        const {name, greeting} = this.state;
        const saveDisabled = !(name && greeting);
 
        return (
            <div>
                <input ref={input => this.input = input}
                       onChange={event => this.updateModel(event)}
                       name="name"
                       value={name}
                       placeholder="Another Name"/>
                <input onChange={event => this.updateModel(event)}
                       name="greeting"
                       value={greeting}
                       placeholder="Greeting"/>
                <button
                    onClick={() => this.reset()}>
                    Clear
                </button>
                <button
                    disabled={saveDisabled}
                    onClick={() => this.save()}>
                    Save
                </button>
            </div>);
    }
 
    constructor(props) {
        super(props);
        const {name, greeting} = this.props.greeting || {name: '', greeting: ''};
        this.state = {
            name,
            greeting
        };
    }
 
    reset() {
        this.setState({name: '', greeting: ''});
        Iif (this.input) {
            this.input.focus();
        }
    }
 
    save() {
        const {onAdd} = this.props;
        const {name, greeting} = this.state;
        onAdd({
            name,
            greeting
        });
    }
 
    updateModel(event) {
        this.setState({[event.target.name]: event.target.value});
    }
}