All files / app/checkout CheckoutStep.tsx

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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 2053x 3x 3x 3x   3x   3x                           185x 185x 185x       3x 185x   185x 8x       3x 617x   617x 51x       3x 4x 1x   1x       3x   802x 802x 802x 802x 802x 802x 802x 802x   802x                                                   802x 802x   802x     802x 1x         801x                                       59x 59x   59x 5x 5x 5x   5x 1x     5x 5x     5x   5x       3x 5x   5x       5x   5x     3x 5x 5x   5x       5x 5x   5x           3x 5x   5x 10x       10x     5x     3x 59x         59x   59x     185x 58x           3x  
import classNames from 'classnames';
import { noop } from 'lodash';
import React, { createRef, Component, ReactNode } from 'react';
import { CSSTransition } from 'react-transition-group';
 
import { isMobileView, MobileView } from '../ui/responsive';
 
import CheckoutStepHeader from './CheckoutStepHeader';
import CheckoutStepType from './CheckoutStepType';
 
export interface CheckoutStepProps {
    heading?: ReactNode;
    isActive?: boolean;
    isComplete?: boolean;
    isEditable?: boolean;
    summary?: ReactNode;
    type: CheckoutStepType;
    onExpanded?(step: CheckoutStepType): void;
    onEdit?(step: CheckoutStepType): void;
}
 
export default class CheckoutStep extends Component<CheckoutStepProps> {
    private containerRef = createRef<HTMLLIElement>();
    private contentRef = createRef<HTMLDivElement>();
    private timeoutRef?: number;
    private timeoutDelay?: number;
 
    componentDidMount(): void {
        const { isActive } = this.props;
 
        if (isActive) {
            this.focusStep();
        }
    }
 
    componentDidUpdate(prevProps: Readonly<CheckoutStepProps>): void {
        const { isActive } = this.props;
 
        if (isActive && isActive !== prevProps.isActive) {
            this.focusStep();
        }
    }
 
    componentWillUnmount(): void {
        if (this.timeoutRef) {
            window.clearTimeout(this.timeoutRef);
 
            this.timeoutRef = undefined;
        }
    }
 
    render(): ReactNode {
        const {
            heading,
            isActive,
            isComplete,
            isEditable,
            onEdit,
            summary,
            type,
        } = this.props;
 
        return (
            <li
                className={ classNames(
                    'checkout-step',
                    'optimizedCheckout-checkoutStep',
                    { [`checkout-step--${type}`]: !!type }
                ) }
                ref={ this.containerRef }
            >
                <div className="checkout-view-header">
                    <CheckoutStepHeader
                        heading={ heading }
                        isActive={ isActive }
                        isComplete={ isComplete }
                        isEditable={ isEditable }
                        onEdit={ onEdit }
                        summary={ summary }
                        type={ type }
                    />
                </div>
 
                { this.renderContent() }
            </li>
        );
    }
 
    private renderContent(): ReactNode {
        const { children, isActive } = this.props;
 
        return <>
            <MobileView>
                { matched => {
                    if (matched) {
                        return !isActive ? null : <div className="checkout-view-content">
                            { children }
                        </div>;
                    }
 
                    return <CSSTransition
                        addEndListener={ this.handleTransitionEnd }
                        classNames="checkout-view-content"
                        in={ isActive }
                        mountOnEnter
                        timeout={ {} }
                        unmountOnExit
                    >
                        <div
                            className="checkout-view-content"
                            ref={ this.contentRef }
                        >
                            { children }
                        </div>
                    </CSSTransition>;
                } }
            </MobileView>
        </>;
    }
 
    private focusStep(): void {
        const delay = isMobileView() ? 0 : this.getTransitionDelay();
 
        this.timeoutRef = window.setTimeout(() => {
            const input = this.getChildInput();
            const position = this.getScrollPosition();
            const { type, onExpanded = noop } = this.props;
 
            if (input) {
                input.focus();
            }
 
            Eif (position !== undefined && !isNaN(position)) {
                window.scrollTo(0, position);
            }
 
            onExpanded(type);
 
            this.timeoutRef = undefined;
        }, delay);
    }
 
    private getChildInput(): HTMLElement | undefined {
        const container = this.containerRef.current;
 
        Iif (!container) {
            return;
        }
 
        const input = container.querySelector<HTMLElement>('input, select, textarea');
 
        return input ? input : undefined;
    }
 
    private getScrollPosition(): number | undefined {
        const container = this.getParentContainer();
        const { isComplete } = this.props;
 
        Iif (!container || window !== window.top) {
            return;
        }
 
        const topOffset = isComplete ? 0 : window.innerHeight / 5;
        const containerOffset = container.getBoundingClientRect().top + (window.scrollY || window.pageYOffset);
 
        return containerOffset - topOffset;
    }
 
    // For now, we need to find the parent container because `CheckoutStep`
    // isn't the outer container yet. Once both the header and body are
    // moved inside this component, we can remove the lookup.
    private getParentContainer(): HTMLElement | undefined {
        let container: HTMLElement | null = this.containerRef.current;
 
        while (container && container.parentElement) {
            Iif (container.parentElement.classList.contains('checkout-step')) {
                return container.parentElement;
            }
 
            container = container.parentElement;
        }
 
        return this.containerRef.current ? this.containerRef.current : undefined;
    }
 
    private getTransitionDelay(): number {
        Iif (this.timeoutDelay !== undefined) {
            return this.timeoutDelay;
        }
 
        // Cache the result to avoid unnecessary reflow
        this.timeoutDelay = parseFloat(this.contentRef.current ? getComputedStyle(this.contentRef.current).transitionDuration : '0s') * 1000;
 
        return this.timeoutDelay;
    }
 
    private handleTransitionEnd: (node: HTMLElement, done: () => void) => void = (node, done) => {
        node.addEventListener('transitionend', ({ target }) => {
            if (target === node) {
                done();
            }
        });
    };
}