primevue-mirror/components/lib/inputswitch/InputSwitch.vue

63 lines
1.8 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<div :class="cx('root')" :style="sx('root')" v-bind="getPTOptions('root')" data-pc-name="inputswitch" :data-p-highlight="checked" :data-p-disabled="disabled">
<input
:id="inputId"
type="checkbox"
role="switch"
:class="[cx('input'), inputClass]"
:style="inputStyle"
:checked="checked"
:tabindex="tabindex"
:disabled="disabled"
:readonly="readonly"
:aria-checked="checked"
:aria-labelledby="ariaLabelledby"
:aria-label="ariaLabel"
@focus="onFocus"
@blur="onBlur"
@change="onChange"
v-bind="getPTOptions('input')"
/>
<span :class="cx('slider')" v-bind="getPTOptions('slider')"></span>
2022-09-06 12:03:37 +00:00
</div>
</template>
<script>
2023-05-24 11:15:28 +00:00
import BaseInputSwitch from './BaseInputSwitch.vue';
2023-05-05 13:10:07 +00:00
2022-09-06 12:03:37 +00:00
export default {
name: 'InputSwitch',
2023-05-24 11:15:28 +00:00
extends: BaseInputSwitch,
emits: ['update:modelValue', 'change', 'focus', 'blur'],
2022-09-06 12:03:37 +00:00
methods: {
getPTOptions(key) {
return this.ptm(key, {
context: {
checked: this.checked,
disabled: this.disabled
}
});
},
onChange(event) {
if (!this.disabled && !this.readonly) {
2022-09-06 12:03:37 +00:00
const newValue = this.checked ? this.falseValue : this.trueValue;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.$emit('update:modelValue', newValue);
this.$emit('change', event);
}
},
onFocus(event) {
this.$emit('focus', event);
},
onBlur(event) {
this.$emit('blur', event);
}
},
computed: {
checked() {
return this.modelValue === this.trueValue;
}
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
</script>