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

54 lines
1.7 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2024-03-25 21:11:32 +00:00
<button v-ripple type="button" :class="cx('root')" :tabindex="tabindex" :disabled="disabled" :aria-pressed="modelValue" @click="onChange" v-bind="getPTOptions('root')" :data-p-highlight="active" :data-p-disabled="disabled">
<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>
</button>
2022-09-06 12:03:37 +00:00
</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,
2024-03-21 13:21:57 +00:00
emits: ['update:modelValue', 'change'],
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);
}
}
},
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
},
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>