Skip to content

Instantly share code, notes, and snippets.

@WreckedAvent
Last active July 29, 2016 15:15
Show Gist options
  • Select an option

  • Save WreckedAvent/fdcd1761844b656d5b350fad5d4ba79f to your computer and use it in GitHub Desktop.

Select an option

Save WreckedAvent/fdcd1761844b656d5b350fad5d4ba79f to your computer and use it in GitHub Desktop.
import * as m from 'mithril'
import * as isSelected from '../functions/isSelected'
const action = (option, selection, e) => {
if (isSelected(ctrl.option, ctrl.se lection)) {
var i = ctrl.selection.indexOf(ctrl.option)
ctrl.selection.splice(i, 1)
} else {
if (Array.isArray(ctrl.selection)) {
ctrl.selection.push(ctrl.option)
} else {
ctrl.selection(ctrl.option)
}
}
e.stopPropagation()
m.redraw()
}
const setClasses = (option, selection, highlighted, index) => {
var selected = isSelected(option, selection);
if (selected && highlighted() === index){
return "selected highlighted";
} else if (selected && highlighted() !== index){
return "selected";
} else if (!selected && highlighted() === index){
return 'highlighted';
} else {
return '';
}
}
export const view = (_, {
option,
selection,
highlighted,
index,
search
}) => m('.item[tabindex=0]', {
class: setClasses(option, selection, highlighted, index),
style: {
display: search() && option.toLowerCase().indexOf(search().toLowerCase()) === -1 ? 'none' : 'block'
},
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