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

97 lines
3.1 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-05-24 14:26:41 +00:00
<div ref="container" v-ripple :class="cx('root')" @click="onClick($event)" v-bind="ptm('root')" :data-p-active="modelValue === true">
<span :class="cx('hiddenInputWrapper')" :style="sx('hiddenAccessible', isUnstyled)" 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')">
<span v-if="onIcon || offIcon" :class="cx('icon')" v-bind="ptm('icon')" />
</slot>
2023-05-24 14:26:41 +00:00
<span :class="cx('label')" v-bind="ptm('label')">{{ 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;';
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>