Refactor #3965 - For Button

pull/4011/head
Tuğçe Küçükoğlu 2023-05-30 17:05:57 +03:00
parent 6dbd3ec18d
commit bffef92cb3
3 changed files with 211 additions and 121 deletions

View File

@ -0,0 +1,196 @@
<script>
import BaseComponent from 'primevue/basecomponent';
import { useStyle } from 'primevue/usestyle';
const styles = `
.p-button {
margin: 0;
display: inline-flex;
cursor: pointer;
user-select: none;
align-items: center;
vertical-align: bottom;
text-align: center;
overflow: hidden;
position: relative;
}
.p-button-label {
flex: 1 1 auto;
}
.p-button-icon-right {
order: 1;
}
.p-button:disabled {
cursor: default;
}
.p-button-icon-only {
justify-content: center;
}
.p-button-icon-only .p-button-label {
visibility: hidden;
width: 0;
flex: 0 0 auto;
}
.p-button-vertical {
flex-direction: column;
}
.p-button-icon-bottom {
order: 2;
}
.p-buttonset .p-button {
margin: 0;
}
.p-buttonset .p-button:not(:last-child) {
border-right: 0 none;
}
.p-buttonset .p-button:not(:first-of-type):not(:last-of-type) {
border-radius: 0;
}
.p-buttonset .p-button:first-of-type {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
.p-buttonset .p-button:last-of-type {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
.p-buttonset .p-button:focus {
position: relative;
z-index: 1;
}
`;
const classes = {
root: ({ instance, props }) => [
'p-button p-component',
{
'p-button-icon-only': instance.hasIcon && !props.label,
'p-button-vertical': (props.iconPos === 'top' || props.iconPos === 'bottom') && props.label,
'p-disabled': instance.$attrs.disabled || props.loading,
'p-button-loading': props.loading,
'p-button-loading-label-only': props.loading && !instance.hasIcon && props.label,
'p-button-link': props.link,
[`p-button-${props.severity}`]: props.severity,
'p-button-raised': props.raised,
'p-button-rounded': props.rounded,
'p-button-text': props.text,
'p-button-outlined': props.outlined,
'p-button-sm': props.size === 'small',
'p-button-lg': props.size === 'large',
'p-button-plain': props.plain
}
],
loadingIcon: 'p-button-loading-icon pi-spin',
icon: ({ props }) => [
'p-button-icon',
{
'p-button-icon-left': props.iconPos === 'left' && props.label,
'p-button-icon-right': props.iconPos === 'right' && props.label,
'p-button-icon-top': props.iconPos === 'top' && props.label,
'p-button-icon-bottom': props.iconPos === 'bottom' && props.label
}
],
label: 'p-button-label',
badge: ({ props }) => [
'p-badge p-component',
{
'p-badge-no-gutter': props.badge && String(props.badge).length === 1
}
]
};
const { load: loadStyle } = useStyle(styles, { id: 'primevue_button_style', manual: true });
export default {
name: 'BaseButton',
extends: BaseComponent,
props: {
label: {
type: String,
default: null
},
icon: {
type: String,
default: null
},
iconPos: {
type: String,
default: 'left'
},
iconClass: {
type: String,
default: null
},
badge: {
type: String,
default: null
},
badgeClass: {
type: String,
default: null
},
loading: {
type: Boolean,
default: false
},
loadingIcon: {
type: String,
default: undefined
},
link: {
type: Boolean,
default: false
},
severity: {
type: String,
default: null
},
raised: {
type: Boolean,
default: false
},
rounded: {
type: Boolean,
default: false
},
text: {
type: Boolean,
default: false
},
outlined: {
type: Boolean,
default: false
},
size: {
type: String,
default: null
},
plain: {
type: Boolean,
default: false
}
},
css: {
classes,
loadStyle
},
provide() {
return {
$parentInstance: this
};
}
};
</script>

View File

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

View File

