import * as React from 'react'; import * as Styles from './styles'; import { isEqual } from 'lodash'; import { Transition } from 'react-transition-group'; import Step from './StepPoint'; import StepLine from './StepLine'; import { drop, map, zip, filter } from 'lodash'; import { StepProps } from "./index"; import {STEP_POINT_STATE, ThemeProps} from "./styles"; import {COLOR_PALETTE} from "../../constants/colors"; const circleRadius = 8; export namespace StepperBar { export interface Props { steps: StepProps[]; onStepClick: (step: StepProps) => void; currentStep?: number; unlockedIndex: number; clickable: boolean; isDone: boolean; doneIcon: any; theme?: ThemeProps; } export interface State { unlockedIndex: number; isLoaded: boolean; svg: { width: number; height: number; shiftSteps: number; } } } export default class StepperBar extends React.Component { private gEl: any; private containerNode: any; constructor(props: StepperBar.Props) { super(props); this.state = { unlockedIndex: props.unlockedIndex, isLoaded: false, svg: { height: 0, width: 0, shiftSteps: 0, } } } componentWillReceiveProps(nextProps) { if (this.props.unlockedIndex !== nextProps.unlockedIndex) { this.setState({ unlockedIndex: nextProps.unlockedIndex }); } if (!isEqual(this.props.steps, nextProps.steps)) { this.updateBarPosition(); } } componentDidUpdate(prevProps, prevState) { // wait for the bars and points to be added to the g element before getting it's width if (this.gEl && this.containerNode) { this.updateBarPosition(); } } updateBarPosition = () => { if (this.gEl) { const { width } = this.gEl.getBoundingClientRect(); if (this.state.svg.shiftSteps !== width) { this.setState(state => ({ svg: { ...state.svg, shiftSteps: width, } })); } } }; handleContainerRef = node => { if (node) { const { height, width } = node.getBoundingClientRect(); this.containerNode = node; this.setState(state => ({ svg: { height, width, shiftSteps: state.svg.shiftSteps, } })) } }; render() { return ( this.gEl = node} transform={`translate(${((this.state.svg.width / 2) - (this.state.svg.shiftSteps / 2) + ((5 + circleRadius) * 2))}, 0)`}> {map(this.props.steps, (step, idx) => { const { width } = this.state.svg; const barWidth = (width * (2/3)) / this.props.steps.length; return ( <> {idx !== this.props.steps.length - 1 && ( {state => ( )} )} ) })} ); } }