Add form support to `SelectButton`

pull/6632/head
Mert Sincan 2024-10-18 17:22:56 +01:00
parent a193c1e550
commit 2785def67c
3 changed files with 13 additions and 18 deletions

View File

@ -1,12 +1,11 @@
<script> <script>
import BaseComponent from '@primevue/core/basecomponent'; import BaseEditableHolder from '@primevue/core/baseeditableholder';
import SelectButtonStyle from 'primevue/selectbutton/style'; import SelectButtonStyle from 'primevue/selectbutton/style';
export default { export default {
name: 'BaseSelectButton', name: 'BaseSelectButton',
extends: BaseComponent, extends: BaseEditableHolder,
props: { props: {
modelValue: null,
options: Array, options: Array,
optionLabel: null, optionLabel: null,
optionValue: null, optionValue: null,
@ -16,11 +15,6 @@ export default {
type: Boolean, type: Boolean,
default: true default: true
}, },
invalid: {
type: Boolean,
default: false
},
disabled: Boolean,
dataKey: null, dataKey: null,
ariaLabelledby: { ariaLabelledby: {
type: String, type: String,

View File

@ -2,6 +2,7 @@
<div :class="cx('root')" role="group" :aria-labelledby="ariaLabelledby" v-bind="ptmi('root')"> <div :class="cx('root')" role="group" :aria-labelledby="ariaLabelledby" v-bind="ptmi('root')">
<template v-for="(option, index) of options" :key="getOptionRenderKey(option)"> <template v-for="(option, index) of options" :key="getOptionRenderKey(option)">
<ToggleButton <ToggleButton
:name="$formName"
:modelValue="isSelected(option)" :modelValue="isSelected(option)"
:onLabel="getOptionLabel(option)" :onLabel="getOptionLabel(option)"
:offLabel="getOptionLabel(option)" :offLabel="getOptionLabel(option)"
@ -21,7 +22,7 @@
</template> </template>
<script> <script>
import { resolveFieldData, equals } from '@primeuix/utils/object'; import { equals, resolveFieldData } from '@primeuix/utils/object';
import Ripple from 'primevue/ripple'; import Ripple from 'primevue/ripple';
import ToggleButton from 'primevue/togglebutton'; import ToggleButton from 'primevue/togglebutton';
import BaseSelectButton from './BaseSelectButton.vue'; import BaseSelectButton from './BaseSelectButton.vue';
@ -30,7 +31,7 @@ export default {
name: 'SelectButton', name: 'SelectButton',
extends: BaseSelectButton, extends: BaseSelectButton,
inheritAttrs: false, inheritAttrs: false,
emits: ['update:modelValue', 'change'], emits: ['change'],
methods: { methods: {
getOptionLabel(option) { getOptionLabel(option) {
return this.optionLabel ? resolveFieldData(option, this.optionLabel) : option; return this.optionLabel ? resolveFieldData(option, this.optionLabel) : option;
@ -59,13 +60,13 @@ export default {
let newValue; let newValue;
if (this.multiple) { if (this.multiple) {
if (selected) newValue = this.modelValue.filter((val) => !equals(val, optionValue, this.equalityKey)); if (selected) newValue = this.d_value.filter((val) => !equals(val, optionValue, this.equalityKey));
else newValue = this.modelValue ? [...this.modelValue, optionValue] : [optionValue]; else newValue = this.d_value ? [...this.d_value, optionValue] : [optionValue];
} else { } else {
newValue = selected ? null : optionValue; newValue = selected ? null : optionValue;
} }
this.$emit('update:modelValue', newValue); this.updateValue(newValue, event);
this.$emit('change', { event: event, value: newValue }); this.$emit('change', { event: event, value: newValue });
}, },
isSelected(option) { isSelected(option) {
@ -73,8 +74,8 @@ export default {
let optionValue = this.getOptionValue(option); let optionValue = this.getOptionValue(option);
if (this.multiple) { if (this.multiple) {
if (this.modelValue) { if (this.d_value) {
for (let val of this.modelValue) { for (let val of this.d_value) {
if (equals(val, optionValue, this.equalityKey)) { if (equals(val, optionValue, this.equalityKey)) {
selected = true; selected = true;
break; break;
@ -82,7 +83,7 @@ export default {
} }
} }
} else { } else {
selected = equals(this.modelValue, optionValue, this.equalityKey); selected = equals(this.d_value, optionValue, this.equalityKey);
} }
return selected; return selected;

View File

@ -37,10 +37,10 @@ const theme = ({ dt }) => `
`; `;
const classes = { const classes = {
root: ({ props }) => [ root: ({ instance }) => [
'p-selectbutton p-component', 'p-selectbutton p-component',
{ {
'p-invalid': props.invalid 'p-invalid': instance.$invalid // @todo: check
} }
] ]
}; };