primevue-mirror/components/togglebutton/ToggleButton.vue

128 lines
3.4 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2022-09-14 11:26:01 +00:00
<div v-ripple :class="buttonClass" @click="onClick($event)">
2022-09-06 12:03:37 +00:00
<span class="p-hidden-accessible">
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)"
v-bind="inputProps"
/>
2022-09-06 12:03:37 +00:00
</span>
<span v-if="hasIcon" :class="iconClass"></span>
2022-09-14 11:26:01 +00:00
<span class="p-button-label">{{ label }}</span>
2022-09-06 12:03:37 +00:00
</div>
</template>
<script>
import Ripple from 'primevue/ripple';
export default {
name: 'ToggleButton',
emits: ['update:modelValue', 'change', 'click', 'focus', 'blur'],
props: {
modelValue: Boolean,
2022-09-14 11:26:01 +00:00
onIcon: String,
offIcon: String,
2022-09-06 12:03:37 +00:00
onLabel: {
type: String,
default: 'Yes'
},
offLabel: {
type: String,
default: 'No'
},
iconPos: {
type: String,
default: 'left'
},
disabled: {
type: Boolean,
default: false
},
tabindex: {
type: Number,
default: null
},
2022-09-14 11:26:01 +00:00
inputId: {
type: String,
default: null
},
inputClass: {
type: String,
default: null
},
inputStyle: {
type: null,
default: null
},
inputProps: {
type: null,
default: null
},
2022-09-06 12:03:37 +00:00
'aria-labelledby': {
type: String,
2022-09-14 11:26:01 +00:00
default: null
2022-09-06 12:03:37 +00:00
},
'aria-label': {
type: String,
default: null
}
},
methods: {
onClick(event) {
if (!this.disabled) {
this.$emit('update:modelValue', !this.modelValue);
this.$emit('change', event);
this.$emit('click', event);
}
},
onFocus(event) {
this.$emit('focus', event);
},
onBlur(event) {
this.$emit('blur', event);
}
},
computed: {
buttonClass() {
return {
'p-button p-togglebutton p-component': true,
'p-button-icon-only': this.hasIcon && !this.hasLabel,
'p-disabled': this.disabled,
'p-highlight': this.modelValue === true
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
iconClass() {
return [
2022-09-14 11:26:01 +00:00
this.modelValue ? this.onIcon : this.offIcon,
2022-09-06 12:03:37 +00:00
'p-button-icon',
{
'p-button-icon-left': this.iconPos === 'left' && this.label,
'p-button-icon-right': this.iconPos === 'right' && this.label
}
2022-09-14 11:26:01 +00:00
];
2022-09-06 12:03:37 +00:00
},
hasLabel() {
return this.onLabel && this.onLabel.length > 0 && this.offLabel && this.offLabel.length > 0;
},
hasIcon() {
return this.onIcon && this.onIcon.length > 0 && this.offIcon && this.offIcon.length > 0;
},
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>