All files / boundless/packages/boundless-progress index.js

100% Statements 4/4
100% Branches 4/4
100% Functions 3/3
100% Lines 4/4
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                                                                                                                                                                  23x 7x                     23x                             23x                        
import React, {PropTypes} from 'react';
import cx from 'classnames';
 
import Button from 'boundless-button';
import omit from 'boundless-utils-omit-keys';
 
/**
__An unopinionated progress implementation, allowing for a variety of shapes and effects.__
 */
export default class Progress extends React.PureComponent {
    static propTypes = {
        /**
         * any [React-supported attribute](https://facebook.github.io/react/docs/tags-and-attributes.html#html-attributes)
         */
        '*': PropTypes.any,
 
        /**
         * any valid HTML tag name
         */
        cancelComponent: PropTypes.oneOfType([
            PropTypes.string,
            PropTypes.func,
        ]),
 
        cancelProps: PropTypes.shape({
            /**
             * any [React-supported attribute](https://facebook.github.io/react/docs/tags-and-attributes.html#html-attributes)
             */
            '*': PropTypes.any,
        }),
 
        /**
         * any valid HTML tag name
         */
        component: PropTypes.string,
 
        /**
         * if supplied, adds a cancel element and calls this function when that element is clicked
         */
        onCancel: PropTypes.func,
 
        /**
         * the integer (and unit, if applicable) of the current progress state, e.g. 0.01 (opacity)
         */
        progress: PropTypes.oneOfType([
          PropTypes.string,
          PropTypes.number,
        ]),
 
        /**
         * any valid HTML tag name
         */
        progressComponent: PropTypes.string,
 
        progressProps: PropTypes.shape({
            /**
             * any [React-supported attribute](https://facebook.github.io/react/docs/tags-and-attributes.html#html-attributes)
             */
            '*': PropTypes.any,
        }),
 
        /**
         * the CSS property to tween (must accept percentages) - defaults to "width"
         */
        tweenProperty: PropTypes.string,
    }
 
    static defaultProps = {
        cancelComponent: 'button',
        cancelProps: {},
        component: 'div',
        onCancel: null,
        progress: undefined,
        progressComponent: 'div',
        progressProps: {},
        tweenProperty: 'width',
    }
 
    static internalKeys = Object.keys(Progress.defaultProps)
 
    renderCancel() {
        if (this.props.onCancel) {
            return (
                <Button
                    {...this.props.cancelProps}
                    className={cx('b-progress-cancel', this.props.cancelProps.className)}
                    component={this.props.cancelComponent}
                    onPressed={this.props.onCancel} />
            );
        }
    }
 
    renderProgress() {
        return (
            <this.props.progressComponent
                {...this.props.progressProps}
                className={cx('b-progress', this.props.progressProps.className, {
                    'b-progress-indeterminate': this.props.progress === undefined,
                })}
                role='presentation'
                style={{
                    ...this.props.progressProps.style,
                    [this.props.tweenProperty]: this.props.progress,
                }} />
        );
    }
 
    render() {
        return (
            <this.props.component
                {...omit(this.props, Progress.internalKeys)}
                className={cx('b-progress-wrapper', this.props.className)}
                data-progress={this.props.progress !== undefined ? this.props.progress : null}>
                {this.renderProgress()}
                {this.props.children}
                {this.renderCancel()}
            </this.props.component>
        );
    }
}