2016-09-12 12:00:44 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2017-05-08 10:59:06 +00:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2016-09-12 12:00:44 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-01-25 14:54:21 +00:00
|
|
|
import React from 'react';
|
2017-12-26 01:03:18 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2019-12-20 01:19:56 +00:00
|
|
|
import * as sdk from '../../../index';
|
2017-01-25 14:54:21 +00:00
|
|
|
import classNames from 'classnames';
|
2017-08-15 12:42:23 +00:00
|
|
|
import { UserAddressType } from '../../../UserAddress';
|
2021-03-09 02:59:41 +00:00
|
|
|
import {replaceableComponent} from "../../../utils/replaceableComponent";
|
2016-09-12 12:00:44 +00:00
|
|
|
|
2021-03-09 02:59:41 +00:00
|
|
|
@replaceableComponent("views.elements.AddressSelector")
|
2020-08-29 11:14:16 +00:00
|
|
|
export default class AddressSelector extends React.Component {
|
|
|
|
static propTypes = {
|
2017-12-26 01:03:18 +00:00
|
|
|
onSelected: PropTypes.func.isRequired,
|
2017-01-18 15:21:50 +00:00
|
|
|
|
2017-01-24 18:23:34 +00:00
|
|
|
// List of the addresses to display
|
2017-12-26 01:03:18 +00:00
|
|
|
addressList: PropTypes.arrayOf(UserAddressType).isRequired,
|
2017-10-17 17:02:35 +00:00
|
|
|
// Whether to show the address on the address tiles
|
2017-12-26 01:03:18 +00:00
|
|
|
showAddress: PropTypes.bool,
|
|
|
|
truncateAt: PropTypes.number.isRequired,
|
|
|
|
selected: PropTypes.number,
|
2017-01-17 14:48:50 +00:00
|
|
|
|
|
|
|
// Element to put as a header on top of the list
|
2017-12-26 01:03:18 +00:00
|
|
|
header: PropTypes.node,
|
2020-08-29 11:14:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-09-12 12:00:44 +00:00
|
|
|
|
2020-08-29 11:14:16 +00:00
|
|
|
this.state = {
|
2016-09-12 12:00:44 +00:00
|
|
|
selected: this.props.selected === undefined ? 0 : this.props.selected,
|
|
|
|
hover: false,
|
|
|
|
};
|
2020-08-29 11:14:16 +00:00
|
|
|
}
|
2016-09-12 12:00:44 +00:00
|
|
|
|
2020-04-01 20:35:39 +00:00
|
|
|
// TODO: [REACT-WARNING] Replace with appropriate lifecycle event
|
2020-10-30 18:18:17 +00:00
|
|
|
UNSAFE_componentWillReceiveProps(props) { // eslint-disable-line camelcase
|
2016-09-12 12:00:44 +00:00
|
|
|
// Make sure the selected item isn't outside the list bounds
|
2017-10-11 16:56:17 +00:00
|
|
|
const selected = this.state.selected;
|
|
|
|
const maxSelected = this._maxSelected(props.addressList);
|
2016-09-12 12:00:44 +00:00
|
|
|
if (selected > maxSelected) {
|
2016-09-12 13:07:51 +00:00
|
|
|
this.setState({ selected: maxSelected });
|
2016-09-12 12:00:44 +00:00
|
|
|
}
|
2020-08-29 11:14:16 +00:00
|
|
|
}
|
2016-09-12 12:00:44 +00:00
|
|
|
|
2020-08-29 11:14:16 +00:00
|
|
|
componentDidUpdate() {
|
2016-09-12 12:00:44 +00:00
|
|
|
// As the user scrolls with the arrow keys keep the selected item
|
|
|
|
// at the top of the window.
|
|
|
|
if (this.scrollElement && this.props.addressList.length > 0 && !this.state.hover) {
|
2017-10-11 16:56:17 +00:00
|
|
|
const elementHeight = this.addressListElement.getBoundingClientRect().height;
|
2016-09-12 12:00:44 +00:00
|
|
|
this.scrollElement.scrollTop = (this.state.selected * elementHeight) - elementHeight;
|
|
|
|
}
|
2020-08-29 11:14:16 +00:00
|
|
|
}
|
2016-09-12 12:00:44 +00:00
|
|
|
|
2020-08-29 11:14:16 +00:00
|
|
|
moveSelectionTop = () => {
|
2017-02-23 12:12:25 +00:00
|
|
|
if (this.state.selected > 0) {
|
|
|
|
this.setState({
|
|
|
|
selected: 0,
|
|
|
|
hover: false,
|
|
|
|
});
|
|
|
|
}
|
2020-08-29 11:14:16 +00:00
|
|
|
};
|
2017-02-23 12:12:25 +00:00
|
|
|
|
2020-08-29 11:14:16 +00:00
|
|
|
moveSelectionUp = () => {
|
2016-09-12 12:00:44 +00:00
|
|
|
if (this.state.selected > 0) {
|
|
|
|
this.setState({
|
|
|
|
selected: this.state.selected - 1,
|
2017-10-11 16:56:17 +00:00
|
|
|
hover: false,
|
2016-09-12 12:00:44 +00:00
|
|
|
});
|
|
|
|
}
|
2020-08-29 11:14:16 +00:00
|
|
|
};
|
2016-09-12 12:00:44 +00:00
|
|
|
|
2020-08-29 11:14:16 +00:00
|
|
|
moveSelectionDown = () => {
|
2016-09-12 12:00:44 +00:00
|
|
|
if (this.state.selected < this._maxSelected(this.props.addressList)) {
|
|
|
|
this.setState({
|
|
|
|
selected: this.state.selected + 1,
|
2017-10-11 16:56:17 +00:00
|
|
|
hover: false,
|
2016-09-12 12:00:44 +00:00
|
|
|
});
|
|
|
|
}
|
2020-08-29 11:14:16 +00:00
|
|
|
};
|
2016-09-12 12:00:44 +00:00
|
|
|
|
2020-08-29 11:14:16 +00:00
|
|
|
chooseSelection = () => {
|
2016-09-12 12:00:44 +00:00
|
|
|
this.selectAddress(this.state.selected);
|
2020-08-29 11:14:16 +00:00
|
|
|
};
|
2016-09-12 12:00:44 +00:00
|
|
|
|
2020-08-29 11:14:16 +00:00
|
|
|
onClick = index => {
|
2017-01-18 17:51:39 +00:00
|
|
|
this.selectAddress(index);
|
2020-08-29 11:14:16 +00:00
|
|
|
};
|
2016-09-12 12:00:44 +00:00
|
|
|
|
2020-08-29 11:14:16 +00:00
|
|
|
onMouseEnter = index => {
|
2017-01-18 17:51:39 +00:00
|
|
|
this.setState({
|
|
|
|
selected: index,
|
|
|
|
hover: true,
|
|
|
|
});
|
2020-08-29 11:14:16 +00:00
|
|
|
};
|
2016-09-12 12:00:44 +00:00
|
|
|
|
2020-08-29 11:14:16 +00:00
|
|
|
onMouseLeave = () => {
|
2017-10-11 16:56:17 +00:00
|
|
|
this.setState({ hover: false });
|
2020-08-29 11:14:16 +00:00
|
|
|
};
|
2016-09-12 12:00:44 +00:00
|
|
|
|
2020-08-29 11:14:16 +00:00
|
|
|
selectAddress = index => {
|
2016-09-12 14:21:17 +00:00
|
|
|
// Only try to select an address if one exists
|
|
|
|
if (this.props.addressList.length !== 0) {
|
|
|
|
this.props.onSelected(index);
|
|
|
|
this.setState({ hover: false });
|
|
|
|
}
|
2020-08-29 11:14:16 +00:00
|
|
|
};
|
2016-09-12 12:00:44 +00:00
|
|
|
|
2020-08-29 11:14:16 +00:00
|
|
|
createAddressListTiles() {
|
2017-10-11 16:56:17 +00:00
|
|
|
const AddressTile = sdk.getComponent("elements.AddressTile");
|
|
|
|
const maxSelected = this._maxSelected(this.props.addressList);
|
|
|
|
const addressList = [];
|
2016-09-12 12:00:44 +00:00
|
|
|
|
|
|
|
// Only create the address elements if there are address
|
|
|
|
if (this.props.addressList.length > 0) {
|
2017-10-11 16:56:17 +00:00
|
|
|
for (let i = 0; i <= maxSelected; i++) {
|
|
|
|
const classes = classNames({
|
2016-09-12 12:00:44 +00:00
|
|
|
"mx_AddressSelector_addressListElement": true,
|
|
|
|
"mx_AddressSelector_selected": this.state.selected === i,
|
|
|
|
});
|
|
|
|
|
|
|
|
// NOTE: Defaulting to "vector" as the network, until the network backend stuff is done.
|
|
|
|
// Saving the addressListElement so we can use it to work out, in the componentDidUpdate
|
|
|
|
// method, how far to scroll when using the arrow keys
|
|
|
|
addressList.push(
|
2017-02-23 12:12:25 +00:00
|
|
|
<div
|
|
|
|
className={classes}
|
|
|
|
onClick={this.onClick.bind(this, i)}
|
|
|
|
onMouseEnter={this.onMouseEnter.bind(this, i)}
|
|
|
|
onMouseLeave={this.onMouseLeave}
|
2017-05-08 10:59:06 +00:00
|
|
|
key={this.props.addressList[i].addressType + "/" + this.props.addressList[i].address}
|
2017-02-23 12:12:25 +00:00
|
|
|
ref={(ref) => { this.addressListElement = ref; }}
|
|
|
|
>
|
2017-10-17 17:02:35 +00:00
|
|
|
<AddressTile
|
|
|
|
address={this.props.addressList[i]}
|
|
|
|
showAddress={this.props.showAddress}
|
|
|
|
justified={true}
|
|
|
|
networkName="vector"
|
2019-01-11 01:37:28 +00:00
|
|
|
networkUrl={require("../../../../res/img/search-icon-vector.svg")}
|
2017-10-17 17:02:35 +00:00
|
|
|
/>
|
2017-10-11 16:56:17 +00:00
|
|
|
</div>,
|
2016-09-12 12:00:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return addressList;
|
2020-08-29 11:14:16 +00:00
|
|
|
}
|
2016-09-12 12:00:44 +00:00
|
|
|
|
2020-08-29 11:14:16 +00:00
|
|
|
_maxSelected(list) {
|
2017-10-11 16:56:17 +00:00
|
|
|
const listSize = list.length === 0 ? 0 : list.length - 1;
|
|
|
|
const maxSelected = listSize > (this.props.truncateAt - 1) ? (this.props.truncateAt - 1) : listSize;
|
2016-09-12 12:00:44 +00:00
|
|
|
return maxSelected;
|
2020-08-29 11:14:16 +00:00
|
|
|
}
|
2016-09-12 12:00:44 +00:00
|
|
|
|
2020-08-29 11:14:16 +00:00
|
|
|
render() {
|
2017-10-11 16:56:17 +00:00
|
|
|
const classes = classNames({
|
2016-09-12 12:00:44 +00:00
|
|
|
"mx_AddressSelector": true,
|
|
|
|
"mx_AddressSelector_empty": this.props.addressList.length === 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
2017-01-20 14:22:27 +00:00
|
|
|
<div className={classes} ref={(ref) => {this.scrollElement = ref;}}>
|
2017-01-17 14:48:50 +00:00
|
|
|
{ this.props.header }
|
2016-09-12 12:00:44 +00:00
|
|
|
{ this.createAddressListTiles() }
|
|
|
|
</div>
|
|
|
|
);
|
2020-08-29 11:14:16 +00:00
|
|
|
}
|
|
|
|
}
|