# All files / checkbox checkbox.tsx

94.59% Statements 35/37
86.36% Branches 19/22
100% Functions 10/10
94.59% Lines 35/37

    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
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    1711x

    1x
    1x

    1x
    1x

    1x

    17x
    1x

    16x

    14x

    14x

    14x

    14x

    14x

    3x

    17x

    17x

    17x

    17x

    17x

    1x

    1x

    1x

    1x
    1x

    1x

    17x

    14x
    5x
    2x

    5x
    2x

    1x
    1x
    1x
     import * as React from "react";
    import * as ReactDOM from "react-dom";
    import Foundation, { HandledProps } from "@microsoft/fast-components-foundation-react";
    import {
        CheckboxHandledProps,
        CheckboxManagedClasses,
        CheckboxProps,
        CheckboxSlot,
        CheckboxUnhandledProps
    } from "./checkbox.props";
    import { CheckboxClassNameContract, ManagedClasses } from "@microsoft/fast-components-class-name-contracts-base";
    import { get } from "lodash-es";

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

    class Checkbox extends Foundation<
        CheckboxHandledProps,
        CheckboxUnhandledProps,
        CheckboxState
    > {
        public static displayName: string = "Checkbox";

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

            return null;
        }

        /**
         * Handled props instantiation
         */
        protected handledProps: HandledProps<CheckboxHandledProps> = {
            checked: void 0,
            disabled: void 0,
            inputId: void 0,
            indeterminate: void 0,
            managedClasses: void 0,
            onChange: 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 (
                <div
                    {...this.unhandledProps()}
                    className={this.generateClassNames()}
                >
                    <input
                        className={get(this.props, "managedClasses.checkbox_input")}
                        id={this.props.inputId}
                        type="checkbox"
                        ref={this.inputRef}
                        onChange={this.handleCheckboxChange}
                        disabled={this.props.disabled || null}
                        checked={this.state.checked}
                    />
                    <span className={get(this.props, "managedClasses.checkbox_stateIndicator")} />
                    {this.renderLabel()}
                </div>
            );
        }

        /**
         * 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);
        }

        /**
         * Render label if it exists
         */
        private renderLabel(): Array<React.ReactElement<any>> {
            return React.Children.map(
                this.withSlot(CheckboxSlot.label),
                (label: React.ReactElement<any>): React.ReactElement<any> => {
                    let className: string | undefined = get(this.props, "managedClasses.checkbox_label");

                    Iif (typeof className !== "string") {
                        return label;
                    }

                    const labelClassName: string | undefined = get(label, "props.className");

                    Eif (typeof labelClassName === "string") {
                        className = `${labelClassName} ${className}`;
                    }

                    return React.cloneElement(label, {className});
                }
            );
        }

        /**
         * 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.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 { CheckboxClassNameContract };