Skip to content

Commit 3d77973

Browse files
raclimcatarak
authored andcommitted
Searchbar (#1132)
* search bar function * Fixes #231, adds searchbar to sketchlist * Fixes #231, update requested changes to searchbar * Fixes #231, reset search term after modal closed
1 parent 973bf7e commit 3d77973

File tree

15 files changed

+232
-6
lines changed

15 files changed

+232
-6
lines changed

client/constants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ export const SET_ASSETS = 'SET_ASSETS';
121121

122122
export const TOGGLE_DIRECTION = 'TOGGLE_DIRECTION';
123123
export const SET_SORTING = 'SET_SORTING';
124+
export const SET_SORT_PARAMS = 'SET_SORT_PARAMS';
125+
export const SET_SEARCH_TERM = 'SET_SEARCH_TERM';
126+
export const CLOSE_SKETCHLIST_MODAL = 'CLOSE_SKETCHLIST_MODAL';
124127

125128
export const START_LOADING = 'START_LOADING';
126129
export const STOP_LOADING = 'STOP_LOADING';

client/images/magnifyingglass.svg

Lines changed: 13 additions & 0 deletions
Loading

client/modules/IDE/actions/sorting.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,14 @@ export function toggleDirectionForField(field) {
2525
field
2626
};
2727
}
28+
29+
export function setSearchTerm(searchTerm) {
30+
return {
31+
type: ActionTypes.SET_SEARCH_TERM,
32+
query: searchTerm
33+
};
34+
}
35+
36+
export function resetSearchTerm() {
37+
return setSearchTerm('');
38+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import PropTypes from 'prop-types';
2+
import React from 'react';
3+
import InlineSVG from 'react-inlinesvg';
4+
import { bindActionCreators } from 'redux';
5+
import { connect } from 'react-redux';
6+
import { throttle } from 'lodash';
7+
import * as SortingActions from '../actions/sorting';
8+
9+
const searchIcon = require('../../../images/magnifyingglass.svg');
10+
11+
class Searchbar extends React.Component {
12+
constructor(props) {
13+
super(props);
14+
this.state = {
15+
searchValue: this.props.searchTerm
16+
};
17+
this.throttledSearchChange = throttle(this.searchChange, 500);
18+
}
19+
20+
componentWillUnmount() {
21+
this.props.resetSearchTerm();
22+
}
23+
24+
handleResetSearch = () => {
25+
this.setState({ searchValue: '' }, () => {
26+
this.props.resetSearchTerm();
27+
});
28+
}
29+
30+
handleSearchEnter = (e) => {
31+
if (e.key === 'Enter') {
32+
this.props.setSearchTerm(this.state.searchValue);
33+
}
34+
}
35+
36+
searchChange = (value) => {
37+
this.props.setSearchTerm(this.state.searchValue);
38+
};
39+
40+
handleSearchChange = (e) => {
41+
this.setState({ searchValue: e.target.value }, () => {
42+
this.throttledSearchChange(this.state.searchValue);
43+
});
44+
}
45+
46+
render() {
47+
const { searchValue } = this.state;
48+
return (
49+
<div className="searchbar">
50+
<button
51+
type="submit"
52+
className="searchbar__button"
53+
onClick={this.handleSearchEnter}
54+
>
55+
<InlineSVG className="searchbar__icon" src={searchIcon} />
56+
</button>
57+
<input
58+
className="searchbar__input"
59+
type="text"
60+
value={searchValue}
61+
placeholder="Search files..."
62+
onChange={this.handleSearchChange}
63+
onKeyUp={this.handleSearchEnter}
64+
/>
65+
<button
66+
className="searchbar__clear-button"
67+
onClick={this.handleResetSearch}
68+
>clear
69+
</button>
70+
</div>
71+
);
72+
}
73+
}
74+
75+
Searchbar.propTypes = {
76+
searchTerm: PropTypes.string.isRequired,
77+
setSearchTerm: PropTypes.func.isRequired,
78+
resetSearchTerm: PropTypes.func.isRequired
79+
};
80+
81+
function mapStateToProps(state) {
82+
return {
83+
searchTerm: state.search.searchTerm
84+
};
85+
}
86+
87+
function mapDispatchToProps(dispatch) {
88+
return bindActionCreators(Object.assign({}, SortingActions), dispatch);
89+
}
90+
91+
export default connect(mapStateToProps, mapDispatchToProps)(Searchbar);

client/modules/IDE/components/SketchList.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class SketchListRowBase extends React.Component {
3030
isFocused: false
3131
};
3232
}
33-
3433
onFocusComponent = () => {
3534
this.setState({ isFocused: true });
3635
}

