JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdn.jsdelivr.net/npm/ractive"></script>
<main></main>
<script id='template' type='text/ractive'>
{{#each colors:i}}
<label>
<input type='checkbox' on-click="['checkedColor',@index]" {{#if isChecked(@index)}}checked{{/if}}/> {{this}}
</label>
{{/each}}
<h2>Selected:</h2>
{{#each selectedColors}}
<p style='color: {{this}}'>{{this}}</p>
{{/each}}
</script>
CSS
body {
font-family: 'Helvetica Neue', arial, sans-serif;
font-weight: 200;
color: #353535;
}
h1, h2, h3, h4, h5, h6 {
font-weight: 200;
}
label {
display: block;
}
JavaScript
var ractive = new Ractive({
el: 'main',
template: '#template',
data: {
colors: [ 'red', 'yellow', 'green', 'blue' ],
selectedColors:[],
isChecked:function(inx){
var color = this.get("colors")[parseInt(inx)]
var ret = this.get("selectedColors").indexOf(color)
if(ret>=0)
return true
return false
}
},
on: {
checkedColor:function(context, msg){
var color = this.get("colors")[parseInt(msg)]
if(!this.get("selectedColors"))
this.set("selectedColors",[])
var pos = this.get("selectedColors").indexOf(color)
if(pos>=0){
this.splice("selectedColors",pos,1)
}else{
this.push("selectedColors",color)
}
}
}
});