Skip to content

Commit 08252ab

Browse files
committed
update not to use jquery
filter clear function implemented
1 parent 84361e2 commit 08252ab

File tree

2 files changed

+53
-28
lines changed

2 files changed

+53
-28
lines changed

.eslintrc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,4 @@
1515
"react/jsx-uses-react": "error",
1616
"react/jsx-uses-vars": "error"
1717
},
18-
"env": {
19-
"jquery": true
20-
}
2118
}

scripts/components/App.js

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ export default class App extends React.Component {
1919
super(props);
2020
this.selectBranch = this.selectBranch.bind(this);
2121
this.selectCount = this.selectCount.bind(this);
22+
this.clearFilter = this.clearFilter.bind(this);
23+
this.changeFilter = this.changeFilter.bind(this);
2224
this.state = {
2325
maxLane: 0,
2426
commits: [],
2527
branches: [],
2628
currentBranch: App.allBranchName,
2729
isFetching: true,
2830
count: 100,
31+
displayCount: 100,
32+
filter: '',
2933
};
3034
}
3135

@@ -49,6 +53,7 @@ export default class App extends React.Component {
4953
}
5054

5155
selectBranch(branch) {
56+
if (branch.startsWith('.')) return;
5257
const params = this.setParams(branch, { count: this.state.count });
5358
this.fetchData(params);
5459
}
@@ -64,7 +69,12 @@ export default class App extends React.Component {
6469
return window.fetch(`./network/commits?${query}`, { credentials: 'include' }).then(
6570
r => r.json()
6671
).then(data => {
67-
const newState = Object.assign({ count: params.count, isFetching: false }, data);
72+
const newState = Object.assign({ count: params.count, isFetching: false },
73+
data,
74+
{ branches: data.branches.map(
75+
branch => ({ branch, visible: true })) },
76+
{ displayCount: data.branches.length }
77+
);
6878
if (data.all) {
6979
newState.currentBranch = App.allBranchName;
7080
}
@@ -82,22 +92,32 @@ export default class App extends React.Component {
8292
e.stopPropagation();
8393
}
8494

85-
doFilter(e) {
86-
const branchFilter = $(e.target);
87-
const inputVal = branchFilter.val();
88-
$.each(
89-
branchFilter.parent().parent().parent()
90-
.find('a'),
91-
(index, elem) => {
92-
if (!inputVal ||
93-
!elem.text.trim() ||
94-
elem.text.trim().toLowerCase().indexOf(inputVal.toLowerCase()) >= 0) {
95-
$(elem).parent().show();
96-
} else {
97-
$(elem).parent().hide();
98-
}
99-
}
100-
);
95+
changeFilter(e) {
96+
this.filter(e.target.value);
97+
}
98+
99+
clearFilter(e) {
100+
e.preventDefault();
101+
e.stopPropagation();
102+
103+
this.filter('');
104+
}
105+
106+
filter(value) {
107+
this.setState(Object.assign(this.state, { filter: value }));
108+
109+
this.setState(Object.assign(this.state, {
110+
branches: this.state.branches.map(({ branch }) => ({
111+
branch,
112+
visible: this.state.filter ?
113+
branch.toLowerCase().indexOf(this.state.filter.toLowerCase()) >= 0 : true,
114+
})),
115+
}));
116+
117+
this.setState(Object.assign(this.state, {
118+
displayCount: this.state.branches.reduce((prev, current) =>
119+
prev + (current.visible ? 1 : 0), 0),
120+
}));
101121
}
102122

103123
render() {
@@ -116,18 +136,26 @@ export default class App extends React.Component {
116136
{App.defaultBranchName}
117137
</MenuItem>
118138
<MenuItem>
119-
<FormControl
120-
type="text"
121-
value={this.state.value}
122-
placeholder="Find branch..."
123-
onClick={this.stopPropagation}
124-
onKeyUp={this.doFilter}
125-
/>
139+
<div style={{ display: 'flex', alignItems: 'center' }}>
140+
<FormControl
141+
type="text" placeholder="Find branch..."
142+
style={{ width: '90%' }}
143+
value={this.state.filter}
144+
onClick={this.stopPropagation}
145+
onChange={this.changeFilter}
146+
/>
147+
<i
148+
className="octicon octicon-x" style={{ display: 'flex' }}
149+
onClick={this.clearFilter}
150+
/>
151+
</div>
126152
</MenuItem>
127153
<MenuItem divider />
128-
{this.state.branches.map(branch =>
154+
{this.state.branches.filter(({ visible }) => visible).map(({ branch }) =>
129155
<MenuItem key={branch} eventKey={branch}>{branch}</MenuItem>
130156
)}
157+
{this.state.displayCount === 0 ?
158+
(<MenuItem eventKey=".">NO MATCHED BRANCH</MenuItem>) : (null)}
131159
</DropdownButton>
132160
<DropdownButton
133161
id="countSelector"

0 commit comments

Comments
 (0)