primevue-mirror/components/lib/togglebutton/ToggleButton.vue

163 lines
4.5 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2022-12-08 11:04:25 +00:00
<div ref="container" v-ripple :class="buttonClass" @click="onClick($event)">
2022-09-06 12:03:37 +00:00
<span class="p-hidden-accessible">
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)"
v-bind="inputProps"
/>
2022-09-06 12:03:37 +00:00
</span>
<span v-if="hasIcon" :class="iconClass"></span>
2022-09-14 11:26:01 +00:00
<span class="p-button-label">{{ label }}</span>
2022-09-06 12:03:37 +00:00
</div>
</template>
<script>
import Ripple from 'primevue/ripple';
export default {
name: 'ToggleButton',
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: {
type: [String, Object],
2022-09-14 11:26:01 +00:00
default: null
},
inputStyle: {
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() {
return this.onIcon && this.onIcon.length > 0 && this.offIcon && this.offIcon.length > 0;
},
label() {
2022-09-14 11:26:01 +00:00
return this.hasLabel ? (this.modelValue ? this.onLabel : this.offLabel) : '&nbsp;';
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>