2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
2024-05-24 11:07:49 +00:00
|
|
|
<div :class="cx('root')" v-bind="getPTOptions('root')" :data-p-checked="checked" :data-p-indeterminate="d_indeterminate || undefined" :data-p-disabled="disabled">
|
2024-01-12 07:07:55 +00:00
|
|
|
<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"
|
2024-02-16 11:42:42 +00:00
|
|
|
:aria-invalid="invalid || undefined"
|
2024-03-27 08:40:37 +00:00
|
|
|
:aria-checked="d_indeterminate ? 'mixed' : undefined"
|
2024-01-12 07:07:55 +00:00
|
|
|
@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')" />
|
2024-03-27 08:40:37 +00:00
|
|
|
<MinusIcon v-else-if="d_indeterminate" :class="cx('icon')" v-bind="getPTOptions('icon')" />
|
2023-04-04 12:39:42 +00:00
|
|
|
</slot>
|
2022-09-06 12:03:37 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-04-18 12:53:43 +00:00
|
|
|
import CheckIcon from 'primevue/icons/check';
|
2024-03-27 08:40:37 +00:00
|
|
|
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,
|
2024-03-27 08:40:37 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2024-01-12 07:07:55 +00:00
|
|
|
onChange(event) {
|
2023-03-07 07:36:58 +00:00
|
|
|
if (!this.disabled && !this.readonly) {
|
2022-09-06 12:03:37 +00:00
|
|
|
let newModelValue;
|
|
|
|
|
|
|
|
if (this.binary) {
|
2024-03-27 08:40:37 +00:00
|
|
|
newModelValue = this.d_indeterminate ? this.trueValue : this.checked ? this.falseValue : this.trueValue;
|
2022-09-14 11:26:01 +00:00
|
|
|
} else {
|
2024-03-27 08:40:37 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-03-27 08:40: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() {
|
2024-03-27 08:40:37 +00:00
|
|
|
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
|
|
|
}
|
2023-04-04 12:39:42 +00:00
|
|
|
},
|
|
|
|
components: {
|
2024-03-27 08:40:37 +00:00
|
|
|
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>
|