primevue
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primevue) [ • 10.4 kB
JavaScript
import e from"primevue/basecomponent";import t from"primevue/icons/chevronleft";import n from"primevue/icons/chevronright";import a from"primevue/ripple";import{UniqueComponentId as i,DomHandler as o}from"primevue/utils";import{resolveDirective as r,openBlock as s,createElementBlock as l,mergeProps as d,createElementVNode as c,withDirectives as p,renderSlot as b,createBlock as h,resolveDynamicComponent as v,createCommentVNode as u,Fragment as f,renderList as g,toDisplayString as x,vShow as m}from"vue";var T={name:"TabView",extends:e,emits:["update:activeIndex","tab-change","tab-click"],props:{activeIndex:{type:Number,default:0},lazy:{type:Boolean,default:!1},scrollable:{type:Boolean,default:!1},tabindex:{type:Number,default:0},selectOnFocus:{type:Boolean,default:!1},previousButtonProps:{type:null,default:null},nextButtonProps:{type:null,default:null},prevIcon:{type:String,default:void 0},nextIcon:{type:String,default:void 0}},data(){return{id:this.$attrs.id,d_activeIndex:this.activeIndex,isPrevButtonDisabled:!0,isNextButtonDisabled:!1}},watch:{"$attrs.id":function(e){this.id=e||i()},activeIndex(e){this.d_activeIndex=e,this.scrollInView({index:e})}},mounted(){this.id=this.id||i(),this.updateInkBar(),this.scrollable&&this.updateButtonState()},updated(){this.updateInkBar()},methods:{isTabPanel:e=>"TabPanel"===e.type.name,isTabActive(e){return this.d_activeIndex===e},getTabProp:(e,t)=>e.props?e.props[t]:void 0,getKey(e,t){return this.getTabProp(e,"header")||t},getTabHeaderActionId(e){return`${this.id}_${e}_header_action`},getTabContentId(e){return`${this.id}_${e}_content`},getTabPT(e,t){return this.ptmo(this.getTabProp(e,"pt"),t,{props:e.props,parent:{props:this.$props,state:this.$data}})},onScroll(e){this.scrollable&&this.updateButtonState(),e.preventDefault()},onPrevButtonClick(){const e=this.$refs.content,t=o.getWidth(e),n=e.scrollLeft-t;e.scrollLeft=n<=0?0:n},onNextButtonClick(){const e=this.$refs.content,t=o.getWidth(e)-this.getVisibleButtonWidths(),n=e.scrollLeft+t,a=e.scrollWidth-t;e.scrollLeft=n>=a?a:n},onTabClick(e,t,n){this.changeActiveIndex(e,t,n),this.$emit("tab-click",{originalEvent:e,index:n})},onTabKeyDown(e,t,n){switch(e.code){case"ArrowLeft":this.onTabArrowLeftKey(e);break;case"ArrowRight":this.onTabArrowRightKey(e);break;case"Home":this.onTabHomeKey(e);break;case"End":this.onTabEndKey(e);break;case"PageDown":this.onPageDownKey(e);break;case"PageUp":this.onPageUpKey(e);break;case"Enter":case"Space":this.onTabEnterKey(e,t,n)}},onTabArrowRightKey(e){const t=this.findNextHeaderAction(e.target.parentElement);t?this.changeFocusedTab(e,t):this.onTabHomeKey(e),e.preventDefault()},onTabArrowLeftKey(e){const t=this.findPrevHeaderAction(e.target.parentElement);t?this.changeFocusedTab(e,t):this.onTabEndKey(e),e.preventDefault()},onTabHomeKey(e){const t=this.findFirstHeaderAction();this.changeFocusedTab(e,t),e.preventDefault()},onTabEndKey(e){const t=this.findLastHeaderAction();this.changeFocusedTab(e,t),e.preventDefault()},onPageDownKey(e){this.scrollInView({index:this.$refs.nav.children.length-2}),e.preventDefault()},onPageUpKey(e){this.scrollInView({index:0}),e.preventDefault()},onTabEnterKey(e,t,n){this.changeActiveIndex(e,t,n),e.preventDefault()},findNextHeaderAction(e,t=!1){const n=t?e:e.nextElementSibling;return n?o.hasClass(n,"p-disabled")||o.hasClass(n,"p-tabview-ink-bar")?this.findNextHeaderAction(n):o.findSingle(n,".p-tabview-header-action"):null},findPrevHeaderAction(e,t=!1){const n=t?e:e.previousElementSibling;return n?o.hasClass(n,"p-disabled")||o.hasClass(n,"p-tabview-ink-bar")?this.findPrevHeaderAction(n):o.findSingle(n,".p-tabview-header-action"):null},findFirstHeaderAction(){return this.findNextHeaderAction(this.$refs.nav.firstElementChild,!0)},findLastHeaderAction(){return this.findPrevHeaderAction(this.$refs.nav.lastElementChild,!0)},changeActiveIndex(e,t,n){this.getTabProp(t,"disabled")||this.d_activeIndex===n||(this.d_activeIndex=n,this.$emit("update:activeIndex",n),this.$emit("tab-change",{originalEvent:e,index:n}),this.scrollInView({index:n}))},changeFocusedTab(e,t){if(t&&(o.focus(t),this.scrollInView({element:t}),this.selectOnFocus)){const n=parseInt(t.parentElement.dataset.index,10),a=this.tabs[n];this.changeActiveIndex(e,a,n)}},scrollInView({element:e,index:t=-1}){const n=e||this.$refs.nav.children[t];n&&n.scrollIntoView&&n.scrollIntoView({block:"nearest"})},updateInkBar(){let e=this.$refs.nav.children[this.d_activeIndex];this.$refs.inkbar.style.width=o.getWidth(e)+"px",this.$refs.inkbar.style.left=o.getOffset(e).left-o.getOffset(this.$refs.nav).left+"px"},updateButtonState(){const e=this.$refs.content,{scrollLeft:t,scrollWidth:n}=e,a=o.getWidth(e);this.isPrevButtonDisabled=0===t,this.isNextButtonDisabled=parseInt(t)===n-a},getVisibleButtonWidths(){const{prevBtn:e,nextBtn:t}=this.$refs;return[e,t].reduce(((e,t)=>t?e+o.getWidth(t):e),0)},getTabHeaderClass(e,t){return["p-tabview-header",this.getTabProp(e,"headerClass"),{"p-highlight":this.d_activeIndex===t,"p-disabled":this.getTabProp(e,"disabled")}]},getTabContentClass(e){return["p-tabview-panel",this.getTabProp(e,"contentClass")]}},computed:{contentClasses(){return["p-tabview p-component",{"p-tabview-scrollable":this.scrollable}]},tabs(){return this.$slots.default().reduce(((e,t)=>(this.isTabPanel(t)?e.push(t):t.children&&t.children instanceof Array&&t.children.forEach((t=>{this.isTabPanel(t)&&e.push(t)})),e)),[])},prevButtonAriaLabel(){return this.$primevue.config.locale.aria?this.$primevue.config.locale.aria.previous:void 0},nextButtonAriaLabel(){return this.$primevue.config.locale.aria?this.$primevue.config.locale.aria.next:void 0}},directives:{ripple:a},components:{ChevronLeftIcon:t,ChevronRightIcon:n}};const y=["tabindex","aria-label"],w=["data-index"],P=["id","tabindex","aria-disabled","aria-selected","aria-controls","onClick","onKeydown"],I=["tabindex","aria-label"],k=["aria-labelledby"];!function(e,t){void 0===t&&(t={});var n=t.insertAt;if(e&&"undefined"!=typeof document){var a=document.head||document.getElementsByTagName("head")[0],i=document.createElement("style");i.type="text/css","top"===n&&a.firstChild?a.insertBefore(i,a.firstChild):a.appendChild(i),i.styleSheet?i.styleSheet.cssText=e:i.appendChild(document.createTextNode(e))}}("\n.p-tabview-nav-container {\n position: relative;\n}\n.p-tabview-scrollable .p-tabview-nav-container {\n overflow: hidden;\n}\n.p-tabview-nav-content {\n overflow-x: auto;\n overflow-y: hidden;\n scroll-behavior: smooth;\n scrollbar-width: none;\n overscroll-behavior: contain auto;\n}\n.p-tabview-nav {\n display: flex;\n margin: 0;\n padding: 0;\n list-style-type: none;\n flex: 1 1 auto;\n}\n.p-tabview-header-action {\n cursor: pointer;\n user-select: none;\n display: flex;\n align-items: center;\n position: relative;\n text-decoration: none;\n overflow: hidden;\n}\n.p-tabview-ink-bar {\n display: none;\n z-index: 1;\n}\n.p-tabview-header-action:focus {\n z-index: 1;\n}\n.p-tabview-title {\n line-height: 1;\n white-space: nowrap;\n}\n.p-tabview-nav-btn {\n position: absolute;\n top: 0;\n z-index: 2;\n height: 100%;\n display: flex;\n align-items: center;\n justify-content: center;\n}\n.p-tabview-nav-prev {\n left: 0;\n}\n.p-tabview-nav-next {\n right: 0;\n}\n.p-tabview-nav-content::-webkit-scrollbar {\n display: none;\n}\n"),T.render=function(e,t,n,a,i,o){const T=r("ripple");return s(),l("div",d({class:o.contentClasses},e.ptm("root")),[c("div",d({class:"p-tabview-nav-container"},e.ptm("navcontainer")),[n.scrollable&&!i.isPrevButtonDisabled?p((s(),l("button",d({key:0,ref:"prevBtn",type:"button",class:"p-tabview-nav-prev p-tabview-nav-btn p-link",tabindex:n.tabindex,"aria-label":o.prevButtonAriaLabel,onClick:t[0]||(t[0]=(...e)=>o.onPrevButtonClick&&o.onPrevButtonClick(...e))},{...n.previousButtonProps,...e.ptm("prevbutton")}),[b(e.$slots,"previcon",{},(()=>[(s(),h(v(n.prevIcon?"span":"ChevronLeftIcon"),d({"aria-hidden":"true",class:n.prevIcon},e.ptm("previcon")),null,16,["class"]))]))],16,y)),[[T]]):u("",!0),c("div",d({ref:"content",class:"p-tabview-nav-content",onScroll:t[1]||(t[1]=(...e)=>o.onScroll&&o.onScroll(...e))},e.ptm("navcontent")),[c("ul",d({ref:"nav",class:"p-tabview-nav",role:"tablist"},e.ptm("nav")),[(s(!0),l(f,null,g(o.tabs,((e,t)=>(s(),l("li",d({key:o.getKey(e,t),style:o.getTabProp(e,"headerStyle"),class:o.getTabHeaderClass(e,t),role:"presentation","data-index":t},{...o.getTabProp(e,"headerProps"),...o.getTabPT(e,"root"),...o.getTabPT(e,"header")}),[p((s(),l("a",d({id:o.getTabHeaderActionId(t),class:"p-tabview-nav-link p-tabview-header-action",tabindex:o.getTabProp(e,"disabled")||!o.isTabActive(t)?-1:n.tabindex,role:"tab","aria-disabled":o.getTabProp(e,"disabled"),"aria-selected":o.isTabActive(t),"aria-controls":o.getTabContentId(t),onClick:n=>o.onTabClick(n,e,t),onKeydown:n=>o.onTabKeyDown(n,e,t)},{...o.getTabProp(e,"headerActionProps"),...o.getTabPT(e,"headeraction")}),[e.props&&e.props.header?(s(),l("span",d({key:0,class:"p-tabview-title"},o.getTabPT(e,"headertitle")),x(e.props.header),17)):u("",!0),e.children&&e.children.header?(s(),h(v(e.children.header),{key:1})):u("",!0)],16,P)),[[T]])],16,w)))),128)),c("li",d({ref:"inkbar",class:"p-tabview-ink-bar",role:"presentation","aria-hidden":"true"},e.ptm("inkbar")),null,16)],16)],16),n.scrollable&&!i.isNextButtonDisabled?p((s(),l("button",d({key:1,ref:"nextBtn",type:"button",class:"p-tabview-nav-next p-tabview-nav-btn p-link",tabindex:n.tabindex,"aria-label":o.nextButtonAriaLabel,onClick:t[2]||(t[2]=(...e)=>o.onNextButtonClick&&o.onNextButtonClick(...e))},{...n.nextButtonProps,...e.ptm("nextbutton")}),[b(e.$slots,"nexticon",{},(()=>[(s(),h(v(n.nextIcon?"span":"ChevronRightIcon"),d({"aria-hidden":"true",class:n.nextIcon},e.ptm("nexticon")),null,16,["class"]))]))],16,I)),[[T]]):u("",!0)],16),c("div",d({class:"p-tabview-panels"},e.ptm("panelcontainer")),[(s(!0),l(f,null,g(o.tabs,((e,t)=>(s(),l(f,{key:o.getKey(e,t)},[!n.lazy||o.isTabActive(t)?p((s(),l("div",d({key:0,style:o.getTabProp(e,"contentStyle"),class:o.getTabContentClass(e),role:"tabpanel","aria-labelledby":o.getTabHeaderActionId(t)},{...o.getTabProp(e,"contentProps"),...o.getTabPT(e,"root"),...o.getTabPT(e,"content")}),[(s(),h(v(e)))],16,k)),[[m,!!n.lazy||o.isTabActive(t)]]):u("",!0)],64)))),128))],16)],16)};export{T as default};