Refactor #3922 - For TriStateCheckbox

This commit is contained in:
Bahadır Sofuoğlu 2023-05-07 14:19:35 +03:00
parent eb3536cb4b
commit 33b82cc951
7 changed files with 193 additions and 10 deletions

View file

@ -10,6 +10,72 @@
import { InputHTMLAttributes, VNode } from 'vue';
import { ClassComponent, GlobalComponentConstructor, Nullable } from '../ts-helpers';
export declare type TriStateCheckboxPassThroughOptionType = TriStateCheckboxPassThroughAttributes | ((options: TriStateCheckboxPassThroughMethodOptions) => TriStateCheckboxPassThroughAttributes) | null | undefined;
/**
* Custom passthrough(pt) option method.
*/
export interface TriStateCheckboxPassThroughMethodOptions {
props: TriStateCheckboxProps;
state: TriStateCheckboxState;
}
/**
* Custom passthrough(pt) options.
* @see {@link TriStateCheckboxProps.pt}
*/
export interface TriStateCheckboxPassThroughOptions {
/**
* Uses to pass attributes to the root's DOM element.
*/
root?: TriStateCheckboxPassThroughOptionType;
/**
* Uses to pass attributes to the input aria's DOM element.
*/
inputAria?: TriStateCheckboxPassThroughOptionType;
/**
* Uses to pass attributes to the input's DOM element.
*/
input?: TriStateCheckboxPassThroughOptionType;
/**
* Uses to pass attributes to the sr only aria's DOM element.
*/
srOnlyAria?: TriStateCheckboxPassThroughOptionType;
/**
* Uses to pass attributes to the checkbox box's DOM element.
*/
checboxBox?: TriStateCheckboxPassThroughOptionType;
/**
* Uses to pass attributes to the check icon's DOM element.
*/
checkIcon?: TriStateCheckboxPassThroughOptionType;
/**
* Uses to pass attributes to the uncheck icon's DOM element.
*/
uncheckIcon?: TriStateCheckboxPassThroughOptionType;
/**
* Uses to pass attributes to the nullable icon's DOM element.
*/
nullableIcon?: TriStateCheckboxPassThroughOptionType;
}
/**
* Custom passthrough attributes for each DOM elements
*/
export interface TriStateCheckboxPassThroughAttributes {
[key: string]: any;
}
/**
* Defines current inline state in TriStateCheckbox component.
*/
export interface TriStateCheckboxState {
/**
* Focused state as a boolean.
*/
focused: boolean;
}
/**
* Defines valid properties in TriStateCheckbox component.
*/
@ -45,6 +111,11 @@ export interface TriStateCheckboxProps {
* Establishes a string value that labels the component.
*/
'aria-label'?: string | undefined;
/**
* Uses to pass attributes to DOM elements inside the component.
* @type {TriStateCheckboxPassThroughOptions}
*/
pt?: TriStateCheckboxPassThroughOptions;
}
/**

View file

@ -1,6 +1,6 @@
<template>
<div :class="containerClass" @click="onClick($event)">
<div class="p-hidden-accessible">
<div :class="containerClass" @click="onClick($event)" v-bind="ptm('root')">
<div class="p-hidden-accessible" v-bind="ptm('inputAria')">
<input
ref="input"
:id="inputId"
@ -13,30 +13,32 @@
@keydown="onKeyDown($event)"
@focus="onFocus($event)"
@blur="onBlur($event)"
v-bind="inputProps"
v-bind="{ ...inputProps, ...ptm('input') }"
/>
</div>
<span class="p-sr-only" aria-live="polite">{{ ariaValueLabel }}</span>
<div ref="box" :class="['p-checkbox-box', { 'p-highlight': modelValue != null, 'p-disabled': disabled, 'p-focus': focused }]">
<span class="p-sr-only" aria-live="polite" v-bind="ptm('srOnlyAria')">{{ ariaValueLabel }}</span>
<div ref="box" :class="['p-checkbox-box', { 'p-highlight': modelValue != null, 'p-disabled': disabled, 'p-focus': focused }]" v-bind="ptm('checboxBox')">
<slot v-if="modelValue === true" name="checkicon">
<component :is="'CheckIcon'" class="p-checkbox-icon" />
<component :is="'CheckIcon'" class="p-checkbox-icon" v-bind="ptm('checkIcon')" />
</slot>
<slot v-else-if="modelValue === false" name="uncheckicon">
<component :is="'TimesIcon'" class="p-checkbox-icon" />
<component :is="'TimesIcon'" class="p-checkbox-icon" v-bind="ptm('uncheckIcon')" />
</slot>
<slot v-else name="nullableicon">
<span class="p-checkbox-icon"></span>
<span class="p-checkbox-icon" v-bind="ptm('nullableIcon')"></span>
</slot>
</div>
</div>
</template>
<script>
import BaseComponent from 'primevue/basecomponent';
import CheckIcon from 'primevue/icons/check';
import TimesIcon from 'primevue/icons/times';
export default {
name: 'TriStateCheckbox',
extends: BaseComponent,
emits: ['click', 'update:modelValue', 'change', 'keydown', 'focus', 'blur'],
props: {
modelValue: null,