All files index.js

96.97% Statements 32/33
95.24% Branches 20/21
100% Functions 6/6
96.97% Lines 32/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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          7x                 7x     7x   7x 2x                     10x   10x 3x     10x   10x 3x     10x 1x         3x             3x   3x 3x 10x 1x 1x   9x 6x   9x   3x 1x   3x           10x   10x   10x 1x         10x                     1x                                                                                                                                            
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Typed from 'typed.js';
 
class ReactTyped extends Component {
    rootElement = React.createRef()
 
    componentDidMount() {
        const {
            style,
            typedRef,
            stopped,
            className,
            ...typedOptions
        } = this.props;
 
 
        this.constructTyped(typedOptions);
 
        if (stopped) {
            this.typed.stop();
        }
    }
 
    constructTyped(options = {}) {
        const {
            style,
            typedRef,
            stopped,
            className,
            ...typedOptions
        } = this.props;
 
        if (this.typed) {
            this.typed.destroy();
        }
 
        this.typed = new Typed(this.rootElement.current, Object.assign(typedOptions, options));
 
        if (this.props.typedRef) {
            this.props.typedRef(this.typed);
        }
 
        this.typed.reConstruct = (opts) => {
            this.constructTyped(opts);
        };
    }
 
    shouldComponentUpdate(nextProps) {
        Eif (this.props !== nextProps) {
            const {
                style,
                typedRef,
                stopped,
                className,
                ...typedOptions
            } = nextProps;
 
            this.typed.options = Object.assign(this.typed.options, typedOptions);
            const reConstructed = !Object.keys(nextProps).every((key) => {
                if (!this.props[key] && nextProps[key]) {
                    this.constructTyped(nextProps);
                    return false;
                }
                if (this.typed[key]) {
                    this.typed[key] = nextProps[key];
                }
                return true;
            });
            if (!reConstructed && this.props.strings.length !== nextProps.strings.length) {
                this.constructTyped(nextProps);
            }
            return true;
        }
        return false;
    }
 
    render() {
        const { style, className, children } = this.props;
 
        let child = <span ref={this.rootElement} />;
 
        if (children) {
            child = React.cloneElement(children, {
                ref: this.rootElement,
            });
        }
 
        return (
            <span
                style={style}
                className={className}
            >
                {child}
            </span>
        );
    }
}
 
ReactTyped.propTypes = {
    /** styles for the outer element */
    style: PropTypes.object,
    /** class name for the outer element */
    className: PropTypes.string,
    /** the element to wrapp */
    children: PropTypes.object,
    /** typedRef(self: Typed) returns the Typed instance */
    typedRef: PropTypes.func,
    /** initialize in stopped state */
    stopped: PropTypes.bool,
    /** strings to be typed */
    strings: PropTypes.arrayOf(PropTypes.string),
    /**  type speed in milliseconds */
    typeSpeed: PropTypes.number,
    /** time before typing starts in milliseconds */
    startDelay: PropTypes.number,
    /** backspacing speed in milliseconds */
    backSpeed: PropTypes.number,
    /**  only backspace what doesn't match the previous string */
    smartBackspace: PropTypes.bool,
    /** shuffle the strings */
    shuffle: PropTypes.bool,
    /** time before backspacing in milliseconds */
    backDelay: PropTypes.number,
    /** Fade out instead of backspace */
    fadeOut: PropTypes.bool,
    /** css class for fade animation */
    fadeOutClass: PropTypes.string,
    /** Fade out delay in milliseconds */
    fadeOutDelay: PropTypes.number,
    /** loop the strings */
    loop: PropTypes.bool,
    /** amount of loops */
    loopCount: PropTypes.number,
    /** show cursor */
    showCursor: PropTypes.bool,
    /** character for cursor */
    cursorChar: PropTypes.string,
    /** insert CSS for cursor and fadeOut into HTML */
    autoInsertCss: PropTypes.bool,
    /** attribute for typing Ex: input placeholder, value, or just HTML text */
    attr: PropTypes.string,
    /**  bind to focus and blur if el is text input */
    bindInputFocusEvents: PropTypes.bool,
    /**  'html' or 'null' for plaintext */
    contentType: PropTypes.oneOf(['html', '']),
    /** onComplete(self: Typed) All typing is complete */
    onComplete: PropTypes.func,
    /** preStringTyped(arrayPos: number, self: Typed) Before each string is typed */
    preStringTyped: PropTypes.func,
    /** onStringTyped(arrayPos: number, self: Typed) After each string is typed */
    onStringTyped: PropTypes.func,
    /** onLastStringBackspaced(self: Typed) During looping, after last string is typed */
    onLastStringBackspaced: PropTypes.func,
    /** onTypingPaused(arrayPos: number, self: Typed) Typing has been stopped */
    onTypingPaused: PropTypes.func,
    /** onTypingResumed(arrayPos: number, self: Typed) Typing has been started after being stopped */
    onTypingResumed: PropTypes.func,
    /** onReset(self: Typed) After reset */
    onReset: PropTypes.func,
    /** onStop(arrayPos: number, self: Typed)    After stop */
    onStop: PropTypes.func,
    /** onStart(arrayPos: number, self: Typed) After start */
    onStart: PropTypes.func,
    /** onDestroy(self: Typed) After destroy */
    onDestroy: PropTypes.func,
};
 
export default ReactTyped;