-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #356 from hammerlab/options-menu
Add an options menu
- Loading branch information
Showing
11 changed files
with
226 additions
and
3 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* A generic menu, intended to be used for both toggling options and invoking commands. | ||
* | ||
* Usage: | ||
* | ||
* var items = [ | ||
* {key: 'a', label: 'Item A', checked: true}, | ||
* {key: 'b', label: 'Item B'}, | ||
* '-', | ||
* {key: 'random', label: 'Pick Random'} | ||
* ]; | ||
* <Menu header="Header" items={items} onSelect={selectHandler} /> | ||
* | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var React = require('react'); | ||
|
||
type MenuItem = { | ||
key: string; | ||
label: string; | ||
checked?: boolean; | ||
}; | ||
|
||
type Props = { | ||
header: string; | ||
items: Array<MenuItem|'-'>; | ||
onSelect: (key: string) => void; | ||
}; | ||
|
||
class Menu extends React.Component<{}, Props, void> { | ||
props: Props; | ||
|
||
clickHandler(idx: number, e: SyntheticMouseEvent) { | ||
e.preventDefault(); | ||
var item = this.props.items[idx]; | ||
if (typeof(item) == 'string') return; // for flow | ||
this.props.onSelect(item.key); | ||
} | ||
|
||
render(): any { | ||
var makeHandler = i => this.clickHandler.bind(this, i); | ||
var els = []; | ||
if (this.props.header) { | ||
els.push(<div key='header' className='menu-header'>{this.props.header}</div>); | ||
} | ||
els = els.concat(this.props.items.map((item, i) => { | ||
if (typeof(item) === 'string') { // would prefer "== '-'", see flow#1066 | ||
return <div key={i} className='menu-separator' />; | ||
} else { | ||
var checkClass = 'check' + (item.checked ? ' checked' : ''); | ||
return ( | ||
<div key={i} className='menu-item' onClick={makeHandler(i)}> | ||
<span className={checkClass}></span> | ||
<span className='menu-item-label'>{item.label}</span> | ||
</div> | ||
); | ||
} | ||
})); | ||
|
||
return ( | ||
<div className='menu'> | ||
{els} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
module.exports = Menu; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters