2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
2023-05-24 11:53:22 +00:00
|
|
|
<div :class="cx('root')" @click="onClick($event)" v-bind="ptm('root')">
|
|
|
|
<div :class="cx('hiddenInputWrapper')" :style="sx('hiddenAccessible', isUnstyled)" 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-05-24 11:53:22 +00:00
|
|
|
<div ref="box" :class="[cx('input'), inputClass]" :style="inputStyle" v-bind="{ ...inputProps, ...ptm('input') }" :data-p-highlight="checked" :data-p-disabled="disabled" :data-p-focused="focused">
|
|
|
|
<slot name="icon" :checked="checked" :class="cx('icon')">
|
|
|
|
<component :is="checked ? 'CheckIcon' : null" :class="cx('icon')" v-bind="ptm('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: {
|
|
|
|
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>
|