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

106 lines
3.4 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-09-04 21:49:02 +00:00
<div ref="container" v-ripple :class="cx('root')" @click="onClick($event)" v-bind="ptm('root', getPTOptions)" :data-p-active="modelValue === true" data-pc-name="togglebutton">
<span class="p-hidden-accessible" v-bind="ptm('hiddenInputWrapper')" :data-p-hidden-accessible="true">
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-05-24 14:26:41 +00:00
<slot name="icon" :value="modelValue" :class="cx('icon')">
2023-09-04 21:49:02 +00:00
<span v-if="onIcon || offIcon" :class="[cx('icon'), modelValue ? onIcon : offIcon]" v-bind="ptm('icon', getPTOptions)" />
</slot>
2023-09-04 21:49:02 +00:00
<span :class="cx('label')" v-bind="ptm('label', getPTOptions)">{{ label }}</span>
2022-09-06 12:03:37 +00:00
</div>
</template>
<script>
import Ripple from 'primevue/ripple';
2023-05-24 14:26:41 +00:00
import BaseToggleButton from './BaseToggleButton.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'ToggleButton',
2023-05-24 14:26:41 +00:00
extends: BaseToggleButton,
2022-09-06 12:03:37 +00:00
emits: ['update:modelValue', 'change', 'click', 'focus', 'blur'],
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: {
hasLabel() {
return this.onLabel && this.onLabel.length > 0 && this.offLabel && this.offLabel.length > 0;
},
hasIcon() {
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) : '&nbsp;';
2023-08-02 14:32:32 +00:00
},
2023-09-04 21:49:02 +00:00
getPTOptions() {
2023-08-02 14:32:32 +00:00
return {
context: {
focused: this.focused,
disabled: this.disabled,
highlighted: this.modelValue === true
}
};
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>