-
Notifications
You must be signed in to change notification settings - Fork 295
/
AssetInput.tsx
192 lines (180 loc) · 5.92 KB
/
AssetInput.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// @flow
import React, { Component } from 'react';
import { observer } from 'mobx-react';
import type { Field } from 'mobx-react-form';
import { intlShape } from 'react-intl';
import { get } from 'lodash';
import classNames from 'classnames';
import SVGInline from 'react-svg-inline';
import { NumericInput } from 'react-polymorph/lib/components/NumericInput';
import { PopOver } from 'react-polymorph/lib/components/PopOver';
import AmountInputSkin from '../skins/AmountInputSkin';
import closeIcon from '../../../assets/images/close-cross.inline.svg';
import removeIcon from '../../../assets/images/remove.inline.svg';
import type { NumberFormat } from '../../../../../common/types/number.types';
import { DiscreetTokenWalletAmount } from '../../../features/discreet-mode';
import Asset from '../../assets/Asset';
import styles from './AssetInput.scss';
import messages from './messages';
type Props = {
uniqueId: string,
getAssetByUniqueId: Function,
assetFields: {
[uniqueId: string]: Field,
},
addFocusableField: Function,
currentNumberFormat: NumberFormat,
removeAssetRow: Function,
handleSubmitOnEnter: Function,
clearAssetFieldValue: Function,
autoFocus: boolean,
};
const INPUT_FIELD_PADDING_DELTA = 10;
@observer
export default class AssetInput extends Component<Props> {
static contextTypes = {
intl: intlShape.isRequired,
};
rightContentRef: { current: null | HTMLDivElement };
constructor(props: Props) {
super(props);
this.rightContentRef = React.createRef();
}
hasAssetValue = (asset: Field) => {
return get(asset, 'value', false);
};
generateInputFieldStyle = () => {
const { current: rightContentDom } = this.rightContentRef;
if (!rightContentDom) {
return null;
}
const rightContentDomRect = rightContentDom.getBoundingClientRect();
return {
paddingRight: `${
rightContentDomRect.width + INPUT_FIELD_PADDING_DELTA
}px`,
};
};
render() {
const { intl } = this.context;
const {
uniqueId,
getAssetByUniqueId,
assetFields,
addFocusableField,
currentNumberFormat,
removeAssetRow,
handleSubmitOnEnter,
clearAssetFieldValue,
autoFocus,
} = this.props;
const asset = getAssetByUniqueId(uniqueId);
if (!asset) {
return false;
}
const { quantity, metadata, decimals } = asset;
const ticker = get(metadata, 'ticker', null);
const assetField = assetFields[uniqueId];
const inputFieldStyle = this.generateInputFieldStyle();
return (
<div key={`receiver_asset_${uniqueId}`} className={styles.component}>
<div className={styles.inputBlock}>
{quantity.isPositive() && (
<div className={styles.amountTokenTotal}>
{intl.formatMessage(messages.ofLabel)}
<span className={styles.amountValue}>
<DiscreetTokenWalletAmount
amount={quantity}
metadata={metadata}
decimals={decimals}
/>
</span>
</div>
)}
<NumericInput
{...assetField.bind()}
ref={(field) => addFocusableField(field)}
placeholder={
decimals
? `0${currentNumberFormat.decimalSeparator}${'0'.repeat(
decimals
)}`
: '0'
}
className={styles.assetItem}
label={
<Asset
asset={asset}
className={styles.assetToken}
hidePopOver
small
/>
}
bigNumberFormat={decimals ? currentNumberFormat : null}
decimalPlaces={decimals}
numberLocaleOptions={{
minimumFractionDigits: decimals,
}}
onChange={(value) => {
assetField.onChange(value);
}}
currency={ticker}
value={assetField.value}
error={assetField.error}
skin={AmountInputSkin}
style={inputFieldStyle}
onKeyPress={(evt: SyntheticKeyboardEvent<EventTarget>) => {
if (decimals === 0) {
const { charCode } = evt;
if (charCode === 190 || charCode === 110 || charCode === 46) {
evt.persist();
evt.preventDefault();
evt.stopPropagation();
}
}
handleSubmitOnEnter(evt);
}}
allowSigns={false}
autoFocus={autoFocus}
/>
<div className={styles.rightContent} ref={this.rightContentRef}>
{this.hasAssetValue(assetField) && (
<div className={styles.clearAssetContainer}>
<PopOver
content={intl.formatMessage(messages.clearLabel)}
placement="top"
>
<button
onClick={() => clearAssetFieldValue(assetField)}
className={styles.clearAssetButton}
>
<SVGInline
svg={closeIcon}
className={styles.clearReceiverIcon}
/>
</button>
</PopOver>
<div className={styles.separator} />
</div>
)}
{ticker ? (
<>
<span className={styles.divider} />
<span className={styles.ticker}>{ticker}</span>
</>
) : null}
</div>
</div>
<div className={styles.removeAssetBlock}>
<span
className={classNames([styles.removeAssetButton, 'flat'])}
onClick={() => removeAssetRow(uniqueId)}
data-testid={`remove-asset-${uniqueId}`}
>
<SVGInline svg={removeIcon} className={styles.removeIcon} />
</span>
</div>
</div>
);
}
}