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

83 lines
2.5 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<div :class="cx('root')" v-bind="getPTOptions('root')" :data-p-highlight="active" :data-p-disabled="disabled">
<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';
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-02-11 23:48:46 +00:00
inheritAttrs: false,
emits: ['update:modelValue', 'change', 'focus', 'blur'],
2022-09-06 12:03:37 +00:00
methods: {
getPTOptions(key) {
2024-02-11 23:48:46 +00:00
const _ptm = key === 'root' ? this.ptmi : this.ptm;
return _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: {
active() {
return this.modelValue === true;
},
2022-09-06 12:03:37 +00:00
hasLabel() {
return ObjectUtils.isNotEmpty(this.onLabel) && ObjectUtils.isNotEmpty(this.offLabel);
2022-09-06 12:03:37 +00:00
},
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>