Refactor #4211 - For ToggleButton

pull/4239/head
Tuğçe Küçükoğlu 2023-08-02 17:32:32 +03:00
parent 991369ff6e
commit 9672b27cf0
2 changed files with 35 additions and 4 deletions

View File

@ -20,6 +20,7 @@ export interface ToggleButtonPassThroughMethodOptions {
instance: any; instance: any;
props: ToggleButtonProps; props: ToggleButtonProps;
state: ToggleButtonState; state: ToggleButtonState;
context: ToggleButtonContext;
} }
/** /**
@ -66,11 +67,32 @@ export interface ToggleButtonPassThroughAttributes {
*/ */
export interface ToggleButtonState { export interface ToggleButtonState {
/** /**
* Focused state as a number. * Focused state as a boolean.
*/ */
focused: boolean; focused: boolean;
} }
/**
* Defines current options in ToggleButton component.
*/
export interface ToggleButtonContext {
/**
* Current focused state as a boolean.
* @defaultValue false
*/
focused: boolean;
/**
* Current disabled state as a boolean.
* @defaultValue false
*/
disabled: boolean;
/**
* Current highlighted state as a boolean.
* @defaultValue false
*/
highlighted: boolean;
}
/** /**
* Defines valid properties in ToggleButton component. * Defines valid properties in ToggleButton component.
*/ */

View File

@ -1,5 +1,5 @@
<template> <template>
<div ref="container" v-ripple :class="cx('root')" @click="onClick($event)" v-bind="ptm('root')" :data-p-active="modelValue === true" data-pc-name="togglebutton"> <div ref="container" v-ripple :class="cx('root')" @click="onClick($event)" v-bind="ptm('root', ptOptions)" :data-p-active="modelValue === true" data-pc-name="togglebutton">
<span class="p-hidden-accessible" v-bind="ptm('hiddenInputWrapper')" :data-p-hidden-accessible="true"> <span class="p-hidden-accessible" v-bind="ptm('hiddenInputWrapper')" :data-p-hidden-accessible="true">
<input <input
:id="inputId" :id="inputId"
@ -17,9 +17,9 @@
/> />
</span> </span>
<slot name="icon" :value="modelValue" :class="cx('icon')"> <slot name="icon" :value="modelValue" :class="cx('icon')">
<span v-if="onIcon || offIcon" :class="[cx('icon'), modelValue ? onIcon : offIcon]" v-bind="ptm('icon')" /> <span v-if="onIcon || offIcon" :class="[cx('icon'), modelValue ? onIcon : offIcon]" v-bind="ptm('icon', ptOptions)" />
</slot> </slot>
<span :class="cx('label')" v-bind="ptm('label')">{{ label }}</span> <span :class="cx('label')" v-bind="ptm('label', ptOptions)">{{ label }}</span>
</div> </div>
</template> </template>
@ -87,6 +87,15 @@ export default {
}, },
label() { label() {
return this.hasLabel ? (this.modelValue ? this.onLabel : this.offLabel) : '&nbsp;'; return this.hasLabel ? (this.modelValue ? this.onLabel : this.offLabel) : '&nbsp;';
},
ptOptions() {
return {
context: {
focused: this.focused,
disabled: this.disabled,
highlighted: this.modelValue === true
}
};
} }
}, },
directives: { directives: {