primevue-mirror/components/lib/datatable/RowCheckbox.vue

117 lines
3.6 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<div :class="cx('checkboxWrapper')" @click="onClick" v-bind="getColumnPT('checkboxWrapper')">
<div class="p-hidden-accessible" v-bind="getColumnPT('hiddenInputWrapper')" :data-p-hidden-accessible="true">
2023-05-08 14:08:06 +00:00
<input
ref="input"
type="checkbox"
:checked="checked"
:disabled="$attrs.disabled"
:tabindex="$attrs.disabled ? null : '0'"
:aria-label="checkboxAriaLabel"
@focus="onFocus($event)"
@blur="onBlur($event)"
@keydown="onKeydown"
v-bind="getColumnPT('hiddenInput')"
2023-05-08 14:08:06 +00:00
/>
2022-12-08 11:04:25 +00:00
</div>
<div ref="box" :class="cx('checkbox')" v-bind="getColumnPT('checkbox')">
<component v-if="rowCheckboxIconTemplate" :is="rowCheckboxIconTemplate" :checked="checked" :class="cx('checkboxIcon')" />
<CheckIcon v-else-if="!rowCheckboxIconTemplate && !!checked" :class="cx('checkboxIcon')" v-bind="getColumnPT('checkboxIcon')" />
2022-09-06 12:03:37 +00:00
</div>
</div>
</template>
<script>
2023-05-08 14:08:06 +00:00
import BaseComponent from 'primevue/basecomponent';
import CheckIcon from 'primevue/icons/check';
2022-12-08 11:04:25 +00:00
import { DomHandler } from 'primevue/utils';
import { mergeProps } from 'vue';
2022-12-08 11:04:25 +00:00
2022-09-06 12:03:37 +00:00
export default {
name: 'RowCheckbox',
hostName: 'DataTable',
2023-05-08 14:08:06 +00:00
extends: BaseComponent,
2022-09-06 12:03:37 +00:00
emits: ['change'],
props: {
2022-09-14 11:26:01 +00:00
value: null,
checked: null,
column: null,
rowCheckboxIconTemplate: {
type: Function,
default: null
},
index: {
type: Number,
default: null
}
2022-09-06 12:03:37 +00:00
},
data() {
return {
focused: false
};
},
methods: {
getColumnPT(key) {
const columnMetaData = {
props: this.column.props,
parent: {
instance: this,
props: this.$props,
state: this.$data
},
2023-05-08 14:08:06 +00:00
context: {
index: this.index,
2023-05-08 14:08:06 +00:00
checked: this.checked,
focused: this.focused,
disabled: this.$attrs.disabled
}
};
return mergeProps(this.ptm(`column.${key}`, { column: columnMetaData }), this.ptm(`column.${key}`, columnMetaData), this.ptmo(this.getColumnProp(), key, columnMetaData));
2023-05-08 14:08:06 +00:00
},
getColumnProp() {
return this.column.props && this.column.props.pt ? this.column.props.pt : undefined; //@todo:
},
2022-09-06 12:03:37 +00:00
onClick(event) {
if (!this.$attrs.disabled) {
this.$emit('change', {
originalEvent: event,
data: this.value
});
2022-12-08 11:04:25 +00:00
DomHandler.focus(this.$refs.input);
2022-09-06 12:03:37 +00:00
}
2022-12-08 11:04:25 +00:00
event.preventDefault();
event.stopPropagation();
2022-09-06 12:03:37 +00:00
},
onFocus() {
this.focused = true;
},
onBlur() {
this.focused = false;
2022-12-08 11:04:25 +00:00
},
onKeydown(event) {
switch (event.code) {
case 'Space': {
this.onClick(event);
break;
}
default:
break;
}
}
},
computed: {
checkboxAriaLabel() {
return this.$primevue.config.locale.aria ? (this.checked ? this.$primevue.config.locale.aria.selectRow : this.$primevue.config.locale.aria.unselectRow) : undefined;
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>