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

67 lines
1.9 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-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"
:aria-invalid="invalid || undefined"
@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,
2024-02-11 08:10:29 +00:00
inheritAttrs: false,
emits: ['update:modelValue', 'change', 'focus', 'blur'],
2022-09-06 12:03:37 +00:00
methods: {
getPTOptions(key) {
2024-02-11 08:30:04 +00:00
const _ptm = key === 'root' ? this.ptmi : this.ptm;
2024-02-11 08:10:29 +00:00
return _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>