2018-01-29 19:13:46 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
2018-01-02 18:04:50 +00:00
|
|
|
import './SwapDropdown.scss';
|
2018-03-01 17:53:29 +00:00
|
|
|
import { DropDown } from 'components/ui';
|
2018-01-02 18:04:50 +00:00
|
|
|
|
|
|
|
export interface SingleCoin {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
image: string;
|
|
|
|
status: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Props<T> {
|
|
|
|
options: SingleCoin[];
|
|
|
|
value: string;
|
|
|
|
onChange(value: T): void;
|
|
|
|
}
|
|
|
|
|
2018-03-01 17:53:29 +00:00
|
|
|
const ValueComp: React.SFC = (props: any) => {
|
|
|
|
return (
|
|
|
|
<div className={`${props.className} swap-option-wrapper`}>
|
|
|
|
<img src={props.value.img} className="swap-option-img" />
|
|
|
|
<span className="swap-option-label">{props.value.label}</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2018-01-02 18:04:50 +00:00
|
|
|
|
2018-03-01 17:53:29 +00:00
|
|
|
const OptionComp: React.SFC = (props: any) => {
|
2018-03-07 23:36:05 +00:00
|
|
|
const handleMouseDown = (event: React.MouseEvent<any>) => {
|
2018-03-01 17:53:29 +00:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
props.onSelect(props.option, event);
|
|
|
|
};
|
2018-03-07 23:36:05 +00:00
|
|
|
const handleMouseEnter = (event: React.MouseEvent<any>) => {
|
2018-03-01 17:53:29 +00:00
|
|
|
props.onFocus(props.option, event);
|
|
|
|
};
|
2018-03-07 23:36:05 +00:00
|
|
|
const handleMouseMove = (event: React.MouseEvent<any>) => {
|
2018-03-01 17:53:29 +00:00
|
|
|
if (props.isFocused) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
props.onFocus(props.option, event);
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={`${props.className} swap-option-wrapper`}
|
|
|
|
onMouseDown={handleMouseDown}
|
|
|
|
onMouseEnter={handleMouseEnter}
|
|
|
|
onMouseMove={handleMouseMove}
|
|
|
|
>
|
|
|
|
<img src={props.option.img} className="swap-option-img" />
|
|
|
|
<span className="swap-option-label">{props.option.label}</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2018-01-02 18:04:50 +00:00
|
|
|
|
2018-03-01 17:53:29 +00:00
|
|
|
class SwapDropdown<T> extends PureComponent<Props<T>> {
|
2018-01-02 18:04:50 +00:00
|
|
|
public render() {
|
2018-03-01 17:53:29 +00:00
|
|
|
const { options, value, onChange } = this.props;
|
|
|
|
const mappedOptions = options.map(opt => {
|
|
|
|
return { label: opt.id, value: opt.name, img: opt.image, status: opt.status };
|
2018-01-02 18:04:50 +00:00
|
|
|
});
|
|
|
|
return (
|
2018-03-01 17:53:29 +00:00
|
|
|
<DropDown
|
|
|
|
className="Swap-dropdown"
|
|
|
|
options={mappedOptions}
|
|
|
|
optionComponent={(props: any) => {
|
|
|
|
return <OptionComp {...props} />;
|
|
|
|
}}
|
|
|
|
value={value}
|
|
|
|
clearable={false}
|
|
|
|
onChange={onChange}
|
|
|
|
valueComponent={(props: any) => {
|
|
|
|
return <ValueComp {...props} />;
|
|
|
|
}}
|
|
|
|
/>
|
2018-01-02 18:04:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SwapDropdown;
|