primevue-mirror/components/lib/tristatecheckbox/TriStateCheckbox.vue

100 lines
3.2 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<div :class="cx('root')" v-bind="getPTOptions('root')" :data-p-highlight="active" :data-p-disabled="disabled">
<input
:id="inputId"
type="checkbox"
:class="[cx('input'), inputClass]"
:style="inputStyle"
:value="modelValue"
:checked="checked"
:tabindex="tabindex"
:disabled="disabled"
:readonly="readonly"
:aria-labelledby="ariaLabelledby"
:aria-label="ariaLabel"
@focus="onFocus"
@blur="onBlur"
@change="onChange"
v-bind="getPTOptions('input')"
/>
<span role="status" class="p-hidden-accessible" aria-live="polite" v-bind="getPTOptions('hiddenValueLabel')" :data-p-hidden-accessible="true">{{ ariaValueLabel }}</span>
<div :class="cx('box')" v-bind="getPTOptions('box')">
2023-05-24 14:39:47 +00:00
<slot v-if="modelValue === true" name="checkicon" :class="cx('checkIcon')">
<CheckIcon :class="cx('checkIcon')" v-bind="getPTOptions('checkIcon')" />
</slot>
2023-05-24 14:39:47 +00:00
<slot v-else-if="modelValue === false" name="uncheckicon" :class="cx('uncheckIcon')">
<TimesIcon :class="cx('uncheckIcon')" v-bind="getPTOptions('uncheckIcon')" />
</slot>
<slot v-else name="nullableicon" :class="cx('nullableIcon')" />
2022-09-06 12:03:37 +00:00
</div>
</div>
</template>
<script>
import CheckIcon from 'primevue/icons/check';
import TimesIcon from 'primevue/icons/times';
2023-05-24 14:39:47 +00:00
import BaseTriStateCheckbox from './BaseTriStateCheckbox.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'TriStateCheckbox',
2023-05-24 14:39:47 +00:00
extends: BaseTriStateCheckbox,
emits: ['update:modelValue', 'change', 'focus', 'blur'],
2022-09-06 12:03:37 +00:00
methods: {
2023-05-09 09:17:12 +00:00
getPTOptions(key) {
return this.ptm(key, {
context: {
active: this.active,
2023-05-09 09:17:12 +00:00
disabled: this.disabled
}
});
},
2022-09-06 12:03:37 +00:00
updateModel() {
let newValue;
2022-09-06 12:03:37 +00:00
switch (this.modelValue) {
case true:
newValue = false;
break;
2022-09-06 12:03:37 +00:00
case false:
newValue = null;
break;
2022-09-06 12:03:37 +00:00
default:
newValue = true;
break;
2022-09-06 12:03:37 +00:00
}
this.$emit('update:modelValue', newValue);
2022-09-06 12:03:37 +00:00
},
onChange(event) {
if (!this.disabled && !this.readonly) {
2022-09-06 12:03:37 +00:00
this.updateModel();
this.$emit('change', event);
2022-09-06 12:03:37 +00:00
}
},
onFocus(event) {
this.$emit('focus', event);
},
onBlur(event) {
this.$emit('blur', event);
}
},
computed: {
active() {
return this.modelValue != null;
},
checked() {
return this.modelValue === true;
},
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
}
},
components: {
CheckIcon,
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>