# All files toggle.tsx

100% Statements 21/21
100% Branches 18/18
100% Functions 7/7
100% Lines 21/21

    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
    1421x

    1x
    1x

    1x

    11x
    1x

    10x

    8x

    8x

    8x

    11x

    11x

    22x

    8x
    5x
    2x

    5x
    2x

    11x
    2x

    1x

     import * as React from "react";
    import * as ReactDOM from "react-dom";
    import { get } from "lodash-es";
    import Foundation, { HandledProps } from "@microsoft/fast-components-foundation-react";
    import { ToggleHandledProps, ToggleManagedClasses, ToggleProps, ToggleUnhandledProps } from "./toggle.props";
    import { ManagedClasses, ToggleClassNameContract } from "@microsoft/fast-components-class-name-contracts-base";

    /**
     * Toggle state interface
     */
    export interface ToggleState {
        selected: boolean;
    }

    /**
     * Toggle base component
     */
    class Toggle extends Foundation<
        ToggleHandledProps,
        ToggleUnhandledProps,
        ToggleState
    > {
        public static displayName: string = "Toggle";

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

            return null;
        }

        protected handledProps: HandledProps<ToggleHandledProps> = {
            managedClasses: void 0,
            disabled: void 0,
            id: void 0,
            labelId: void 0,
            selected: void 0,
            selectedMessage: void 0,
            statusMessageId: void 0,
            unselectedMessage: void 0,
            onChange: void 0
        };

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

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

        /**
         * Renders the component
         */
        public render(): React.ReactElement<HTMLDivElement> {
            return (
                <div
                    {...this.unhandledProps()}
                    className={this.generateClassNames()}
                    aria-disabled={this.props.disabled || null}
                >
                    {this.generateLabel()}
                    <div className={get(this.props, "managedClasses.toggle_toggleButton")}>
                        <input
                            className={get(this.props, "managedClasses.toggle_input")}
                            type="checkbox"
                            id={this.props.id}
                            aria-describedby={this.props.statusMessageId}
                            disabled={this.props.disabled}
                            value={this.generateToggleStateLabel()}
                            onChange={this.handleToggleChange}
                            checked={this.state.selected}
                        />
                        <span className={get(this.props, "managedClasses.toggle_stateIndicator")} />
                    </div>
                    <span
                        id={this.props.statusMessageId}
                    >
                        {this.generateToggleStateLabel()}
                    </span>
                </div>
            );
        }

        /**
         * Generates class names
         */
        protected generateClassNames(): string {
            return super.generateClassNames(get(this.props, "managedClasses.toggle"));
        }

        /**
         * Creates proper string based on state
         */
        private generateToggleStateLabel(): string {
            return this.state.selected ? this.props.selectedMessage : this.props.unselectedMessage;
        }

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

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

        /**
         * Generates label if it exists
         */
        private generateLabel(): React.ReactElement<HTMLElement> {
            if (this.props.labelId || this.props.children) {
                return(
                    <label
                        className={get(this.props, "managedClasses.toggle_label")}
                        id={this.props.labelId}
                        htmlFor={this.props.id}
                    >
                        {this.props.children}
                    </label>
                );
            }
        }
    }

    export default Toggle;
    export * from "./toggle.props";
    export { ToggleClassNameContract };