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

112 lines
3.9 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-05-29 09:19:55 +00:00
<div :class="cx('root')" @click="onClick($event)" v-bind="ptm('root')" data-pc-name="tristatecheckbox">
<div class="p-hidden-accessible" v-bind="ptm('hiddenInputWrapper')" :data-p-hidden-accessible="true">
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)"
2023-05-09 09:17:12 +00:00
v-bind="{ ...inputProps, ...ptm('hiddenInput') }"
2022-09-14 11:26:01 +00:00
/>
2022-09-06 12:03:37 +00:00
</div>
<span role="status" class="p-hidden-accessible" aria-live="polite" v-bind="ptm('hiddenValueLabel')" :data-p-hidden-accessible="true">{{ ariaValueLabel }}</span>
2023-05-24 14:39:47 +00:00
<div ref="box" :class="cx('checkbox')" v-bind="getPTOptions('checkbox')" :data-p-highlight="modelValue != null" :data-p-disabled="disabled" :data-p-focused="focused">
<slot v-if="modelValue === true" name="checkicon" :class="cx('checkIcon')">
<component :is="'CheckIcon'" :class="cx('checkIcon')" v-bind="ptm('checkIcon')" />
</slot>
2023-05-24 14:39:47 +00:00
<slot v-else-if="modelValue === false" name="uncheckicon" :class="cx('uncheckIcon')">
<component :is="'TimesIcon'" :class="cx('uncheckIcon')" v-bind="ptm('uncheckIcon')" />
</slot>
2023-05-24 14:39:47 +00:00
<slot v-else name="nullableicon" :class="cx('nullableIcon')">
<span :class="cx('nullableIcon')" v-bind="ptm('nullableIcon')"></span>
</slot>
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,
2022-09-06 12:03:37 +00:00
emits: ['click', 'update:modelValue', 'change', 'keydown', 'focus', 'blur'],
data() {
return {
focused: false
};
},
methods: {
2023-05-09 09:17:12 +00:00
getPTOptions(key) {
return this.ptm(key, {
context: {
active: this.modelValue !== null,
focused: this.focused,
disabled: this.disabled
}
});
},
2022-09-06 12:03:37 +00:00
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
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' || event.code === 'NumpadEnter') {
2022-09-06 12:03:37 +00:00
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: {
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: 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>