@ -1,139 +1,28 @@
<template>
<button v-ripple :class="buttonClass" type="button" :aria-label="defaultAriaLabel" :disabled="disabled" v-bind="ptm('root')">
<button v-ripple :class="cx('root')" type="button" :aria-label="defaultAriaLabel" :disabled="disabled" v-bind="ptm('root')" data-pc-name="button" :data-pc-severity="severity">
<slot>
<slot v-if="loading" name="loadingicon" :class="loadingIconStyleClass">
<span v-if="loadingIcon" :class="[loadingIconStyleClass, loadingIcon]" v-bind="ptm('loadingIcon')" />
<SpinnerIcon v-else :class="loadingIconStyleClass" spin v-bind="ptm('loadingIcon')" />
<slot v-if="loading" name="loadingicon" :class="[cx('loadingIcon'), cx('icon')]">
<span v-if="loadingIcon" :class="[cx('loadingIcon'), cx('icon'), loadingIcon]" v-bind="ptm('loadingIcon')" />
<SpinnerIcon v-else :class="[cx('loadingIcon'), cx('icon')]" spin v-bind="ptm('loadingIcon')" />
</slot>
<slot v-else name="icon" :class="iconStyleClass">
<span v-if="icon" :class="[iconStyleClass, icon]" v-bind="ptm('icon')"></span>
<slot v-else name="icon" :class="cx('icon')">
<span v-if="icon" :class="[cx('icon'), icon]" v-bind="ptm('icon')"></span>
</slot>
<span class="p-button-label" v-bind="ptm('label')">{{ label || '&nbsp;' }}</span>
<span v-if="badge" :class="badgeStyleClass" v-bind="ptm('badge')">{{ badge }}</span>
<span :class="cx('label')" v-bind="ptm('label')">{{ label || '&nbsp;' }}</span>
<span v-if="badge" :class="[cx('badge'), badgeClass]" v-bind="ptm('badge')">{{ badge }}</span>
</slot>
</button>
</template>
<script>
import BaseComponent from 'primevue/basecomponent';
import SpinnerIcon from 'primevue/icons/spinner';
import Ripple from 'primevue/ripple';
import BaseButton from './BaseButton.vue';
export default {
name: 'Button',
extends: BaseComponent,
props: {
label: {
type: String,
default: null
},
icon: {
type: String,
default: null
},
iconPos: {
type: String,
default: 'left'
},
iconClass: {
type: String,
default: null
},
badge: {
type: String,
default: null
},
badgeClass: {
type: String,
default: null
},
loading: {
type: Boolean,
default: false
},
loadingIcon: {
type: String,
default: undefined
},
link: {
type: Boolean,
default: false
},
severity: {
type: String,
default: null
},
raised: {
type: Boolean,
default: false
},
rounded: {
type: Boolean,
default: false
},
text: {
type: Boolean,
default: false
},
outlined: {
type: Boolean,
default: false
},
size: {
type: String,
default: null
},
plain: {
type: Boolean,
default: false
}
},
extends: BaseButton,
computed: {
buttonClass() {
return [
'p-button p-component',
{
'p-button-icon-only': this.hasIcon && !this.label,
'p-button-vertical': (this.iconPos === 'top' || this.iconPos === 'bottom') && this.label,
'p-disabled': this.$attrs.disabled || this.loading,
'p-button-loading': this.loading,
'p-button-loading-label-only': this.loading && !this.hasIcon && this.label,
'p-button-link': this.link,
[`p-button-${this.severity}`]: this.severity,
'p-button-raised': this.raised,
'p-button-rounded': this.rounded,
'p-button-text': this.text,
'p-button-outlined': this.outlined,
'p-button-sm': this.size === 'small',
'p-button-lg': this.size === 'large',
'p-button-plain': this.plain
}
];
},
iconStyleClass() {
return [
'p-button-icon',
this.iconClass,
{
'p-button-icon-left': this.iconPos === 'left' && this.label,
'p-button-icon-right': this.iconPos === 'right' && this.label,
'p-button-icon-top': this.iconPos === 'top' && this.label,
'p-button-icon-bottom': this.iconPos === 'bottom' && this.label
}
];
},
loadingIconStyleClass() {
return ['p-button-loading-icon pi-spin', this.iconStyleClass];
},
badgeStyleClass() {
return [
'p-badge p-component',
this.badgeClass,
{
'p-badge-no-gutter': this.badge && String(this.badge).length === 1
}
];
},
disabled() {
return this.$attrs.disabled || this.loading;
},