import React from 'react'; import { connect } from 'react-redux'; import { ICurrentTo } from 'features/types'; import { transactionActions } from 'features/transaction'; import { Query } from 'components/renderCbs'; import { AddressInputFactory } from './AddressInputFactory'; import './AddressFieldFactory.scss'; interface DispatchProps { setCurrentTo: transactionActions.TSetCurrentTo; } interface OwnProps { to: string | null; isSelfAddress?: boolean; showLabelMatch?: boolean; withProps(props: CallbackProps): React.ReactElement | null; } interface State { isFocused: boolean; } export interface CallbackProps { isValid: boolean; isLabelEntry: boolean; readOnly: boolean; currentTo: ICurrentTo; onChange(ev: React.FormEvent): void; onFocus(ev: React.FormEvent): void; onBlur(ev: React.FormEvent): void; } type Props = DispatchProps & OwnProps; class AddressFieldFactoryClass extends React.Component { public state: State = { isFocused: false }; private goingToBlur: number | null = null; public componentDidMount() { // this 'to' parameter can be either token or actual field related const { to } = this.props; if (to) { this.props.setCurrentTo(to); } } public componentWillUnmount() { if (this.goingToBlur) { window.clearTimeout(this.goingToBlur); } } public render() { return (
); } private focus = () => this.setState({ isFocused: true }); private blur = () => this.setState({ isFocused: false }); private setAddress = (ev: React.FormEvent) => { const { value } = ev.currentTarget; this.props.setCurrentTo(value); }; private setBlurTimeout = () => (this.goingToBlur = window.setTimeout(this.blur, 150)); } const AddressFieldFactory = connect(null, { setCurrentTo: transactionActions.setCurrentTo })( AddressFieldFactoryClass ); interface DefaultAddressFieldProps { isSelfAddress?: boolean; showLabelMatch?: boolean; withProps(props: CallbackProps): React.ReactElement | null; } const DefaultAddressField: React.SFC = ({ isSelfAddress, showLabelMatch, withProps }) => ( ( )} /> ); export { DefaultAddressField as AddressFieldFactory };