primevue-mirror/components/lib/checkbox/Checkbox.vue

103 lines
3.4 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-checked="checked" :data-p-indeterminate="d_indeterminate || undefined" :data-p-disabled="disabled">
<input
:id="inputId"
type="checkbox"
:class="[cx('input'), inputClass]"
:style="inputStyle"
:value="value"
:name="name"
:checked="checked"
:tabindex="tabindex"
:disabled="disabled"
:readonly="readonly"
:required="required"
:aria-labelledby="ariaLabelledby"
:aria-label="ariaLabel"
:aria-invalid="invalid || undefined"
:aria-checked="d_indeterminate ? 'mixed' : undefined"
@focus="onFocus"
@blur="onBlur"
@change="onChange"
v-bind="getPTOptions('input')"
/>
<div :class="cx('box')" v-bind="getPTOptions('box')">
2023-05-24 11:53:22 +00:00
<slot name="icon" :checked="checked" :class="cx('icon')">
2023-06-23 13:08:09 +00:00
<CheckIcon v-if="checked" :class="cx('icon')" v-bind="getPTOptions('icon')" />
<MinusIcon v-else-if="d_indeterminate" :class="cx('icon')" v-bind="getPTOptions('icon')" />
</slot>
2022-09-06 12:03:37 +00:00
</div>
</div>
</template>
<script>
import CheckIcon from 'primevue/icons/check';
import MinusIcon from 'primevue/icons/minus';
2022-09-14 11:26:01 +00:00
import { ObjectUtils } from 'primevue/utils';
2023-05-24 11:53:22 +00:00
import BaseCheckbox from './BaseCheckbox.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'Checkbox',
2023-05-24 11:53:22 +00:00
extends: BaseCheckbox,
2024-02-11 08:10:29 +00:00
inheritAttrs: false,
emits: ['update:modelValue', 'change', 'focus', 'blur', 'update:indeterminate'],
data() {
return {
d_indeterminate: this.indeterminate
};
},
watch: {
indeterminate(newValue) {
this.d_indeterminate = newValue;
}
},
2022-09-06 12:03:37 +00:00
methods: {
2023-06-23 13:08:09 +00:00
getPTOptions(key) {
2024-02-11 08:10:29 +00:00
const _ptm = key === 'root' ? this.ptmi : this.ptm;
return _ptm(key, {
2023-06-23 13:08:09 +00:00
context: {
checked: this.checked,
disabled: this.disabled
}
});
},
onChange(event) {
if (!this.disabled && !this.readonly) {
2022-09-06 12:03:37 +00:00
let newModelValue;
if (this.binary) {
newModelValue = this.d_indeterminate ? this.trueValue : this.checked ? this.falseValue : this.trueValue;
2022-09-14 11:26:01 +00:00
} else {
if (this.checked || this.d_indeterminate) newModelValue = this.modelValue.filter((val) => !ObjectUtils.equals(val, this.value));
2022-09-14 11:26:01 +00:00
else newModelValue = this.modelValue ? [...this.modelValue, this.value] : [this.value];
2022-09-06 12:03:37 +00:00
}
if (this.d_indeterminate) {
this.d_indeterminate = false;
this.$emit('update:indeterminate', this.d_indeterminate);
}
2022-09-06 12:03:37 +00:00
this.$emit('update:modelValue', newModelValue);
this.$emit('change', event);
}
},
onFocus(event) {
this.$emit('focus', event);
},
onBlur(event) {
this.$emit('blur', event);
}
},
computed: {
checked() {
return this.d_indeterminate ? false : this.binary ? this.modelValue === this.trueValue : ObjectUtils.contains(this.value, this.modelValue);
2022-09-06 12:03:37 +00:00
}
},
components: {
CheckIcon,
MinusIcon
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>