Refactor #3965 - For SelectButton

pull/3997/head
Tuğçe Küçükoğlu 2023-05-25 17:09:22 +03:00
parent e562695287
commit 808b7625ac
3 changed files with 57 additions and 44 deletions

View File

@ -0,0 +1,41 @@
<script>
import BaseComponent from 'primevue/basecomponent';
const classes = {
root: ({ props }) => ['p-selectbutton p-buttonset p-component', { 'p-disabled': props.disabled }],
button: ({ instance, option }) => [
'p-button p-component',
{
'p-highlight': instance.isSelected(option),
'p-disabled': instance.isOptionDisabled(option)
}
],
label: 'p-button-label'
};
export default {
name: 'BaseSelectButton',
extends: BaseComponent,
props: {
modelValue: null,
options: Array,
optionLabel: null,
optionValue: null,
optionDisabled: null,
multiple: Boolean,
unselectable: {
type: Boolean,
default: false
},
disabled: Boolean,
dataKey: null,
'aria-labelledby': {
type: String,
default: null
}
},
css: {
classes
}
};
</script>

View File

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

View File

@ -1,5 +1,5 @@
<template> <template>
<div ref="container" :class="containerClass" role="group" :aria-labelledby="ariaLabelledby" v-bind="ptm('root')"> <div ref="container" :class="cx('root')" role="group" :aria-labelledby="ariaLabelledby" v-bind="ptm('root')">
<div <div
v-for="(option, i) of options" v-for="(option, i) of options"
:key="getOptionRenderKey(option)" :key="getOptionRenderKey(option)"
@ -9,47 +9,31 @@
:role="multiple ? 'checkbox' : 'radio'" :role="multiple ? 'checkbox' : 'radio'"
:aria-checked="isSelected(option)" :aria-checked="isSelected(option)"
:aria-disabled="optionDisabled" :aria-disabled="optionDisabled"
:class="getButtonClass(option, i)" :class="cx('button', { option })"
@click="onOptionSelect($event, option, i)" @click="onOptionSelect($event, option, i)"
@keydown="onKeydown($event, option, i)" @keydown="onKeydown($event, option, i)"
@focus="onFocus($event)" @focus="onFocus($event)"
@blur="onBlur($event, option)" @blur="onBlur($event, option)"
v-bind="getPTOptions(option, 'button')" v-bind="getPTOptions(option, 'button')"
:data-p-highlight="isSelected(option)"
:data-p-disabled="isOptionDisabled(option)"
> >
<slot name="option" :option="option" :index="i"> <slot name="option" :option="option" :index="i" :class="cx('label')">
<span class="p-button-label" v-bind="getPTOptions(option, 'label')">{{ getOptionLabel(option) }}</span> <span :class="cx('label')" v-bind="getPTOptions(option, 'label')">{{ getOptionLabel(option) }}</span>
</slot> </slot>
</div> </div>
</div> </div>
</template> </template>
<script> <script>
import BaseComponent from 'primevue/basecomponent';
import Ripple from 'primevue/ripple'; import Ripple from 'primevue/ripple';
import { DomHandler, ObjectUtils } from 'primevue/utils'; import { DomHandler, ObjectUtils } from 'primevue/utils';
import BaseSelectButton from './BaseSelectButton.vue';
export default { export default {
name: 'SelectButton', name: 'SelectButton',
extends: BaseComponent, extends: BaseSelectButton,
emits: ['update:modelValue', 'focus', 'blur', 'change'], emits: ['update:modelValue', 'focus', 'blur', 'change'],
props: {
modelValue: null,
options: Array,
optionLabel: null,
optionValue: null,
optionDisabled: null,
multiple: Boolean,
unselectable: {
type: Boolean,
default: false
},
disabled: Boolean,
dataKey: null,
'aria-labelledby': {
type: String,
default: null
}
},
data() { data() {
return { return {
focusedIndex: 0 focusedIndex: 0
@ -60,11 +44,11 @@ export default {
}, },
methods: { methods: {
defaultTabIndexes() { defaultTabIndexes() {
let opts = DomHandler.find(this.$refs.container, '.p-button'); let opts = DomHandler.find(this.$refs.container, '[data-pc-section="button"]');
let firstHighlight = DomHandler.findSingle(this.$refs.container, '.p-highlight'); let firstHighlight = DomHandler.findSingle(this.$refs.container, '[data-p-highlight="true"]');
for (let i = 0; i < opts.length; i++) { for (let i = 0; i < opts.length; i++) {
if ((DomHandler.hasClass(opts[i], 'p-highlight') && ObjectUtils.equals(opts[i], firstHighlight)) || (firstHighlight === null && i == 0)) { if ((DomHandler.getAttribute(opts[i], 'data-p-highlight') === true && ObjectUtils.equals(opts[i], firstHighlight)) || (firstHighlight === null && i == 0)) {
this.focusedIndex = i; this.focusedIndex = i;
} }
} }
@ -189,26 +173,9 @@ export default {
} }
this.$emit('blur', event, option); this.$emit('blur', event, option);
},
getButtonClass(option) {
return [
'p-button p-component',
{
'p-highlight': this.isSelected(option),
'p-disabled': this.isOptionDisabled(option)
}
];
} }
}, },
computed: { computed: {
containerClass() {
return [
'p-selectbutton p-buttonset p-component',
{
'p-disabled': this.disabled
}
];
},
equalityKey() { equalityKey() {
return this.optionValue ? null : this.dataKey; return this.optionValue ? null : this.dataKey;
} }