UNPKG

primevue

Version:

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![npm version](https://badge.fury.io/js/primevue.svg)](https://badge.fury.io/js/primevue) [![Discord Chat](https://img.shields.io/discord/55794023

2 lines (1 loc) 4.89 kB
import e from"primevue/ripple";import t from"primevue/basecomponent";import{resolveDirective as n,withDirectives as i,openBlock as o,createElementBlock as l,mergeProps as r,createElementVNode as u,renderSlot as c,normalizeClass as a,createCommentVNode as s,toDisplayString as p}from"vue";var f={name:"ToggleButton",extends:{name:"BaseToggleButton",extends:t,props:{modelValue:Boolean,onIcon:String,offIcon:String,onLabel:{type:String,default:"Yes"},offLabel:{type:String,default:"No"},iconPos:{type:String,default:"left"},disabled:{type:Boolean,default:!1},tabindex:{type:Number,default:null},inputId:{type:String,default:null},inputClass:{type:[String,Object],default:null},inputStyle:{type:Object,default:null},inputProps:{type:null,default:null},"aria-labelledby":{type:String,default:null},"aria-label":{type:String,default:null}},css:{classes:{root:function(e){var t=e.instance,n=e.props;return["p-button p-togglebutton p-component",{"p-focus":t.focused,"p-button-icon-only":t.hasIcon&&!t.hasLabel,"p-disabled":n.disabled,"p-highlight":!0===n.modelValue}]},icon:function(e){var t=e.instance,n=e.props;return["p-button-icon",{"p-button-icon-left":"left"===n.iconPos&&t.label,"p-button-icon-right":"right"===n.iconPos&&t.label}]},label:"p-button-label"}},provide:function(){return{$parentInstance:this}}},emits:["update:modelValue","change","click","focus","blur"],outsideClickListener:null,data:function(){return{focused:!1}},mounted:function(){this.bindOutsideClickListener()},beforeUnmount:function(){this.unbindOutsideClickListener()},methods:{onClick:function(e){this.disabled||(this.$emit("update:modelValue",!this.modelValue),this.$emit("change",e),this.$emit("click",e),this.focused=!0)},onFocus:function(e){this.focused=!0,this.$emit("focus",e)},onBlur:function(e){this.focused=!1,this.$emit("blur",e)},bindOutsideClickListener:function(){var e=this;this.outsideClickListener||(this.outsideClickListener=function(t){e.focused&&!e.$refs.container.contains(t.target)&&(e.focused=!1)},document.addEventListener("click",this.outsideClickListener))},unbindOutsideClickListener:function(){this.outsideClickListener&&(document.removeEventListener("click",this.outsideClickListener),this.outsideClickListener=null)}},computed:{hasLabel:function(){return this.onLabel&&this.onLabel.length>0&&this.offLabel&&this.offLabel.length>0},hasIcon:function(){return this.$slots.icon||this.onIcon&&this.offIcon},label:function(){return this.hasLabel?this.modelValue?this.onLabel:this.offLabel:"&nbsp;"}},directives:{ripple:e}};function d(e){return d="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},d(e)}function b(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);t&&(i=i.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,i)}return n}function m(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?b(Object(n),!0).forEach((function(t){h(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):b(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function h(e,t,n){return(t=y(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function y(e){var t=g(e,"string");return"symbol"===d(t)?t:String(t)}function g(e,t){if("object"!==d(e)||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var i=n.call(e,t||"default");if("object"!==d(i))return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}var v=["data-p-active"],L=["id","checked","value","aria-labelledby","aria-label"];f.render=function(e,t,f,d,b,h){var y=n("ripple");return i((o(),l("div",r({ref:"container",class:e.cx("root"),onClick:t[2]||(t[2]=function(e){return h.onClick(e)})},e.ptm("root"),{"data-p-active":!0===e.modelValue,"data-pc-name":"togglebutton"}),[u("span",r({class:"p-hidden-accessible"},e.ptm("hiddenInputWrapper"),{"data-p-hidden-accessible":!0}),[u("input",r({id:e.inputId,type:"checkbox",role:"switch",class:e.inputClass,style:e.inputStyle,checked:e.modelValue,value:e.modelValue,"aria-labelledby":e.ariaLabelledby,"aria-label":e.ariaLabel,onFocus:t[0]||(t[0]=function(e){return h.onFocus(e)}),onBlur:t[1]||(t[1]=function(e){return h.onBlur(e)})},m(m({},e.inputProps),e.ptm("hiddenInput"))),null,16,L)],16),c(e.$slots,"icon",{value:e.modelValue,class:a(e.cx("icon"))},(function(){return[e.onIcon||e.offIcon?(o(),l("span",r({key:0,class:[e.cx("icon"),e.modelValue?e.onIcon:e.offIcon]},e.ptm("icon")),null,16)):s("",!0)]})),u("span",r({class:e.cx("label")},e.ptm("label")),p(h.label),17)],16,v)),[[y]])};export{f as default};