2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
2023-06-23 13:08:09 +00:00
|
|
|
<div :class="cx('root')" @click="onClick($event)" v-bind="getPTOptions('root')" data-pc-name="checkbox">
|
2023-07-03 22:20:35 +00:00
|
|
|
<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"
|
|
|
|
:value="value"
|
|
|
|
:name="name"
|
|
|
|
:checked="checked"
|
|
|
|
:tabindex="tabindex"
|
|
|
|
:disabled="disabled"
|
|
|
|
:readonly="readonly"
|
|
|
|
:required="required"
|
|
|
|
:aria-labelledby="ariaLabelledby"
|
|
|
|
:aria-label="ariaLabel"
|
|
|
|
@focus="onFocus($event)"
|
|
|
|
@blur="onBlur($event)"
|
2023-05-10 08:26:12 +00:00
|
|
|
v-bind="ptm('hiddenInput')"
|
2022-09-14 11:26:01 +00:00
|
|
|
/>
|
2022-09-06 12:03:37 +00:00
|
|
|
</div>
|
2023-06-23 13:08:09 +00:00
|
|
|
<div ref="box" :class="[cx('input'), inputClass]" :style="inputStyle" v-bind="{ ...inputProps, ...getPTOptions('input') }" :data-p-highlight="checked" :data-p-disabled="disabled" :data-p-focused="focused">
|
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')" />
|
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';
|
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,
|
2022-09-06 12:03:37 +00:00
|
|
|
emits: ['click', 'update:modelValue', 'change', 'input', 'focus', 'blur'],
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
focused: false
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2023-06-23 13:08:09 +00:00
|
|
|
getPTOptions(key) {
|
|
|
|
return this.ptm(key, {
|
|
|
|
context: {
|
|
|
|
checked: this.checked,
|
|
|
|
focused: this.focused,
|
|
|
|
disabled: this.disabled
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
onClick(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) {
|
|
|
|
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('click', event);
|
|
|
|
this.$emit('update:modelValue', newModelValue);
|
|
|
|
this.$emit('change', event);
|
|
|
|
this.$emit('input', newModelValue);
|
|
|
|
this.$refs.input.focus();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onFocus(event) {
|
|
|
|
this.focused = true;
|
|
|
|
this.$emit('focus', event);
|
|
|
|
},
|
|
|
|
onBlur(event) {
|
|
|
|
this.focused = false;
|
|
|
|
this.$emit('blur', event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
checked() {
|
|
|
|
return this.binary ? this.modelValue === this.trueValue : ObjectUtils.contains(this.value, this.modelValue);
|
|
|
|
}
|
2023-04-04 12:39:42 +00:00
|
|
|
},
|
|
|
|
components: {
|
|
|
|
CheckIcon: 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>
|