2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
2023-05-07 11:38:47 +00:00
|
|
|
<div ref="container" v-ripple :class="buttonClass" @click="onClick($event)" v-bind="ptm('root')">
|
2023-05-09 08:57:03 +00:00
|
|
|
<span class="p-hidden-accessible" v-bind="ptm('hiddenInputWrapper')">
|
2022-09-14 11:26:01 +00:00
|
|
|
<input
|
|
|
|
:id="inputId"
|
|
|
|
type="checkbox"
|
|
|
|
role="switch"
|
|
|
|
:class="inputClass"
|
|
|
|
:style="inputStyle"
|
|
|
|
:checked="modelValue"
|
|
|
|
:value="modelValue"
|
|
|
|
:aria-labelledby="ariaLabelledby"
|
|
|
|
:aria-label="ariaLabel"
|
|
|
|
@focus="onFocus($event)"
|
|
|
|
@blur="onBlur($event)"
|
2023-05-09 08:57:03 +00:00
|
|
|
v-bind="{ ...inputProps, ...ptm('hiddenInput') }"
|
2022-09-14 11:26:01 +00:00
|
|
|
/>
|
2022-09-06 12:03:37 +00:00
|
|
|
</span>
|
2023-04-18 10:50:13 +00:00
|
|
|
<slot name="icon" :value="modelValue" :class="iconClass">
|
2023-05-07 11:38:47 +00:00
|
|
|
<span v-if="onIcon || offIcon" :class="iconClass" v-bind="ptm('icon')" />
|
2023-04-18 10:50:13 +00:00
|
|
|
</slot>
|
2023-05-07 11:38:47 +00:00
|
|
|
<span class="p-button-label" v-bind="ptm('label')">{{ label }}</span>
|
2022-09-06 12:03:37 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-05-07 11:38:47 +00:00
|
|
|
import BaseComponent from 'primevue/basecomponent';
|
2022-09-06 12:03:37 +00:00
|
|
|
import Ripple from 'primevue/ripple';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'ToggleButton',
|
2023-05-07 11:38:47 +00:00
|
|
|
extends: BaseComponent,
|
2022-09-06 12:03:37 +00:00
|
|
|
emits: ['update:modelValue', 'change', 'click', 'focus', 'blur'],
|
|
|
|
props: {
|
|
|
|
modelValue: Boolean,
|
2022-09-14 11:26:01 +00:00
|
|
|
onIcon: String,
|
|
|
|
offIcon: String,
|
2022-09-06 12:03:37 +00:00
|
|
|
onLabel: {
|
|
|
|
type: String,
|
|
|
|
default: 'Yes'
|
|
|
|
},
|
|
|
|
offLabel: {
|
|
|
|
type: String,
|
|
|
|
default: 'No'
|
|
|
|
},
|
|
|
|
iconPos: {
|
|
|
|
type: String,
|
|
|
|
default: 'left'
|
|
|
|
},
|
|
|
|
disabled: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
tabindex: {
|
|
|
|
type: Number,
|
|
|
|
default: null
|
|
|
|
},
|
2022-09-14 11:26:01 +00:00
|
|
|
inputId: {
|
|
|
|
type: String,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
inputClass: {
|
2023-03-09 07:02:25 +00:00
|
|
|
type: [String, Object],
|
2022-09-14 11:26:01 +00:00
|
|
|
default: null
|
|
|
|
},
|
|
|
|
inputStyle: {
|
2023-03-09 07:02:25 +00:00
|
|
|
type: Object,
|
2022-09-14 11:26:01 +00:00
|
|
|
default: null
|
|
|
|
},
|
|
|
|
inputProps: {
|
|
|
|
type: null,
|
|
|
|
default: null
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
'aria-labelledby': {
|
|
|
|
type: String,
|
2022-09-14 11:26:01 +00:00
|
|
|
default: null
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
'aria-label': {
|
|
|
|
type: String,
|
|
|
|
default: null
|
|
|
|
}
|
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
outsideClickListener: null,
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
focused: false
|
|
|
|
};
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.bindOutsideClickListener();
|
|
|
|
},
|
|
|
|
beforeUnmount() {
|
|
|
|
this.unbindOutsideClickListener();
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
methods: {
|
|
|
|
onClick(event) {
|
|
|
|
if (!this.disabled) {
|
|
|
|
this.$emit('update:modelValue', !this.modelValue);
|
|
|
|
this.$emit('change', event);
|
|
|
|
this.$emit('click', event);
|
2022-12-08 11:04:25 +00:00
|
|
|
this.focused = true;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
onFocus(event) {
|
2022-12-08 11:04:25 +00:00
|
|
|
this.focused = true;
|
2022-09-06 12:03:37 +00:00
|
|
|
this.$emit('focus', event);
|
|
|
|
},
|
|
|
|
onBlur(event) {
|
2022-12-08 11:04:25 +00:00
|
|
|
this.focused = false;
|
2022-09-06 12:03:37 +00:00
|
|
|
this.$emit('blur', event);
|
2022-12-08 11:04:25 +00:00
|
|
|
},
|
|
|
|
bindOutsideClickListener() {
|
|
|
|
if (!this.outsideClickListener) {
|
|
|
|
this.outsideClickListener = (event) => {
|
|
|
|
if (this.focused && !this.$refs.container.contains(event.target)) {
|
|
|
|
this.focused = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener('click', this.outsideClickListener);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unbindOutsideClickListener() {
|
|
|
|
if (this.outsideClickListener) {
|
|
|
|
document.removeEventListener('click', this.outsideClickListener);
|
|
|
|
this.outsideClickListener = null;
|
|
|
|
}
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
buttonClass() {
|
2022-12-08 11:04:25 +00:00
|
|
|
return [
|
|
|
|
'p-button p-togglebutton p-component',
|
|
|
|
{
|
|
|
|
'p-focus': this.focused,
|
|
|
|
'p-button-icon-only': this.hasIcon && !this.hasLabel,
|
|
|
|
'p-disabled': this.disabled,
|
|
|
|
'p-highlight': this.modelValue === true
|
|
|
|
}
|
|
|
|
];
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
iconClass() {
|
|
|
|
return [
|
2022-09-14 11:26:01 +00:00
|
|
|
this.modelValue ? this.onIcon : this.offIcon,
|
2022-09-06 12:03:37 +00:00
|
|
|
'p-button-icon',
|
|
|
|
{
|
|
|
|
'p-button-icon-left': this.iconPos === 'left' && this.label,
|
|
|
|
'p-button-icon-right': this.iconPos === 'right' && this.label
|
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
];
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
hasLabel() {
|
|
|
|
return this.onLabel && this.onLabel.length > 0 && this.offLabel && this.offLabel.length > 0;
|
|
|
|
},
|
|
|
|
hasIcon() {
|
2023-04-18 10:50:13 +00:00
|
|
|
return this.$slots.icon || (this.onIcon && this.offIcon);
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
label() {
|
2022-09-14 11:26:01 +00:00
|
|
|
return this.hasLabel ? (this.modelValue ? this.onLabel : this.offLabel) : ' ';
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
directives: {
|
2022-09-14 11:26:01 +00:00
|
|
|
ripple: Ripple
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|
|
|
|
</script>
|