Skip to content

Instantly share code, notes, and snippets.

@WreckedAvent
Last active July 29, 2016 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save WreckedAvent/fdcd1761844b656d5b350fad5d4ba79f to your computer and use it in GitHub Desktop.
Save WreckedAvent/fdcd1761844b656d5b350fad5d4ba79f to your computer and use it in GitHub Desktop.
// 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