Skip to content
This repository has been archived by the owner on Mar 3, 2019. It is now read-only.

Initial setup of filterbox on popover. #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/components/app/PairsFilterBox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React, { Component } from 'react';

class PairsFilterBox extends Component {
constructor(props) {
super(props);
this.state = {filterText: ''};
this.onStateChange = this.onStateChange.bind(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using funcName = () => {} at class level?

this.onFilterChange = (e) => {this.onStateChange('filterText', e.target.value)};
this.renderChildren = this.renderChildren.bind(this);
this.getFilteredPairs = this.getFilteredPairs.bind(this);
}

onStateChange(key, value) {
this.setState({[key]: value});
}

getFilteredPairs(pairs) {
const ret = {};
const filterText = this.state.filterText.toLowerCase();

if(filterText.length === 0) return pairs;

const pairKeys = Object.keys(pairs);
pairKeys.forEach((key) => {
const pair = pairs[key];

if(pair.addr.toLowerCase().includes(filterText) || pair.name.toLowerCase().includes(filterText)) {
ret[key] = pair;
}
});

return ret;
}

renderChildren() {
const filteredPairs = this.getFilteredPairs(this.props.pairs);

return React.Children.map(this.props.children, child => {
return React.cloneElement(child, {
pairs: filteredPairs
});
});
}

render() {
return (
<div>
<input type="search" value={this.state.filterText} style={{width: '100%'}}
placeholder="Filter by token (ETH) or contract address (0x0...)" onChange={this.onFilterChange}/>
<div>
{this.renderChildren()}
</div>
</div>
);
}
}

export default PairsFilterBox;
7 changes: 6 additions & 1 deletion src/components/app/PairsPopover.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ import { AnchorButton, PopoverInteractionKind } from '@blueprintjs/core';

import PairsTable from './PairsTable';
import PopoverBlockingScroll from '../common/PopoverBlockingScroll';
import PairsFilterBox from './PairsFilterBox';

function PairsPopover({ pairs, lastUpdated }) {
return (
<PopoverBlockingScroll
content={<PairsTable pairs={pairs} lastUpdated={lastUpdated} />}
content={
<PairsFilterBox pairs={pairs}>
<PairsTable lastUpdated={lastUpdated} />
</PairsFilterBox>
}
inline={false}
interactionKind={PopoverInteractionKind.CLICK}
placement="bottom-start"
Expand Down