# All files / checkbox checkbox.tsx

96.55% Statements 28/29
86.96% Branches 20/23
100% Functions 9/9
96.55% Lines 28/29

    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
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    1461x

    1x
    1x
    1x
    1x

    17x
    1x

    16x

    14x

    14x

    14x

    14x

    14x

    3x

    17x

    17x

    17x

    17x

    17x

    17x

    14x
    5x
    2x

    5x
    2x

    1x
    1x
    1x
     import * as React from "react";
    import * as ReactDOM from "react-dom";
    import Foundation, { HandledProps } from "../foundation";
    import { CheckboxHTMLTags, CheckboxProps, ICheckboxHandledProps, ICheckboxManagedClasses, ICheckboxUnhandledProps } from "./checkbox.props";
    import { ICheckboxClassNameContract, IManagedClasses } from "@microsoft/fast-components-class-name-contracts-base";
    import { get } from "lodash-es";

    /**
     * Checkbox state interface
     */
    export interface ICheckboxState {
        checked: boolean;
    }

    /* tslint:disable-next-line */
    class Checkbox extends Foundation<ICheckboxHandledProps & ICheckboxManagedClasses, ICheckboxUnhandledProps, ICheckboxState> {

        /**
         * React life-cycle method
         */
        public static getDerivedStateFromProps(nextProps: ICheckboxHandledProps, prevState: ICheckboxState): null | Partial<ICheckboxState> {
            if (typeof nextProps.checked === "boolean" && nextProps.checked !== prevState.checked) {
                return {
                    checked: nextProps.checked
                };
            }

            return null;
        }

        /**
         * Handled props instantiation
         */
        protected handledProps: HandledProps<ICheckboxHandledProps & IManagedClasses<ICheckboxClassNameContract>> = {
            checked: void 0,
            disabled: void 0,
            indeterminate: void 0,
            managedClasses: void 0,
            onChange: void 0,
            tag: void 0,
            text: void 0
        };

        /**
         * Provides reference to input
         */
        private inputRef: React.RefObject<HTMLInputElement>;

        /**
         * Define constructor
         */
        constructor(props: CheckboxProps) {
            super(props);

            this.state = {
                checked: this.props.checked || false
            };

            this.inputRef = React.createRef();
        }

        /**
         * React life-cycle method
         */
        public componentDidMount(): void {
            this.applyIndeterminateState();
        }

        /**
         * React life-cycle method
         */
        public componentDidUpdate(): void {
            this.applyIndeterminateState();
        }

        /**
         * Renders the component
         */
        public render(): React.ReactElement<HTMLElement> {
            return (
                <this.tag
                    {...this.unhandledProps()}
                    className={this.generateClassNames()}
                >
                    <input
                        className={get(this.props, "managedClasses.checkbox_input")}
                        type="checkbox"
                        ref={this.inputRef}
                        onChange={this.handleCheckboxChange}
                        disabled={this.props.disabled || null}
                        checked={this.state.checked}
                    />
                    <span className={get(this.props, "managedClasses.checkbox_span")} />
                    <span className={get(this.props, "managedClasses.checkbox_label")}>
                        {this.props.text ? this.props.text : null}
                    </span>
                </this.tag>
            );
        }

        /**
         * Generates class names
         */
        protected generateClassNames(): string {
            let classes: string = get(this.props, "managedClasses.checkbox");

            classes = this.props.disabled ? `${classes} ${get(this.props, "managedClasses.checkbox_disabled")}` : classes;

            return super.generateClassNames(classes);
        }

        /**
         * Stores HTML tag for use in render
         */
        private get tag(): string {
            return CheckboxHTMLTags[this.props.tag] || CheckboxHTMLTags.label;
        }

        /**
         * Apply indeterminate state to items that are indeterminate.
         * This method should be called after render because it relies on element references.
         */
        private applyIndeterminateState(): void {
             Iif (this.props.indeterminate && this.inputRef.current && this.state.checked) {
                this.inputRef.current.indeterminate = this.props.indeterminate;
            }
        }

        /**
         * Handles onChange as a controlled component
         */
        private handleCheckboxChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
            if (typeof this.props.checked !== "boolean") {
                this.setState({checked: !this.state.checked});
            }

            if (this.props.onChange) {
                this.props.onChange(e);
            }
        }
    }

    export default Checkbox;
    export * from "./checkbox.props";
    export {ICheckboxClassNameContract};