Last active
July 29, 2016 15:15
-
-
Save WreckedAvent/fdcd1761844b656d5b350fad5d4ba79f to your computer and use it in GitHub Desktop.
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
// based on https://github.com/dontwork/mithril-select/blob/master/src/components/selectOption.js | |
import * as m from 'mithril' | |
import * as isSelected from '../functions/isSelected' | |
const action = (option, selection, e) => { | |
if (isSelected(option, selection)) { | |
const i = selection.indexOf(option) | |
selection.splice(i, 1) | |
} else { | |
Array.isArray(selection) | |
? selection.push(option) | |
: selection(option) | |
} | |
e.stopPropagation() | |
m.redraw() | |
} | |
const selectClass = (option, selection) => | |
isSelected(option, selection) ? 'selected' : '' | |
const highlightClass = (highlighted, index) => | |
highlighted() === index ? 'highlighted' : '' | |
const displayAttr = (search, option) => | |
search() && option.toLowerCase().indexOf(search().toLowerCase()) === -1 ? 'none' : 'block' | |
export const view = ({}, { | |
option, | |
selection, | |
highlighted, | |
index, | |
search | |
}) => m('.item[tabindex=0]', { | |
class: `${selectClass(option, selection)} ${highlightClass(highlighted, index)}`, | |
style: { | |
display: displayAttr(search, option) | |
}, | |
onclick: e => action(option, selection, e), | |
onfocus: () => highlighted(index) | |
onmouseover: () => highlighted(index) | |
}, option) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment