Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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
You can’t perform that action at this time.