# All files / progress progress.tsx

100% Statements 22/22
100% Branches 6/6
100% Functions 9/9
100% Lines 22/22

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

    1x
    1x

    1x

    1x
    1x
    1x

    1x

    7x

    7x

    7x

    7x

    7x

    12x

    19x

    19x
    60x
    38x

    19x

    1x
    1x
    1x
     import * as React from "react";
    import * as ReactDOM from "react-dom";
    import { get } from "lodash-es";
    import Foundation, { HandledProps } from "../foundation";
    import { IProgressHandledProps, IProgressManagedClasses, IProgressUnhandledProps } from "./progress.props";
    import { IManagedClasses, IProgressClassNameContract } from "@microsoft/fast-components-class-name-contracts-base";

    export enum ProgressType {
        determinate = "determinate",
        indeterminate = "indeterminate"
    }

    /* tslint:disable-next-line */
    class Progress extends Foundation<IProgressHandledProps & IManagedClasses<IProgressClassNameContract>, React.HTMLAttributes<HTMLDivElement>, {}> {

        public static defaultProps: Partial<IProgressHandledProps> = {
            minValue: 0,
            maxValue: 100
        };

        protected handledProps: HandledProps<IProgressHandledProps & IManagedClasses<IProgressClassNameContract>> = {
            children: void 0,
            managedClasses: void 0,
            maxValue: void 0,
            minValue: void 0,
            value: void 0
        };

        /**
         * Renders the component
         */
        public render(): React.ReactElement<HTMLDivElement> {
            return (
                <div
                    {...this.unhandledProps()}
                    className={this.generateClassNames()}
                    role="progressbar"
                    aria-valuenow={this.props.value}
                    aria-valuemin={this.props.minValue}
                    aria-valuemax={this.props.maxValue}
                >
                    {this.renderChildren()}
                </div>
            );
        }

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

        /**
         * Renders children based on value prop
         */
        private renderChildren(): JSX.Element[] {
            return this.props.value !== undefined
                ? this.renderChildElements(ProgressType.determinate)
                : this.renderChildElements(ProgressType.indeterminate);
        }

        /**
         * Renders children by slot name
         */
        private renderChildElements(slot: ProgressType): JSX.Element[] {
            return this.getChildBySlot(
                this.props.children,
                slot
            ).map((childItem: JSX.Element, index: number): JSX.Element => {
                return React.cloneElement(
                    this.getChildBySlot(
                        this.props.children,
                        slot
                    )[0],
                    {
                        key: index
                    }
                );
            });
        }

        /**
         * Gets children by slot name
         */
        private getChildBySlot(children: React.ReactNode, slot: ProgressType): JSX.Element[] {
            const childBySlot: JSX.Element[] = [];

            React.Children.forEach(children, (child: JSX.Element): void => {
                if (child.props.slot === slot) {
                    childBySlot.push(child);
                }
            });

            return childBySlot;
        }
    }

    export default Progress;
    export * from "./progress.props";
    export { IProgressClassNameContract };