2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
|
|
|
<div :class="containerClass" @click="onClick($event)">
|
|
|
|
<div class="p-hidden-accessible">
|
2022-09-14 11:26:01 +00:00
|
|
|
<input
|
|
|
|
ref="input"
|
|
|
|
:id="inputId"
|
|
|
|
type="checkbox"
|
|
|
|
role="switch"
|
|
|
|
:class="inputClass"
|
|
|
|
:style="inputStyle"
|
|
|
|
:checked="checked"
|
|
|
|
:disabled="disabled"
|
|
|
|
:aria-checked="checked"
|
|
|
|
:aria-labelledby="ariaLabelledby"
|
|
|
|
:aria-label="ariaLabel"
|
|
|
|
@focus="onFocus($event)"
|
|
|
|
@blur="onBlur($event)"
|
|
|
|
v-bind="inputProps"
|
|
|
|
/>
|
2022-09-06 12:03:37 +00:00
|
|
|
</div>
|
|
|
|
<span class="p-inputswitch-slider"></span>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
name: 'InputSwitch',
|
|
|
|
emits: ['click', 'update:modelValue', 'change', 'input', 'focus', 'blur'],
|
|
|
|
props: {
|
|
|
|
modelValue: {
|
|
|
|
type: null,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
trueValue: {
|
|
|
|
type: null,
|
|
|
|
default: true
|
|
|
|
},
|
|
|
|
falseValue: {
|
|
|
|
type: null,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
disabled: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
2022-09-14 11:26:01 +00:00
|
|
|
inputId: {
|
|
|
|
type: String,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
inputClass: {
|
2023-03-09 07:02:25 +00:00
|
|
|
type: [String, Object],
|
2022-09-14 11:26:01 +00:00
|
|
|
default: null
|
|
|
|
},
|
|
|
|
inputStyle: {
|
2023-03-09 07:02:25 +00:00
|
|
|
type: Object,
|
2022-09-14 11:26:01 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
focused: false
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onClick(event) {
|
|
|
|
if (!this.disabled) {
|
|
|
|
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('click', event);
|
|
|
|
this.$emit('update:modelValue', newValue);
|
|
|
|
this.$emit('change', event);
|
|
|
|
this.$emit('input', newValue);
|
|
|
|
this.$refs.input.focus();
|
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
onFocus(event) {
|
|
|
|
this.focused = true;
|
|
|
|
this.$emit('focus', event);
|
|
|
|
},
|
|
|
|
onBlur(event) {
|
|
|
|
this.focused = false;
|
|
|
|
this.$emit('blur', event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
containerClass() {
|
|
|
|
return [
|
|
|
|
'p-inputswitch p-component',
|
|
|
|
{
|
|
|
|
'p-inputswitch-checked': this.checked,
|
2022-09-14 11:26:01 +00:00
|
|
|
'p-disabled': this.disabled,
|
2022-09-06 12:03:37 +00:00
|
|
|
'p-focus': this.focused
|
|
|
|
}
|
|
|
|
];
|
|
|
|
},
|
|
|
|
checked() {
|
|
|
|
return this.modelValue === this.trueValue;
|
|
|
|
}
|
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|
2022-09-06 12:03:37 +00:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.p-inputswitch {
|
|
|
|
position: relative;
|
|
|
|
display: inline-block;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-inputswitch-slider {
|
|
|
|
position: absolute;
|
|
|
|
cursor: pointer;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
right: 0;
|
|
|
|
bottom: 0;
|
2023-02-28 17:49:08 +00:00
|
|
|
border: 1px solid transparent;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
.p-inputswitch-slider:before {
|
|
|
|
position: absolute;
|
2022-09-14 11:26:01 +00:00
|
|
|
content: '';
|
2022-09-06 12:03:37 +00:00
|
|
|
top: 50%;
|
|
|
|
}
|
|
|
|
</style>
|