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

149 lines
4.2 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-05-05 09:09:09 +00:00
<div :class="containerClass" @click="onClick($event)" v-bind="ptm('root')">
2023-05-05 13:09:56 +00:00
<div class="p-hidden-accessible" v-bind="ptm('hiddenAccessible')">
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-05 09:09:09 +00:00
v-bind="ptm('inputAria')"
2022-09-14 11:26:01 +00:00
/>
2022-09-06 12:03:37 +00:00
</div>
2023-05-05 09:09:09 +00:00
<div ref="box" :class="['p-checkbox-box', inputClass, { 'p-highlight': checked, 'p-disabled': disabled, 'p-focus': focused }]" :style="inputStyle" v-bind="{ ...inputProps, ...ptm('input') }">
<slot name="icon" :checked="checked">
2023-05-05 09:09:09 +00:00
<component :is="checked ? 'CheckIcon' : null" class="p-checkbox-icon" v-bind="ptm('icon')" />
</slot>
2022-09-06 12:03:37 +00:00
</div>
</div>
</template>
<script>
2023-05-05 09:09:09 +00:00
import BaseComponent from 'primevue/basecomponent';
import CheckIcon from 'primevue/icons/check';
2022-09-14 11:26:01 +00:00
import { ObjectUtils } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
export default {
name: 'Checkbox',
2023-05-05 09:09:09 +00:00
extends: BaseComponent,
2022-09-06 12:03:37 +00:00
emits: ['click', 'update:modelValue', 'change', 'input', 'focus', 'blur'],
props: {
value: null,
modelValue: null,
binary: Boolean,
name: {
type: String,
default: null
},
trueValue: {
type: null,
default: true
},
falseValue: {
type: null,
default: false
},
disabled: {
type: Boolean,
default: false
},
readonly: {
type: Boolean,
default: false
},
required: {
type: Boolean,
default: false
},
tabindex: {
type: Number,
default: null
},
2022-09-14 11:26:01 +00:00
inputId: {
type: String,
default: null
},
inputClass: {
type: [String, Object],
2022-09-14 11:26:01 +00:00
default: null
},
inputStyle: {
type: Object,
2022-09-14 11:26:01 +00:00
default: null
},
inputProps: {
type: null,
default: null
},
2022-09-06 12:03:37 +00:00
'aria-labelledby': {
type: String,
2022-09-14 11:26:01 +00:00
default: null
2022-09-06 12:03:37 +00:00
},
'aria-label': {
type: String,
default: null
}
},
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);
},
containerClass() {
return [
2022-09-14 11:26:01 +00:00
'p-checkbox p-component',
{
2022-09-06 12:03:37 +00:00
'p-checkbox-checked': this.checked,
'p-checkbox-disabled': this.disabled,
'p-checkbox-focused': this.focused
2022-09-14 11:26:01 +00:00
}
];
2022-09-06 12:03:37 +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>