primevue-mirror/components/lib/radiobutton/RadioButton.vue

114 lines
3.1 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-05-05 15:20:34 +00:00
<div :class="containerClass" @click="onClick($event)" v-bind="ptm('root')">
<div class="p-hidden-accessible" v-bind="ptm('hiddenInputWrapper')">
2022-09-14 11:26:01 +00:00
<input
ref="input"
:id="inputId"
type="radio"
:class="inputClass"
:style="inputStyle"
:name="name"
:checked="checked"
:disabled="disabled"
:value="value"
:aria-labelledby="ariaLabelledby"
:aria-label="ariaLabel"
@focus="onFocus"
@blur="onBlur"
2023-05-05 15:20:34 +00:00
v-bind="ptm('hiddenInput')"
2022-09-14 11:26:01 +00:00
/>
2022-09-06 12:03:37 +00:00
</div>
2023-05-05 15:20:34 +00:00
<div ref="box" :class="['p-radiobutton-box', { 'p-highlight': checked, 'p-disabled': disabled, 'p-focus': focused }]" v-bind="{ ...inputProps, ...ptm('input') }">
<div class="p-radiobutton-icon" v-bind="ptm('icon')"></div>
2022-09-06 12:03:37 +00:00
</div>
</div>
</template>
<script>
2023-05-05 15:20:34 +00:00
import BaseComponent from 'primevue/basecomponent';
2022-09-14 11:26:01 +00:00
import { ObjectUtils } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
export default {
name: 'RadioButton',
2023-05-05 15:20:34 +00:00
extends: BaseComponent,
2022-09-06 12:03:37 +00:00
emits: ['click', 'update:modelValue', 'change', 'focus', 'blur'],
props: {
2022-09-14 11:26:01 +00:00
value: null,
2022-09-06 12:03:37 +00:00
modelValue: null,
name: {
type: String,
default: null
},
disabled: {
type: Boolean,
default: false
},
2022-09-14 11:26:01 +00:00
inputId: {
type: String,
default: null
},
inputClass: {
type: [String, Object],
2022-09-14 11:26:01 +00:00
default: null
},
inputStyle: {
type: Object,
2022-09-14 11:26:01 +00:00
default: null
},
inputProps: {
type: null,
default: null
},
2022-09-06 12:03:37 +00:00
'aria-labelledby': {
type: String,
2022-09-14 11:26:01 +00:00
default: null
2022-09-06 12:03:37 +00:00
},
'aria-label': {
type: String,
default: null
}
},
data() {
return {
focused: false
};
},
methods: {
onClick(event) {
if (!this.disabled) {
this.$emit('click', event);
this.$emit('update:modelValue', this.value);
this.$refs.input.focus();
if (!this.checked) {
this.$emit('change', event);
}
}
},
onFocus(event) {
this.focused = true;
this.$emit('focus', event);
},
onBlur(event) {
this.focused = false;
this.$emit('blur', event);
}
},
computed: {
checked() {
return this.modelValue != null && ObjectUtils.equals(this.modelValue, this.value);
},
containerClass() {
return [
2022-09-14 11:26:01 +00:00
'p-radiobutton p-component',
{
2022-09-06 12:03:37 +00:00
'p-radiobutton-checked': this.checked,
'p-radiobutton-disabled': this.disabled,
'p-radiobutton-focused': this.focused
2022-09-14 11:26:01 +00:00
}
];
2022-09-06 12:03:37 +00:00
}
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
</script>