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"
|
|
|
|
:checked="modelValue === true"
|
|
|
|
:tabindex="tabindex"
|
|
|
|
:disabled="disabled"
|
|
|
|
:aria-labelledby="ariaLabelledby"
|
|
|
|
:aria-label="ariaLabel"
|
|
|
|
@keydown="onKeyDown($event)"
|
|
|
|
@focus="onFocus($event)"
|
|
|
|
@blur="onBlur($event)"
|
|
|
|
v-bind="inputProps"
|
|
|
|
/>
|
2022-09-06 12:03:37 +00:00
|
|
|
</div>
|
2022-09-14 11:26:01 +00:00
|
|
|
<span class="p-sr-only" aria-live="polite">{{ ariaValueLabel }}</span>
|
|
|
|
<div ref="box" :class="['p-checkbox-box', { 'p-highlight': modelValue != null, 'p-disabled': disabled, 'p-focus': focused }]">
|
2023-04-04 12:42:13 +00:00
|
|
|
<slot v-if="modelValue === true" name="checkicon">
|
|
|
|
<component :is="'CheckIcon'" class="p-checkbox-icon" />
|
|
|
|
</slot>
|
|
|
|
<slot v-else-if="modelValue === false" name="uncheckicon">
|
|
|
|
<component :is="'TimesIcon'" class="p-checkbox-icon" />
|
|
|
|
</slot>
|
2023-04-14 12:28:17 +00:00
|
|
|
<slot v-else name="nullableicon">
|
|
|
|
<span class="p-checkbox-icon"></span>
|
|
|
|
</slot>
|
2022-09-06 12:03:37 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-04-04 12:42:13 +00:00
|
|
|
import CheckIcon from 'primevue/icon/check';
|
|
|
|
import TimesIcon from 'primevue/icon/times';
|
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
export default {
|
|
|
|
name: 'TriStateCheckbox',
|
|
|
|
emits: ['click', 'update:modelValue', 'change', 'keydown', 'focus', 'blur'],
|
|
|
|
props: {
|
|
|
|
modelValue: null,
|
2022-09-14 11:26:01 +00:00
|
|
|
inputId: {
|
|
|
|
type: String,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
inputProps: {
|
|
|
|
type: null,
|
|
|
|
default: null
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
disabled: {
|
2022-09-14 11:26:01 +00:00
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
tabindex: {
|
|
|
|
type: Number,
|
|
|
|
default: 0
|
|
|
|
},
|
|
|
|
'aria-labelledby': {
|
|
|
|
type: String,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
'aria-label': {
|
|
|
|
type: String,
|
|
|
|
default: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
focused: false
|
|
|
|
};
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
updateModel() {
|
|
|
|
if (!this.disabled) {
|
|
|
|
let newValue;
|
|
|
|
|
|
|
|
switch (this.modelValue) {
|
|
|
|
case true:
|
|
|
|
newValue = false;
|
2022-09-14 11:26:01 +00:00
|
|
|
break;
|
2022-09-06 12:03:37 +00:00
|
|
|
|
|
|
|
case false:
|
|
|
|
newValue = null;
|
2022-09-14 11:26:01 +00:00
|
|
|
break;
|
2022-09-06 12:03:37 +00:00
|
|
|
|
2023-03-17 13:56:30 +00:00
|
|
|
default:
|
2022-09-06 12:03:37 +00:00
|
|
|
newValue = true;
|
2022-09-14 11:26:01 +00:00
|
|
|
break;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.$emit('update:modelValue', newValue);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onClick(event) {
|
|
|
|
this.updateModel();
|
|
|
|
this.$emit('click', event);
|
|
|
|
this.$emit('change', event);
|
|
|
|
this.$refs.input.focus();
|
|
|
|
},
|
|
|
|
onKeyDown(event) {
|
|
|
|
if (event.code === 'Enter') {
|
|
|
|
this.updateModel();
|
|
|
|
this.$emit('keydown', event);
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onFocus(event) {
|
|
|
|
this.focused = true;
|
|
|
|
this.$emit('focus', event);
|
|
|
|
},
|
|
|
|
onBlur(event) {
|
|
|
|
this.focused = false;
|
|
|
|
this.$emit('blur', event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
containerClass() {
|
|
|
|
return [
|
2022-09-14 11:26:01 +00:00
|
|
|
'p-checkbox p-component',
|
|
|
|
{
|
2022-09-06 12:03:37 +00:00
|
|
|
'p-checkbox-checked': this.modelValue === true,
|
|
|
|
'p-checkbox-disabled': this.disabled,
|
|
|
|
'p-checkbox-focused': this.focused
|
2022-09-14 11:26:01 +00:00
|
|
|
}
|
|
|
|
];
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
ariaValueLabel() {
|
2022-09-14 11:26:01 +00:00
|
|
|
return this.modelValue ? this.$primevue.config.locale.aria.trueLabel : this.modelValue === false ? this.$primevue.config.locale.aria.falseLabel : this.$primevue.config.locale.aria.nullLabel;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2023-04-04 12:42:13 +00:00
|
|
|
},
|
|
|
|
components: {
|
|
|
|
CheckIcon: CheckIcon,
|
|
|
|
TimesIcon: TimesIcon
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|
2022-09-06 12:03:37 +00:00
|
|
|
</script>
|