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

83 lines
2.6 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="checked" :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"
@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')" />
</slot>
2022-09-06 12:03:37 +00:00
</div>
</div>
</template>
<script>
import CheckIcon from 'primevue/icons/check';
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'],
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.checked ? this.falseValue : this.trueValue;
2022-09-14 11:26:01 +00:00
} else {
if (this.checked) newModelValue = this.modelValue.filter((val) => !ObjectUtils.equals(val, this.value));
else newModelValue = this.modelValue ? [...this.modelValue, this.value] : [this.value];
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.binary ? this.modelValue === this.trueValue : ObjectUtils.contains(this.value, this.modelValue);
}
},
components: {
CheckIcon
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>