2016-09-12 15:00:44 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2017-05-08 13:59:06 +03:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2016-09-12 15:00:44 +03: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.
|
|
|
|
*/
|
|
|
|
|
2021-07-20 12:04:29 +03:00
|
|
|
import React, { createRef } from 'react';
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as sdk from '../../../index';
|
2017-01-25 17:54:21 +03:00
|
|
|
import classNames from 'classnames';
|
2021-06-29 15:11:58 +03:00
|
|
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
2021-07-20 13:16:17 +03:00
|
|
|
import { IUserAddress } from '../../../UserAddress';
|
2016-09-12 15:00:44 +03:00
|
|
|
|
2021-07-20 12:04:29 +03:00
|
|
|
interface IProps {
|
|
|
|
onSelected: (index: number) => void;
|
|
|
|
|
|
|
|
// List of the addresses to display
|
2021-07-20 13:16:17 +03:00
|
|
|
addressList: IUserAddress[];
|
2021-07-20 12:04:29 +03:00
|
|
|
// Whether to show the address on the address tiles
|
|
|
|
showAddress?: boolean;
|
|
|
|
truncateAt: number;
|
|
|
|
selected?: number;
|
|
|
|
|
|
|
|
// Element to put as a header on top of the list
|
|
|
|
header?: JSX.Element;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
selected: number;
|
|
|
|
hover: boolean;
|
|
|
|
}
|
|
|
|
|
2021-03-09 05:59:41 +03:00
|
|
|
@replaceableComponent("views.elements.AddressSelector")
|
2021-07-20 12:04:29 +03:00
|
|
|
export default class AddressSelector extends React.Component<IProps, IState> {
|
|
|
|
private scrollElement = createRef<HTMLDivElement>();
|
|
|
|
private addressListElement = createRef<HTMLDivElement>();
|
2020-08-29 14:14:16 +03:00
|
|
|
|
2021-07-20 12:04:29 +03:00
|
|
|
constructor(props: IProps) {
|
2020-08-29 14:14:16 +03:00
|
|
|
super(props);
|
2016-09-12 15:00:44 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
this.state = {
|
2016-09-12 15:00:44 +03:00
|
|
|
selected: this.props.selected === undefined ? 0 : this.props.selected,
|
|
|
|
hover: false,
|
|
|
|
};
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-09-12 15:00:44 +03:00
|
|
|
|
2020-04-01 23:35:39 +03:00
|
|
|
// TODO: [REACT-WARNING] Replace with appropriate lifecycle event
|
2021-07-20 12:04:29 +03:00
|
|
|
UNSAFE_componentWillReceiveProps(props: IProps) { // eslint-disable-line
|
2016-09-12 15:00:44 +03:00
|
|
|
// Make sure the selected item isn't outside the list bounds
|
2017-10-11 19:56:17 +03:00
|
|
|
const selected = this.state.selected;
|
2021-07-20 12:04:29 +03:00
|
|
|
const maxSelected = this.maxSelected(props.addressList);
|
2016-09-12 15:00:44 +03:00
|
|
|
if (selected > maxSelected) {
|
2016-09-12 16:07:51 +03:00
|
|
|
this.setState({ selected: maxSelected });
|
2016-09-12 15:00:44 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-09-12 15:00:44 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
componentDidUpdate() {
|
2016-09-12 15:00:44 +03:00
|
|
|
// As the user scrolls with the arrow keys keep the selected item
|
|
|
|
// at the top of the window.
|
2021-07-20 12:04:29 +03:00
|
|
|
if (this.scrollElement.current && this.props.addressList.length > 0 && !this.state.hover) {
|
|
|
|
const elementHeight = this.addressListElement.current.getBoundingClientRect().height;
|
|
|
|
this.scrollElement.current.scrollTop = (this.state.selected * elementHeight) - elementHeight;
|
2016-09-12 15:00:44 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-09-12 15:00:44 +03:00
|
|
|
|
2021-07-20 12:04:29 +03:00
|
|
|
public moveSelectionTop = (): void => {
|
2017-02-23 15:12:25 +03:00
|
|
|
if (this.state.selected > 0) {
|
|
|
|
this.setState({
|
|
|
|
selected: 0,
|
|
|
|
hover: false,
|
|
|
|
});
|
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2017-02-23 15:12:25 +03:00
|
|
|
|
2021-07-20 12:04:29 +03:00
|
|
|
public moveSelectionUp = (): void => {
|
2016-09-12 15:00:44 +03:00
|
|
|
if (this.state.selected > 0) {
|
|
|
|
this.setState({
|
|
|
|
selected: this.state.selected - 1,
|
2017-10-11 19:56:17 +03:00
|
|
|
hover: false,
|
2016-09-12 15:00:44 +03:00
|
|
|
});
|
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2016-09-12 15:00:44 +03:00
|
|
|
|
2021-07-20 12:04:29 +03:00
|
|
|
public moveSelectionDown = (): void => {
|
|
|
|
if (this.state.selected < this.maxSelected(this.props.addressList)) {
|
2016-09-12 15:00:44 +03:00
|
|
|
this.setState({
|
|
|
|
selected: this.state.selected + 1,
|
2017-10-11 19:56:17 +03:00
|
|
|
hover: false,
|
2016-09-12 15:00:44 +03:00
|
|
|
});
|
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2016-09-12 15:00:44 +03:00
|
|
|
|
2021-07-20 12:04:29 +03:00
|
|
|
public chooseSelection = (): void => {
|
2016-09-12 15:00:44 +03:00
|
|
|
this.selectAddress(this.state.selected);
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2016-09-12 15:00:44 +03:00
|
|
|
|
2021-07-20 12:04:29 +03:00
|
|
|
private onClick = (index: number): void => {
|
2017-01-18 20:51:39 +03:00
|
|
|
this.selectAddress(index);
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2016-09-12 15:00:44 +03:00
|
|
|
|
2021-07-20 12:04:29 +03:00
|
|
|
private onMouseEnter = (index: number): void => {
|
2017-01-18 20:51:39 +03:00
|
|
|
this.setState({
|
|
|
|
selected: index,
|
|
|
|
hover: true,
|
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2016-09-12 15:00:44 +03:00
|
|
|
|
2021-07-20 12:04:29 +03:00
|
|
|
private onMouseLeave = (): void => {
|
2017-10-11 19:56:17 +03:00
|
|
|
this.setState({ hover: false });
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2016-09-12 15:00:44 +03:00
|
|
|
|
2021-07-20 12:04:29 +03:00
|
|
|
private selectAddress = (index: number): void => {
|
2016-09-12 17:21:17 +03: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 14:14:16 +03:00
|
|
|
};
|
2016-09-12 15:00:44 +03:00
|
|
|
|
2021-07-20 12:04:29 +03:00
|
|
|
private createAddressListTiles(): JSX.Element[] {
|
2017-10-11 19:56:17 +03:00
|
|
|
const AddressTile = sdk.getComponent("elements.AddressTile");
|
2021-07-20 12:04:29 +03:00
|
|
|
const maxSelected = this.maxSelected(this.props.addressList);
|
2017-10-11 19:56:17 +03:00
|
|
|
const addressList = [];
|
2016-09-12 15:00:44 +03:00
|
|
|
|
|
|
|
// Only create the address elements if there are address
|
|
|
|
if (this.props.addressList.length > 0) {
|
2017-10-11 19:56:17 +03:00
|
|
|
for (let i = 0; i <= maxSelected; i++) {
|
|
|
|
const classes = classNames({
|
2016-09-12 15:00:44 +03: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 15:12:25 +03:00
|
|
|
<div
|
|
|
|
className={classes}
|
|
|
|
onClick={this.onClick.bind(this, i)}
|
|
|
|
onMouseEnter={this.onMouseEnter.bind(this, i)}
|
|
|
|
onMouseLeave={this.onMouseLeave}
|
2017-05-08 13:59:06 +03:00
|
|
|
key={this.props.addressList[i].addressType + "/" + this.props.addressList[i].address}
|
2021-07-20 12:04:29 +03:00
|
|
|
ref={this.addressListElement}
|
2017-02-23 15:12:25 +03:00
|
|
|
>
|
2017-10-17 20:02:35 +03:00
|
|
|
<AddressTile
|
|
|
|
address={this.props.addressList[i]}
|
|
|
|
showAddress={this.props.showAddress}
|
|
|
|
justified={true}
|
|
|
|
networkName="vector"
|
2019-01-11 04:37:28 +03:00
|
|
|
networkUrl={require("../../../../res/img/search-icon-vector.svg")}
|
2017-10-17 20:02:35 +03:00
|
|
|
/>
|
2017-10-11 19:56:17 +03:00
|
|
|
</div>,
|
2016-09-12 15:00:44 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return addressList;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-09-12 15:00:44 +03:00
|
|
|
|
2021-07-20 12:04:29 +03:00
|
|
|
private maxSelected(list): number {
|
2017-10-11 19:56:17 +03: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 15:00:44 +03:00
|
|
|
return maxSelected;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-09-12 15:00:44 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
render() {
|
2017-10-11 19:56:17 +03:00
|
|
|
const classes = classNames({
|
2016-09-12 15:00:44 +03:00
|
|
|
"mx_AddressSelector": true,
|
|
|
|
"mx_AddressSelector_empty": this.props.addressList.length === 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
2021-07-20 12:04:29 +03:00
|
|
|
<div className={classes} ref={this.scrollElement}>
|
2017-01-17 17:48:50 +03:00
|
|
|
{ this.props.header }
|
2016-09-12 15:00:44 +03:00
|
|
|
{ this.createAddressListTiles() }
|
|
|
|
</div>
|
|
|
|
);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
|
|
|
}
|