From 05976128b117ed03961c588da83c292ec8ddecfd Mon Sep 17 00:00:00 2001 From: f0x Date: Wed, 23 Jan 2019 16:07:20 +0100 Subject: [PATCH] Make FilterList reusable --- components/filterList.js | 62 ++++++++++++++++++++++++++++++++++++++ components/sidebar.js | 65 ++++------------------------------------ 2 files changed, 68 insertions(+), 59 deletions(-) create mode 100644 components/filterList.js diff --git a/components/filterList.js b/components/filterList.js new file mode 100644 index 0000000..3aa2e05 --- /dev/null +++ b/components/filterList.js @@ -0,0 +1,62 @@ +'use strict' +const React = require('react') +const ReactDOM = require('react-dom') +const create = require('create-react-class') +const Promise = require('bluebird') +const debounce = require('debounce') + +let FilterList = create({ + displayName: "FilterList", + + getInitialState: function() { + return { + selection: -1, + filter: "" + } + }, + + select: function(id) { + this.setState({selection: id}) + this.props.callback(id) + }, + + inputRef: function(ref) { + if (ref == null) { + return + } + this.setState({ + inputRef: ref + }) + ref.addEventListener("keyup", debounce(this.input, 20)) + }, + + input: function(e) { + this.setState({ + filter: e.target.value.toUpperCase() + }) + }, + + render: function() { + let items = this.props.items.map((item, id) => { + let props = { + active: this.state.selection == id, + filter: this.state.filter, + content: item, + key: id, + listId: id, + select: this.select + } + return React.createElement(this.props.element, props) + }) + return <> +
+ +
+
+ {items} +
+ + } +}) + +module.exports = FilterList diff --git a/components/sidebar.js b/components/sidebar.js index 8a72423..3dec2db 100644 --- a/components/sidebar.js +++ b/components/sidebar.js @@ -5,67 +5,14 @@ const create = require('create-react-class') const Promise = require('bluebird') const debounce = require('debounce') -let Filter = create({ - displayName: "Filter", - - inputRef: function(ref) { - if (ref == null) { - return - } - this.setState({ - inputRef: ref - }) - ref.addEventListener("keyup", debounce(this.input, 20)) - }, - - input: function(e) { - this.props.setFilter(e.target.value.toUpperCase()) - }, - - render: function() { - return
- -
- } -}) - -let List = create({ - displayName: "List", - - getInitialState: function() { - return { - roomId: 0 - } - }, - - select: function(id) { - this.setState({roomId: id}) - }, - - render: function() { - let rooms = ["Neo", "version 4", "Codename", "Iris", "Let's All Love Lain", "Very long room name abcdefghijklmnopqrstuvwxyz"] - let roomList = rooms.map((room, i) => { - return - }) - return
- {roomList} -
- } -}) +const FilterList = require('./filterList.js') let RoomListItem = create({ displayName: "RoomListItem", getInitialState: function() { return { - filterName: this.props.name.toUpperCase(), + filterName: this.props.content.toUpperCase(), unread: Math.random() > 0.7 } }, @@ -90,8 +37,8 @@ let RoomListItem = create({ className += " unread" } return
- - {this.props.name} + + {this.props.content}
} }) @@ -113,9 +60,9 @@ let Sidebar = create({ }, render: function() { + let rooms = ["Neo", "version 4", "Codename", "Iris", "Let's All Love Lain", "Very long room name abcdefghijklmnopqrstuvwxyz"] return
- - +
} })