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

82 lines
2.8 KiB
Vue
Raw Normal View History

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')" />
</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,
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) {
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);
}
},
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>