import React from 'react' import { connect } from 'react-redux' import { selectStep } from '../actions/step' import { collapsibleSteps } from '../config' import MoreIcon from '../../images/icon-more.svg' type ExtraStepOption = { label: string callback: () => any } type ConfigurationStepProps = { active: boolean number: number title: string extraOptions?: ExtraStepOption[] className?: string children: any onClick: () => any } type ConfigurationStepState = { optionsMenuActive: boolean } class ConfigurationStep extends React.Component { static defaultProps = { extraOptions: [], className: '', } optionsRef: React.RefObject constructor(props: ConfigurationStepProps) { super(props) this.optionsRef = React.createRef() this.state = { optionsMenuActive: false } } toggleOptions = () => { const { optionsMenuActive } = this.state if (optionsMenuActive) { this.hideOptions() } else { this.showOptions() } } showOptions = () => { this.setState({ optionsMenuActive: true }) window.document.body.addEventListener('click', this.handleBodyClick) } hideOptions = () => { this.setState({ optionsMenuActive: false }) window.document.body.removeEventListener('click', this.handleBodyClick) } handleBodyClick = (e: MouseEvent) => { const { optionsMenuActive } = this.state if ( !optionsMenuActive || e.target === this.optionsRef?.current || this.optionsRef?.current?.contains(e.target as Node) ) { return } this.hideOptions() } render() { const { number, title, children, active, extraOptions, className, onClick } = this.props const { optionsMenuActive } = this.state if (collapsibleSteps) { return (
{ e.stopPropagation() onClick() }} >

{number}
{title}

{children}
) } return (

{number}
{title}
{extraOptions && extraOptions.length > 0 && (
{extraOptions.map(option => ( option.callback()}> {option.label} ))}
)}

{children}
) } } export default connect(null, (dispatch, { name }: { name: string }) => { return { onClick: () => { dispatch(selectStep(name)) }, } })(ConfigurationStep)