client/modules/IDE/pages/IDEView.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import * as ConsoleActions from '../actions/console';
3030
import { getHTMLFile } from '../reducers/files';
3131
import Overlay from '../../App/components/Overlay';
3232
import SketchList from '../components/SketchList';
33+
import Searchbar from '../components/Searchbar';
3334
import AssetList from '../components/AssetList';
3435
import About from '../components/About';
3536
import Feedback from '../components/Feedback';
@@ -371,6 +372,7 @@ class IDEView extends React.Component {
371372
title="Open a Sketch"
372373
previousPath={this.props.ide.previousPath}
373374
>
375+
<Searchbar />
374376
<SketchList
375377
username={this.props.params.username}
376378
user={this.props.user}

client/modules/IDE/reducers/ide.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const initialState = {
1313
shareModalProjectId: null,
1414
shareModalProjectName: null,
1515
shareModalProjectUsername: null,
16+
sketchlistModalVisible: false,
1617
editorOptionsVisible: false,
1718
keyboardShortcutVisible: false,
1819
unsavedChanges: false,

client/modules/IDE/reducers/search.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as ActionTypes from '../../../constants';
2+
3+
const initialState = {
4+
searchTerm: ''
5+
};
6+
7+
export default (state = initialState, action) => {
8+
switch (action.type) {
9+
case ActionTypes.SET_SEARCH_TERM:
10+
return { ...state, searchTerm: action.query };
11+
default:
12+
return state;
13+
}
14+
};

client/modules/IDE/selectors/projects.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,27 @@ import { DIRECTION } from '../actions/sorting';
66
const getSketches = state => state.sketches;
77
const getField = state => state.sorting.field;
88
const getDirection = state => state.sorting.direction;
9+
const getSearchTerm = state => state.search.searchTerm;
910

10-
const getSortedSketches = createSelector(
11+
const getFilteredSketches = createSelector(
1112
getSketches,
13+
getSearchTerm,
14+
(sketches, search) => {
15+
if (search) {
16+
const searchStrings = sketches.map((sketch) => {
17+
const smallSketch = {
18+
name: sketch.name
19+
};
20+
return { ...sketch, searchString: Object.values(smallSketch).join(' ').toLowerCase() };
21+
});
22+
return searchStrings.filter(sketch => sketch.searchString.includes(search.toLowerCase()));
23+
}
24+
return sketches;
25+
}
26+
);
27+
28+
const getSortedSketches = createSelector(
29+
getFilteredSketches,
1230
getField,
1331
getDirection,
1432
(sketches, field, direction) => {

client/reducers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import sketches from './modules/IDE/reducers/projects';
1010
import toast from './modules/IDE/reducers/toast';
1111
import console from './modules/IDE/reducers/console';
1212
import assets from './modules/IDE/reducers/assets';
13+
import search from './modules/IDE/reducers/search';
1314
import sorting from './modules/IDE/reducers/sorting';
1415
import loading from './modules/IDE/reducers/loading';
1516

@@ -21,6 +22,7 @@ const rootReducer = combineReducers({
2122
user,
2223
project,
2324
sketches,
25+
search,
2426
sorting,
2527
editorAccessibility,
2628
toast,

0 commit comments

Comments
 (0)