UNPKG

@gitlab/ui

Version:
161 lines (150 loc) 4.96 kB
import uniqueId from 'lodash/uniqueId'; import GlCollapse from '../collapse/collapse'; import GlAnimatedChevronRightDownIcon from '../animated_icon/animated_chevron_right_down_icon'; import GlButton from '../button/button'; import { COLLAPSE_EVENT } from './constants'; import __vue_normalize__ from 'vue-runtime-helpers/dist/normalize-component.js'; var script = { name: 'GlAccordionItem', components: { GlCollapse, GlButton, GlAnimatedChevronRightDownIcon }, inject: ['defaultHeaderLevel', 'autoCollapse'], inheritAttrs: false, model: { prop: 'visible', event: 'input' }, props: { /** * Used to set the title of accordion link */ title: { type: String, required: true }, /** * Used to set the title of accordion link when the content is visible * */ titleVisible: { type: String, default: null, required: false }, /** * When set, it will ensure the accordion item is initially visible */ visible: { type: Boolean, default: false, required: false }, /** * The header tag used in the accordion (h1/h2/h3/h4/h5/h6). This overrides the value provided by GlAccordion. For accessibility this should be set to an appropriate value in the context where the accordion is used., */ headerLevel: { type: Number, required: false, default: null, validator(value) { return value > 0 && value <= 6; } }, /** * Additional CSS class(es) to be applied to the header */ headerClass: { type: [String, Object, Array], required: false, default: '' } }, data() { return { accordionItemId: uniqueId('accordion-item-'), localVisible: this.visible }; }, computed: { headerComponent() { const level = this.headerLevel || this.defaultHeaderLevel(); return `h${level}`; }, buttonTitle() { return this.localVisible && this.titleVisible ? this.titleVisible : this.title; } }, watch: { visible: { handler(newVisible) { this.localVisible = newVisible; this.checkAndCollapseSiblingAccordionItems(newVisible); } } }, created() { this.$emit('input', this.localVisible); }, mounted() { this.$parent.$el.addEventListener(COLLAPSE_EVENT, this.onParentCollapse); }, beforeDestroy() { this.$parent.$el.removeEventListener(COLLAPSE_EVENT, this.onParentCollapse); }, methods: { onParentCollapse(_ref) { let { detail: accordionItemId } = _ref; if (accordionItemId === this.accordionItemId) { return; } this.$emit('input', false); this.localVisible = false; }, onButtonClick() { const newLocalVisible = !this.localVisible; this.$emit('input', newLocalVisible); this.localVisible = newLocalVisible; this.checkAndCollapseSiblingAccordionItems(newLocalVisible); }, checkAndCollapseSiblingAccordionItems(newVisible) { if (this.autoCollapse() && newVisible) { this.$parent.$el.dispatchEvent(new CustomEvent(COLLAPSE_EVENT, { detail: this.accordionItemId })); } } } }; /* script */ const __vue_script__ = script; /* template */ var __vue_render__ = function () {var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;return _c('div',{staticClass:"gl-accordion-item"},[_c(_vm.headerComponent,{tag:"component",staticClass:"gl-accordion-item-header",class:_vm.headerClass},[_c('gl-button',{attrs:{"variant":"link","button-text-classes":"gl-flex","aria-expanded":_vm.localVisible ? 'true' : 'false',"aria-controls":_vm.accordionItemId},on:{"click":_vm.onButtonClick}},[_c('gl-animated-chevron-right-down-icon',{attrs:{"is-on":_vm.localVisible}}),_vm._v("\n "+_vm._s(_vm.buttonTitle)+"\n ")],1)],1),_vm._v(" "),_c('gl-collapse',{attrs:{"id":_vm.accordionItemId,"data-testid":("accordion-item-collapse-" + _vm.accordionItemId)},model:{value:(_vm.localVisible),callback:function ($$v) {_vm.localVisible=$$v;},expression:"localVisible"}},[_c('div',{staticClass:"gl-mt-3 gl-text-base"},[_vm._t("default")],2)])],1)}; var __vue_staticRenderFns__ = []; /* style */ const __vue_inject_styles__ = undefined; /* scoped */ const __vue_scope_id__ = undefined; /* module identifier */ const __vue_module_identifier__ = undefined; /* functional template */ const __vue_is_functional_template__ = false; /* style inject */ /* style inject SSR */ /* style inject shadow dom */ const __vue_component__ = /*#__PURE__*/__vue_normalize__( { render: __vue_render__, staticRenderFns: __vue_staticRenderFns__ }, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, false, undefined, undefined, undefined ); export { __vue_component__ as default };