Add form support to `ToggleButton`

pull/6632/head
Mert Sincan 2024-10-18 17:26:05 +01:00
parent cff6d40f0d
commit 044eacead1
3 changed files with 14 additions and 22 deletions

View File

@ -5,8 +5,9 @@
:class="cx('root')" :class="cx('root')"
:tabindex="tabindex" :tabindex="tabindex"
:disabled="disabled" :disabled="disabled"
:aria-pressed="modelValue" :aria-pressed="d_value"
@click="onChange" @click="onChange"
@blur="onBlur"
v-bind="getPTOptions('root')" v-bind="getPTOptions('root')"
:aria-labelledby="ariaLabelledby" :aria-labelledby="ariaLabelledby"
:data-p-checked="active" :data-p-checked="active"
@ -14,8 +15,8 @@
> >
<span :class="cx('content')" v-bind="getPTOptions('content')"> <span :class="cx('content')" v-bind="getPTOptions('content')">
<slot> <slot>
<slot name="icon" :value="modelValue" :class="cx('icon')"> <slot name="icon" :value="d_value" :class="cx('icon')">
<span v-if="onIcon || offIcon" :class="[cx('icon'), modelValue ? onIcon : offIcon]" v-bind="getPTOptions('icon')" /> <span v-if="onIcon || offIcon" :class="[cx('icon'), d_value ? onIcon : offIcon]" v-bind="getPTOptions('icon')" />
</slot> </slot>
<span :class="cx('label')" v-bind="getPTOptions('label')">{{ label }}</span> <span :class="cx('label')" v-bind="getPTOptions('label')">{{ label }}</span>
</slot> </slot>
@ -32,7 +33,7 @@ export default {
name: 'ToggleButton', name: 'ToggleButton',
extends: BaseToggleButton, extends: BaseToggleButton,
inheritAttrs: false, inheritAttrs: false,
emits: ['update:modelValue', 'change'], emits: ['change'],
methods: { methods: {
getPTOptions(key) { getPTOptions(key) {
const _ptm = key === 'root' ? this.ptmi : this.ptm; const _ptm = key === 'root' ? this.ptmi : this.ptm;
@ -46,20 +47,23 @@ export default {
}, },
onChange(event) { onChange(event) {
if (!this.disabled && !this.readonly) { if (!this.disabled && !this.readonly) {
this.$emit('update:modelValue', !this.modelValue); this.updateValue(!this.d_value, event);
this.$emit('change', event); this.$emit('change', event);
} }
},
onBlur(event) {
this.formField.onBlur?.(event);
} }
}, },
computed: { computed: {
active() { active() {
return this.modelValue === true; return this.d_value === true;
}, },
hasLabel() { hasLabel() {
return isNotEmpty(this.onLabel) && isNotEmpty(this.offLabel); return isNotEmpty(this.onLabel) && isNotEmpty(this.offLabel);
}, },
label() { label() {
return this.hasLabel ? (this.modelValue ? this.onLabel : this.offLabel) : '&nbsp;'; return this.hasLabel ? (this.d_value ? this.onLabel : this.offLabel) : '&nbsp;';
} }
}, },
directives: { directives: {

View File

@ -106,7 +106,7 @@ const classes = {
'p-togglebutton p-component', 'p-togglebutton p-component',
{ {
'p-togglebutton-checked': instance.active, 'p-togglebutton-checked': instance.active,
'p-invalid': props.invalid 'p-invalid': instance.$invalid
} }
], ],
content: 'p-togglebutton-content', content: 'p-togglebutton-content',

View File

@ -1,15 +1,11 @@
<script> <script>
import BaseComponent from '@primevue/core/basecomponent'; import BaseEditableHolder from '@primevue/core/baseeditableholder';
import ToggleSwitchStyle from 'primevue/toggleswitch/style'; import ToggleSwitchStyle from 'primevue/toggleswitch/style';
export default { export default {
name: 'BaseToggleSwitch', name: 'BaseToggleSwitch',
extends: BaseComponent, extends: BaseEditableHolder,
props: { props: {
modelValue: {
type: null,
default: false
},
trueValue: { trueValue: {
type: null, type: null,
default: true default: true
@ -18,14 +14,6 @@ export default {
type: null, type: null,
default: false default: false
}, },
invalid: {
type: Boolean,
default: false
},
disabled: {
type: Boolean,
default: false
},
readonly: { readonly: {
type: Boolean, type: Boolean,
default: false default: false