primevue-mirror/components/fieldset/Fieldset.vue

103 lines
2.8 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2022-09-14 11:26:01 +00:00
<fieldset :class="['p-fieldset p-component', { 'p-fieldset-toggleable': toggleable }]">
2022-09-06 12:03:37 +00:00
<legend class="p-fieldset-legend">
2022-09-14 11:26:01 +00:00
<slot v-if="!toggleable" name="legend">
<span :id="ariaId + '_header'" class="p-fieldset-legend-text">{{ legend }}</span>
2022-09-06 12:03:37 +00:00
</slot>
2022-09-14 11:26:01 +00:00
<a v-if="toggleable" :id="ariaId + '_header'" v-ripple tabindex="0" role="button" :aria-controls="ariaId + '_content'" :aria-expanded="!d_collapsed" :aria-label="toggleButtonProps || legend" @click="toggle" @keydown="onKeyDown">
2022-09-06 12:03:37 +00:00
<span :class="iconClass"></span>
<slot name="legend">
2022-09-14 11:26:01 +00:00
<span class="p-fieldset-legend-text">{{ legend }}</span>
2022-09-06 12:03:37 +00:00
</slot>
</a>
</legend>
<transition name="p-toggleable-content">
2022-09-14 11:26:01 +00:00
<div v-show="!d_collapsed" :id="ariaId + '_content'" class="p-toggleable-content" role="region" :aria-labelledby="ariaId + '_header'">
2022-09-06 12:03:37 +00:00
<div class="p-fieldset-content">
<slot></slot>
</div>
</div>
</transition>
</fieldset>
</template>
<script>
2022-09-14 11:26:01 +00:00
import { UniqueComponentId } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
import Ripple from 'primevue/ripple';
export default {
name: 'Fieldset',
emits: ['update:collapsed', 'toggle'],
props: {
legend: String,
toggleable: Boolean,
2022-09-14 11:26:01 +00:00
collapsed: Boolean,
toggleButtonProps: String
2022-09-06 12:03:37 +00:00
},
data() {
return {
2022-09-14 11:26:01 +00:00
d_collapsed: this.collapsed
};
2022-09-06 12:03:37 +00:00
},
watch: {
collapsed(newValue) {
this.d_collapsed = newValue;
}
},
methods: {
toggle(event) {
this.d_collapsed = !this.d_collapsed;
this.$emit('update:collapsed', this.d_collapsed);
this.$emit('toggle', {
originalEvent: event,
value: this.d_collapsed
});
2022-09-14 11:26:01 +00:00
},
onKeyDown(event) {
if (event.code === 'Enter' || event.code === 'Space') {
this.toggle(event);
event.preventDefault();
}
2022-09-06 12:03:37 +00:00
}
},
2022-09-14 11:26:01 +00:00
computed: {
iconClass() {
return [
'p-fieldset-toggler pi ',
{
'pi-minus': !this.d_collapsed,
'pi-plus': this.d_collapsed
}
];
2022-09-06 12:03:37 +00:00
},
ariaId() {
return UniqueComponentId();
}
},
directives: {
2022-09-14 11:26:01 +00:00
ripple: Ripple
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>
<style>
.p-fieldset-legend > a,
.p-fieldset-legend > span {
display: flex;
align-items: center;
justify-content: center;
}
.p-fieldset-toggleable .p-fieldset-legend a {
cursor: pointer;
user-select: none;
overflow: hidden;
position: relative;
text-decoration: none;
}
.p-fieldset-legend-text {
line-height: 1;
}
</style>