Files

147 lines
3.7 KiB
TypeScript
Raw Permalink Normal View History

2017-12-18 16:23:31 -05:00
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';
2018-05-21 18:10:51 -05:00
import './AddressFieldFactory.scss';
2017-12-18 16:23:31 -05:00
interface DispatchProps {
setCurrentTo: transactionActions.TSetCurrentTo;
2017-12-18 16:23:31 -05:00
}
interface OwnProps {
to: string | null;
2018-03-22 14:30:51 -04:00
isSelfAddress?: boolean;
2018-05-21 18:10:51 -05:00
showLabelMatch?: boolean;
2018-07-12 12:49:21 -05:00
showIdenticon?: boolean;
value?: string;
dropdownThreshold?: number;
2018-01-02 12:04:50 -06:00
withProps(props: CallbackProps): React.ReactElement<any> | null;
2018-07-12 13:04:22 -05:00
onChangeOverride?(ev: React.FormEvent<HTMLInputElement>): void;
2018-01-02 12:04:50 -06:00
}
2018-05-21 18:10:51 -05:00
interface State {
isFocused: boolean;
}
2018-01-02 12:04:50 -06:00
export interface CallbackProps {
isValid: boolean;
2018-05-21 18:10:51 -05:00
isLabelEntry: boolean;
2018-01-02 12:04:50 -06:00
readOnly: boolean;
currentTo: ICurrentTo;
onChange(ev: React.FormEvent<HTMLInputElement>): void;
2018-05-21 18:10:51 -05:00
onFocus(ev: React.FormEvent<HTMLInputElement>): void;
onBlur(ev: React.FormEvent<HTMLInputElement>): void;
2017-12-18 16:23:31 -05:00
}
type Props = DispatchProps & OwnProps;
2017-12-18 16:23:31 -05:00
class AddressFieldFactoryClass extends React.Component<Props> {
2018-05-21 18:10:51 -05:00
public state: State = {
isFocused: false
};
private goingToBlur: number | null = null;
2017-12-18 16:23:31 -05:00
public componentDidMount() {
// this 'to' parameter can be either token or actual field related
const { to } = this.props;
if (to) {
this.props.setCurrentTo(to);
}
}
2018-05-21 18:10:51 -05:00
public componentWillUnmount() {
if (this.goingToBlur) {
window.clearTimeout(this.goingToBlur);
}
}
2017-12-18 16:23:31 -05:00
public render() {
2018-07-12 12:49:21 -05:00
const {
isSelfAddress,
showLabelMatch,
withProps,
showIdenticon,
onChangeOverride,
value,
dropdownThreshold
} = this.props;
2018-03-22 14:30:51 -04:00
return (
2018-05-21 18:10:51 -05:00
<div className="AddressField">
<AddressInputFactory
2018-07-12 12:49:21 -05:00
isSelfAddress={isSelfAddress}
showLabelMatch={showLabelMatch}
withProps={withProps}
showIdenticon={showIdenticon}
onChangeOverride={onChangeOverride}
value={value}
dropdownThreshold={dropdownThreshold}
2018-05-21 18:10:51 -05:00
isFocused={this.state.isFocused}
onChange={this.setAddress}
onFocus={this.focus}
onBlur={this.setBlurTimeout}
/>
</div>
2018-03-22 14:30:51 -04:00
);
2017-12-18 16:23:31 -05:00
}
2018-05-21 18:10:51 -05:00
private focus = () => this.setState({ isFocused: true });
private blur = () => this.setState({ isFocused: false });
2017-12-18 16:23:31 -05:00
private setAddress = (ev: React.FormEvent<HTMLInputElement>) => {
2018-07-12 12:49:21 -05:00
const { onChangeOverride, setCurrentTo } = this.props;
2017-12-18 16:23:31 -05:00
const { value } = ev.currentTarget;
2018-07-12 12:49:21 -05:00
onChangeOverride ? onChangeOverride(ev) : setCurrentTo(value);
2017-12-18 16:23:31 -05:00
};
2018-05-21 18:10:51 -05:00
private setBlurTimeout = () => (this.goingToBlur = window.setTimeout(this.blur, 150));
2017-12-18 16:23:31 -05:00
}
const AddressFieldFactory = connect(null, { setCurrentTo: transactionActions.setCurrentTo })(
AddressFieldFactoryClass
);
2017-12-18 16:23:31 -05:00
2018-01-02 12:04:50 -06:00
interface DefaultAddressFieldProps {
2018-03-22 14:30:51 -04:00
isSelfAddress?: boolean;
2018-05-21 18:10:51 -05:00
showLabelMatch?: boolean;
2018-07-12 12:49:21 -05:00
showIdenticon?: boolean;
value?: string;
dropdownThreshold?: number;
2018-01-02 12:04:50 -06:00
withProps(props: CallbackProps): React.ReactElement<any> | null;
2018-07-12 13:04:22 -05:00
onChangeOverride?(ev: React.FormEvent<HTMLInputElement>): void;
2018-01-02 12:04:50 -06:00
}
2018-05-21 18:10:51 -05:00
const DefaultAddressField: React.SFC<DefaultAddressFieldProps> = ({
isSelfAddress,
showLabelMatch,
2018-07-12 12:49:21 -05:00
showIdenticon,
value,
withProps,
onChangeOverride,
dropdownThreshold
2018-05-21 18:10:51 -05:00
}) => (
<Query
params={['to']}
2018-03-22 14:30:51 -04:00
withQuery={({ to }) => (
2018-05-21 18:10:51 -05:00
<AddressFieldFactory
to={to}
isSelfAddress={isSelfAddress}
showLabelMatch={showLabelMatch}
withProps={withProps}
2018-07-12 12:49:21 -05:00
showIdenticon={showIdenticon}
onChangeOverride={onChangeOverride}
value={value}
dropdownThreshold={dropdownThreshold}
2018-05-21 18:10:51 -05:00
/>
2018-03-22 14:30:51 -04:00
)}
/>
2017-12-18 16:23:31 -05:00
);
2018-01-02 12:04:50 -06:00
export { DefaultAddressField as AddressFieldFactory };