mdb-ui-kit/js/src/radio.js

74 lines
1.8 KiB
JavaScript

//import Util from './util'
// Radio decorator, to be called after Input
const Radio = (($) => {
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
const NAME = 'radio'
const DATA_KEY = `mdb.${NAME}`
const JQUERY_NO_CONFLICT = $.fn[NAME]
const Default = {
template: `<span class='circle'></span><span class='check'></span>`
}
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
class Radio {
constructor(element, config) {
this.$element = $(element)
this.config = $.extend({}, Default, config)
this.$element.after(this.config.template)
}
dispose() {
$.removeData(this.$element, DATA_KEY)
this.$element = null
this.config = null
}
// ------------------------------------------------------------------------
// private
// ------------------------------------------------------------------------
// static
static _jQueryInterface(config) {
return this.each(function () {
let $element = $(this)
let data = $element.data(DATA_KEY)
if (!data) {
data = new Radio(this, config)
$element.data(DATA_KEY, data)
}
})
}
}
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
$.fn[NAME] = Radio._jQueryInterface
$.fn[NAME].Constructor = Radio
$.fn[NAME].noConflict = () => {
$.fn[NAME] = JQUERY_NO_CONFLICT
return Radio._jQueryInterface
}
return Radio
})(jQuery)
export default Radio