-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
index.js
140 lines (116 loc) · 3.75 KB
/
index.js
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
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { KeyringOptions, KeyringOption$Type } from '@polkadot/ui-keyring/types';
import type { BareProps } from '../types';
import './InputAddress.css';
import React from 'react';
import keyring from '@polkadot/ui-keyring';
import createOptionHeader from '@polkadot/ui-keyring/options/header';
import addressDecode from '@polkadot/util-keyring/address/decode';
import Dropdown from '../Dropdown';
import classes from '../util/classes';
import addressToAddress from './addressToAddress';
type Props = BareProps & {
defaultValue?: string | Uint8Array,
hideAddress?: boolean;
isError?: boolean,
isInput?: boolean,
label?: string,
onChange: (value: Uint8Array) => void,
type?: KeyringOption$Type,
value?: string | Uint8Array,
withLabel?: boolean
};
type State = {
defaultValue: ?string,
options: KeyringOptions,
value?: string
}
const RECENT_KEY = 'header-recent';
const transform = (value: string): Uint8Array => {
try {
return addressDecode(value);
} catch (error) {
return new Uint8Array([]);
}
};
// NOTE: We are not extending Component here since the options may change in the keyring (which needs a re-render), however the input props will be the same (so, no PureComponent with shallow compare here)
export default class InputAddress extends React.Component<Props, State> {
constructor (props: Props) {
super(props);
this.state = ({
defaultValue: addressToAddress(props.defaultValue)
}: $Shape<State>);
}
static getDerivedStateFromProps ({ value }: Props): $Shape<State> | null {
try {
return {
value: addressToAddress(value) || undefined
};
} catch (error) {
return null;
}
}
render (): React$Node {
const { className, hideAddress = false, isError, label, onChange, type = 'all', style, withLabel } = this.props;
const { defaultValue, value } = this.state;
const options = keyring.getOptions(type);
return (
<Dropdown
className={classes('ui--InputAddress', hideAddress ? 'flag--hideAddress' : '', className)}
defaultValue={
value !== undefined
? undefined
: defaultValue
}
isError={isError}
label={label}
onChange={onChange}
onSearch={this.onSearch}
options={options}
style={style}
transform={transform}
value={value}
withLabel={withLabel}
/>
);
}
onSearch = (filteredOptions: KeyringOptions, query: string): KeyringOptions => {
const { isInput = true } = this.props;
const queryLower = query.toLowerCase();
const matches = filteredOptions.filter((item) => {
if (item.value === null) {
return true;
}
const { name, value } = item;
const hasMatch = name.toLowerCase().indexOf(queryLower) !== -1 ||
value.toLowerCase().indexOf(queryLower) !== -1;
return hasMatch;
});
const valueMatches = matches.filter((item) => item.value !== null);
if (isInput && valueMatches.length === 0) {
const publicKey = transform(query);
if (publicKey.length === 32) {
if (!matches.find((item) => item.key === RECENT_KEY)) {
matches.push(
createOptionHeader('Recent')
);
}
matches.push(
keyring.saveRecent(query)
);
}
}
return matches.filter((item, index) => {
const isLast = index === matches.length - 1;
const nextItem = matches[index + 1];
const hasNext = nextItem && nextItem.value;
if (item.value !== null || (!isLast && hasNext)) {
return true;
}
return false;
});
};
}