JSFiddle - React, Tailwind, and code Playground
by mqchen
HTML
<div class="buttonbar" data-buttonvalue-target="foo">
<button class="icon" data-icon="J" data-analysisreact-role="thumbsup" value="0">Ingen funn</button>
<button class="icon" data-icon="j" data-analysisreact-role="thumbsdown" value="1">Bekymrende</button>
<button class="icon" data-icon="/" data-analysisreact-role="irrelevant" value="2">Irrelevant</button>
</div>
<input type="text" name="foo" value="" />
CSS
body {
padding: 40px;
font-family: Arial, sans-serif;
}
/** Icons */
@font-face {
font-family: 'Entypo';
src: url('https://dl.dropbox.com/s/uydr0f8lcx81brq/entypo-webfont.woff?dl=1') format('woff');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'IcoMoon';
src: url('https://dl.dropbox.com/s/gfwrdxu3asuhiel/IcoMoon.woff?dl=1') format('woff');
font-weight: normal;
font-style: normal;
}
.icon-b, .icon-a, .icon {
position: relative;
}
.icon-b:before, .icon-a:after, .icon:after {
text-indent: 0;
text-transform: none;
font-weight: normal;
content: attr(data-icon);
font-family: 'IcoMoon';
line-height: inherit;
position: absolute;
display: block;
top: 50%;
margin: -20% 0 0 0;
}
.icon-b {
padding-left: 28px;
}
.icon-b:before {
left: 10px;
}
.icon-a {
padding-right: 28px;
}
.icon-a:after {
right: 10px;
}
.icon {
text-indent: -999px;
overflow: hidden;
}
.icon:after {
left: 10px;
}
/** Entypo */
.icon-entypo.icon-b:before, .icon-entypo.icon-a:after, .icon-entypo.icon:after {
font-family: 'Entypo';
margin: -45% 0 0 0;
}
button.icon-entypo.icon-b:before, button.icon-entypo.icon-a:after, button.icon-entypo.icon:after {
font-size: 2em;
}
/** IcoMoon */
.icon-icomoon.icon-b:before, .icon-icomoon.icon-a:after, .icon-icomoon.icon:after {
font-family: 'IcoMoon';
margin: -20% 0 0 0;
}
/** Button bar */
.buttonbar {
float: left;
margin: 0 10px 20px 10px;
}
.buttonbar button {
border-radius: 0;
position: relative;
margin-left: -1px;
white-space: nowrap;
}
.buttonbar button:first-child,
.buttonbar button.visiblebutton {
-webkit-border-top-left-radius: 3px;
-webkit-border-bottom-left-radius: 3px;
border-top-left-radius: 3px;
border-bottom-left-radius: 3px;
}
.buttonbar button:last-child,
.buttonbar button.visiblebutton {
-webkit-border-top-right-radius: 3px;
...
JavaScript
if(Udir === undefined) {
var Udir = {};
}
Udir.buttonValueSetter = function(b, o) {
var options = {
"eventNS" : ".buttonvalue",
"targetAttr" : "data-buttonvalue-target",
"valueAttr" : "value",
"buttonSelector" : "button",
"activeClass" : "activebutton"
};
var buttonbar = null;
var buttons = [];
var target = null;
var methods = {
init : function(bb, opt) {
$.extend(true, options, opt);
var buttonbar = $(bb);
// Get target
var targetStr = buttonbar.attr(options.targetAttr);
target = $("[name='" + targetStr + "']");
// Get buttons
buttons = $(options.buttonSelector, buttonbar);
// Add events to buttons
methods._addEvents();
// Set initial value
methods._processTargetValue();
},
_addEvents : function() {
buttons.each(function(index, button) {
var $button = $(button);
$button.bind("click" + options.eventNS, function(e) {
e.preventDefault();
var buttonValue = $button.attr(options.valueAttr);
methods.setValue(buttonValue);
});
});
// Listen to changes in target
target.bind("change" + options.eventNS, methods._processTargetValue);
target.bind("keyup" + options.eventNS, methods._processTargetValue);
},
_processTargetValue : function() {
var initValue = target.val();
methods.setValue(initValue);
},
setValue : function(value) {
// Get button which matches this value
buttons.each(function(index, button) {
var $button = $(button);
...