Refactor #3965 - For ToggleButton

pull/3997/head
Tuğçe Küçükoğlu 2023-05-24 17:26:41 +03:00
parent 2d9be7a7d0
commit eff31e4a5a
3 changed files with 107 additions and 77 deletions

View File

@ -0,0 +1,95 @@
<script>
import BaseComponent from 'primevue/basecomponent';
import { useStyle } from 'primevue/usestyle';
const styles = ``;
const classes = {
root: ({ instance, props }) => [
'p-button p-togglebutton p-component',
{
'p-focus': instance.focused,
'p-button-icon-only': instance.hasIcon && !instance.hasLabel,
'p-disabled': props.disabled,
'p-highlight': props.modelValue === true
}
],
hiddenInputWrapper: 'p-hidden-accessible',
icon: ({ instance, props }) => [
props.modelValue ? props.onIcon : props.offIcon,
'p-button-icon',
{
'p-button-icon-left': props.iconPos === 'left' && instance.label,
'p-button-icon-right': props.iconPos === 'right' && instance.label
}
],
label: 'p-button-label'
};
const { load: loadStyle, unload: unloadStyle } = useStyle(styles, { id: 'primevue_togglebutton_style', manual: true });
export default {
name: 'BaseToggleButton',
extends: BaseComponent,
props: {
modelValue: Boolean,
onIcon: String,
offIcon: String,
onLabel: {
type: String,
default: 'Yes'
},
offLabel: {
type: String,
default: 'No'
},
iconPos: {
type: String,
default: 'left'
},
disabled: {
type: Boolean,
default: false
},
tabindex: {
type: Number,
default: null
},
inputId: {
type: String,
default: null
},
inputClass: {
type: [String, Object],
default: null
},
inputStyle: {
type: Object,
default: null
},
inputProps: {
type: null,
default: null
},
'aria-labelledby': {
type: String,
default: null
},
'aria-label': {
type: String,
default: null
}
},
css: {
classes
},
watch: {
isUnstyled: {
immediate: true,
handler(newValue) {
!newValue && loadStyle();
}
}
}
};
</script>

View File

@ -136,6 +136,11 @@ export interface ToggleButtonProps {
* @type {ToggleButtonPassThroughOptions} * @type {ToggleButtonPassThroughOptions}
*/ */
pt?: ToggleButtonPassThroughOptions; pt?: ToggleButtonPassThroughOptions;
/**
* When enabled, it removes component related styles in the core.
* @defaultValue false
*/
unstyled?: boolean;
} }
/** /**

View File

@ -1,6 +1,6 @@
<template> <template>
<div ref="container" v-ripple :class="buttonClass" @click="onClick($event)" v-bind="ptm('root')"> <div ref="container" v-ripple :class="cx('root')" @click="onClick($event)" v-bind="ptm('root')" :data-p-active="modelValue === true">
<span class="p-hidden-accessible" v-bind="ptm('hiddenInputWrapper')"> <span :class="cx('hiddenInputWrapper')" :style="sx('hiddenAccessible', isUnstyled)" v-bind="ptm('hiddenInputWrapper')" :data-p-hidden-accessible="true">
<input <input
:id="inputId" :id="inputId"
type="checkbox" type="checkbox"
@ -16,70 +16,21 @@
v-bind="{ ...inputProps, ...ptm('hiddenInput') }" v-bind="{ ...inputProps, ...ptm('hiddenInput') }"
/> />
</span> </span>
<slot name="icon" :value="modelValue" :class="iconClass"> <slot name="icon" :value="modelValue" :class="cx('icon')">
<span v-if="onIcon || offIcon" :class="iconClass" v-bind="ptm('icon')" /> <span v-if="onIcon || offIcon" :class="cx('icon')" v-bind="ptm('icon')" />
</slot> </slot>
<span class="p-button-label" v-bind="ptm('label')">{{ label }}</span> <span :class="cx('label')" v-bind="ptm('label')">{{ label }}</span>
</div> </div>
</template> </template>
<script> <script>
import BaseComponent from 'primevue/basecomponent';
import Ripple from 'primevue/ripple'; import Ripple from 'primevue/ripple';
import BaseToggleButton from './BaseToggleButton.vue';
export default { export default {
name: 'ToggleButton', name: 'ToggleButton',
extends: BaseComponent, extends: BaseToggleButton,
emits: ['update:modelValue', 'change', 'click', 'focus', 'blur'], emits: ['update:modelValue', 'change', 'click', 'focus', 'blur'],
props: {
modelValue: Boolean,
onIcon: String,
offIcon: String,
onLabel: {
type: String,
default: 'Yes'
},
offLabel: {
type: String,
default: 'No'
},
iconPos: {
type: String,
default: 'left'
},
disabled: {
type: Boolean,
default: false
},
tabindex: {
type: Number,
default: null
},
inputId: {
type: String,
default: null
},
inputClass: {
type: [String, Object],
default: null
},
inputStyle: {
type: Object,
default: null
},
inputProps: {
type: null,
default: null
},
'aria-labelledby': {
type: String,
default: null
},
'aria-label': {
type: String,
default: null
}
},
outsideClickListener: null, outsideClickListener: null,
data() { data() {
return { return {
@ -128,27 +79,6 @@ export default {
} }
}, },
computed: { computed: {
buttonClass() {
return [
'p-button p-togglebutton p-component',
{
'p-focus': this.focused,
'p-button-icon-only': this.hasIcon && !this.hasLabel,
'p-disabled': this.disabled,
'p-highlight': this.modelValue === true
}
];
},
iconClass() {
return [
this.modelValue ? this.onIcon : this.offIcon,
'p-button-icon',
{
'p-button-icon-left': this.iconPos === 'left' && this.label,
'p-button-icon-right': this.iconPos === 'right' && this.label
}
];
},
hasLabel() { hasLabel() {
return this.onLabel && this.onLabel.length > 0 && this.offLabel && this.offLabel.length > 0; return this.onLabel && this.onLabel.length > 0 && this.offLabel && this.offLabel.length > 0;
}, },