Fixed #5479 - Checkbox: new indeterminate state

pull/5507/head
tugcekucukoglu 2024-03-27 11:40:37 +03:00
parent 5486ec46fe
commit c696989964
5 changed files with 36 additions and 11 deletions

View File

@ -13,6 +13,10 @@ export default {
type: String, type: String,
default: null default: null
}, },
indeterminate: {
type: Boolean,
default: false
},
trueValue: { trueValue: {
type: null, type: null,
default: true default: true

View File

@ -111,6 +111,11 @@ export interface CheckboxProps {
* @default false * @default false
*/ */
binary?: boolean; binary?: boolean;
/**
* When present, it specifies input state as indeterminate.
* @default false
*/
indeterminate?: boolean | undefined;
/** /**
* When present, it specifies that the component should have invalid state style. * When present, it specifies that the component should have invalid state style.
* @defaultValue false * @defaultValue false

View File

@ -1,5 +1,5 @@
<template> <template>
<div :class="cx('root')" v-bind="getPTOptions('root')" :data-p-highlight="checked" :data-p-disabled="disabled"> <div :class="cx('root')" v-bind="getPTOptions('root')" :data-p-highlight="checked" :data-p-checked="checked" :data-p-indeterminate="d_indeterminate || undefined" :data-p-disabled="disabled">
<input <input
:id="inputId" :id="inputId"
type="checkbox" type="checkbox"
@ -15,6 +15,7 @@
:aria-labelledby="ariaLabelledby" :aria-labelledby="ariaLabelledby"
:aria-label="ariaLabel" :aria-label="ariaLabel"
:aria-invalid="invalid || undefined" :aria-invalid="invalid || undefined"
:aria-checked="d_indeterminate ? 'mixed' : undefined"
@focus="onFocus" @focus="onFocus"
@blur="onBlur" @blur="onBlur"
@change="onChange" @change="onChange"
@ -23,6 +24,7 @@
<div :class="cx('box')" v-bind="getPTOptions('box')"> <div :class="cx('box')" v-bind="getPTOptions('box')">
<slot name="icon" :checked="checked" :class="cx('icon')"> <slot name="icon" :checked="checked" :class="cx('icon')">
<CheckIcon v-if="checked" :class="cx('icon')" v-bind="getPTOptions('icon')" /> <CheckIcon v-if="checked" :class="cx('icon')" v-bind="getPTOptions('icon')" />
<MinusIcon v-else-if="d_indeterminate" :class="cx('icon')" v-bind="getPTOptions('icon')" />
</slot> </slot>
</div> </div>
</div> </div>
@ -30,6 +32,7 @@
<script> <script>
import CheckIcon from 'primevue/icons/check'; import CheckIcon from 'primevue/icons/check';
import MinusIcon from 'primevue/icons/minus';
import { ObjectUtils } from 'primevue/utils'; import { ObjectUtils } from 'primevue/utils';
import BaseCheckbox from './BaseCheckbox.vue'; import BaseCheckbox from './BaseCheckbox.vue';
@ -37,7 +40,17 @@ export default {
name: 'Checkbox', name: 'Checkbox',
extends: BaseCheckbox, extends: BaseCheckbox,
inheritAttrs: false, inheritAttrs: false,
emits: ['update:modelValue', 'change', 'focus', 'blur'], emits: ['update:modelValue', 'change', 'focus', 'blur', 'update:indeterminate'],
data() {
return {
d_indeterminate: this.indeterminate
};
},
watch: {
indeterminate(newValue) {
this.d_indeterminate = newValue;
}
},
methods: { methods: {
getPTOptions(key) { getPTOptions(key) {
const _ptm = key === 'root' ? this.ptmi : this.ptm; const _ptm = key === 'root' ? this.ptmi : this.ptm;
@ -54,12 +67,17 @@ export default {
let newModelValue; let newModelValue;
if (this.binary) { if (this.binary) {
newModelValue = this.checked ? this.falseValue : this.trueValue; newModelValue = this.d_indeterminate ? this.trueValue : this.checked ? this.falseValue : this.trueValue;
} else { } else {
if (this.checked) newModelValue = this.modelValue.filter((val) => !ObjectUtils.equals(val, this.value)); if (this.checked || this.d_indeterminate) newModelValue = this.modelValue.filter((val) => !ObjectUtils.equals(val, this.value));
else newModelValue = this.modelValue ? [...this.modelValue, this.value] : [this.value]; else newModelValue = this.modelValue ? [...this.modelValue, this.value] : [this.value];
} }
if (this.d_indeterminate) {
this.d_indeterminate = false;
this.$emit('update:indeterminate', this.d_indeterminate);
}
this.$emit('update:modelValue', newModelValue); this.$emit('update:modelValue', newModelValue);
this.$emit('change', event); this.$emit('change', event);
} }
@ -73,11 +91,12 @@ export default {
}, },
computed: { computed: {
checked() { checked() {
return this.binary ? this.modelValue === this.trueValue : ObjectUtils.contains(this.value, this.modelValue); return this.d_indeterminate ? false : this.binary ? this.modelValue === this.trueValue : ObjectUtils.contains(this.value, this.modelValue);
} }
}, },
components: { components: {
CheckIcon CheckIcon,
MinusIcon
} }
}; };
</script> </script>

View File

@ -26,10 +26,9 @@
<component v-else :is="node.collapsedIcon ? 'span' : 'ChevronRightIcon'" :class="cx('togglerIcon')" v-bind="getPTOptions('togglerIcon')" /> <component v-else :is="node.collapsedIcon ? 'span' : 'ChevronRightIcon'" :class="cx('togglerIcon')" v-bind="getPTOptions('togglerIcon')" />
</template> </template>
</button> </button>
<Checkbox v-if="checkboxMode" :modelValue="checked" :binary="true" :class="cx('nodeCheckbox')" :tabindex="-1" :unstyled="unstyled" :pt="getPTOptions('nodeCheckbox')" :data-p-checked="checked" :data-p-partialchecked="partialChecked"> <Checkbox v-if="checkboxMode" :modelValue="checked" :binary="true" :indeterminate="partialChecked" :class="cx('nodeCheckbox')" :tabindex="-1" :unstyled="unstyled" :pt="getPTOptions('nodeCheckbox')" :data-p-partialchecked="partialChecked">
<template #icon="slotProps"> <template #icon="slotProps">
<component v-if="templates['checkboxicon']" :is="templates['checkboxicon']" :checked="slotProps.checked" :partialChecked="partialChecked" :class="slotProps.class" /> <component v-if="templates['checkboxicon']" :is="templates['checkboxicon']" :checked="slotProps.checked" :partialChecked="partialChecked" :class="slotProps.class" />
<component v-else :is="checked ? 'CheckIcon' : partialChecked ? 'MinusIcon' : null" :class="slotProps.class" v-bind="getPTOptions('nodeCheckbox.icon')" />
</template> </template>
</Checkbox> </Checkbox>
<component v-if="templates['nodeicon']" :is="templates['nodeicon']" :node="node" :class="[cx('nodeIcon')]"></component> <component v-if="templates['nodeicon']" :is="templates['nodeicon']" :node="node" :class="[cx('nodeIcon')]"></component>

View File

@ -18,15 +18,13 @@
:class="cx('rowCheckbox')" :class="cx('rowCheckbox')"
@change="toggleCheckbox" @change="toggleCheckbox"
:tabindex="-1" :tabindex="-1"
:indeterminate="partialChecked"
:unstyled="unstyled" :unstyled="unstyled"
:pt="getColumnCheckboxPT('rowCheckbox')" :pt="getColumnCheckboxPT('rowCheckbox')"
:data-p-highlight="checked"
:data-p-checked="checked"
:data-p-partialchecked="partialChecked" :data-p-partialchecked="partialChecked"
> >
<template #icon="slotProps"> <template #icon="slotProps">
<component v-if="templates['checkboxicon']" :is="templates['checkboxicon']" :checked="slotProps.checked" :partialChecked="partialChecked" :class="slotProps.class" /> <component v-if="templates['checkboxicon']" :is="templates['checkboxicon']" :checked="slotProps.checked" :partialChecked="partialChecked" :class="slotProps.class" />
<component v-else :is="checked ? 'CheckIcon' : partialChecked ? 'MinusIcon' : null" :class="slotProps.class" v-bind="getColumnCheckboxPT('rowCheckbox.icon')" />
</template> </template>
</Checkbox> </Checkbox>
<component v-if="column.children && column.children.body" :is="column.children.body" :node="node" :column="column" /> <component v-if="column.children && column.children.body" :is="column.children.body" :node="node" :column="column" />