2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
2024-01-30 14:35:32 +00:00
|
|
|
<div :class="cx('root')" v-bind="getPTOptions('root')" :data-p-highlight="active" :data-p-disabled="disabled">
|
2024-01-12 07:07:55 +00:00
|
|
|
<input
|
|
|
|
:id="inputId"
|
|
|
|
type="checkbox"
|
|
|
|
role="switch"
|
|
|
|
:class="[cx('input'), inputClass]"
|
|
|
|
:style="inputStyle"
|
|
|
|
:value="modelValue"
|
|
|
|
:checked="active"
|
|
|
|
:tabindex="tabindex"
|
|
|
|
:disabled="disabled"
|
|
|
|
:readonly="readonly"
|
|
|
|
:aria-labelledby="ariaLabelledby"
|
|
|
|
:aria-label="ariaLabel"
|
|
|
|
@focus="onFocus"
|
|
|
|
@blur="onBlur"
|
|
|
|
@change="onChange"
|
|
|
|
v-bind="getPTOptions('input')"
|
|
|
|
/>
|
2024-01-14 15:21:43 +00:00
|
|
|
<div v-ripple :class="cx('box')" v-bind="getPTOptions('box')">
|
|
|
|
<slot name="icon" :value="modelValue" :class="cx('icon')">
|
|
|
|
<span v-if="onIcon || offIcon" :class="[cx('icon'), modelValue ? onIcon : offIcon]" v-bind="getPTOptions('icon')" />
|
|
|
|
</slot>
|
|
|
|
<span :class="cx('label')" v-bind="getPTOptions('label')">{{ label }}</span>
|
|
|
|
</div>
|
2022-09-06 12:03:37 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import Ripple from 'primevue/ripple';
|
2024-01-12 07:07:55 +00:00
|
|
|
import { ObjectUtils } from 'primevue/utils';
|
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,
|
2024-01-12 07:07:55 +00:00
|
|
|
emits: ['update:modelValue', 'change', 'focus', 'blur'],
|
2022-09-06 12:03:37 +00:00
|
|
|
methods: {
|
2024-01-12 07:07:55 +00:00
|
|
|
getPTOptions(key) {
|
|
|
|
return this.ptm(key, {
|
|
|
|
context: {
|
|
|
|
active: this.active,
|
|
|
|
disabled: this.disabled
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onChange(event) {
|
|
|
|
if (!this.disabled && !this.readonly) {
|
2022-09-06 12:03:37 +00:00
|
|
|
this.$emit('update:modelValue', !this.modelValue);
|
|
|
|
this.$emit('change', event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onFocus(event) {
|
|
|
|
this.$emit('focus', event);
|
|
|
|
},
|
|
|
|
onBlur(event) {
|
|
|
|
this.$emit('blur', event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2024-01-12 07:07:55 +00:00
|
|
|
active() {
|
|
|
|
return this.modelValue === true;
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
hasLabel() {
|
2024-01-12 07:07:55 +00:00
|
|
|
return ObjectUtils.isNotEmpty(this.onLabel) && ObjectUtils.isNotEmpty(this.offLabel);
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
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>
|