2 lines
11 KiB
JavaScript
2 lines
11 KiB
JavaScript
import e,{Component as t}from"react";import{DomHandler as n,UniqueComponentId as r,classNames as a,ObjectUtils as i}from"primereact/utils";import{Ripple as o}from"primereact/ripple";function l(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function s(e){if(Array.isArray(e))return l(e)}function c(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function u(e,t){if(e){if("string"==typeof e)return l(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?l(e,t):void 0}}function p(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function d(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function f(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function b(e,t,n){return t&&f(e.prototype,t),n&&f(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e}function h(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function v(e,t){return v=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},v(e,t)}function y(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&v(e,t)}function m(e){return m="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},m(e)}function w(e,t){if(t&&("object"===m(t)||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return d(e)}function k(e){return k=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},k(e)}function g(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function I(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function T(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?I(Object(n),!0).forEach((function(t){g(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):I(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function O(e){var t=S();return function(){var n,r=k(e);if(t){var a=k(this).constructor;n=Reflect.construct(r,arguments,a)}else n=r.apply(this,arguments);return w(this,n)}}function S(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}var C=function(e){y(r,t);var n=O(r);function r(){return h(this,r),n.apply(this,arguments)}return b(r)}();g(C,"defaultProps",{header:null,headerTemplate:null,leftIcon:null,rightIcon:null,closable:!1,disabled:!1,style:null,className:null,headerStyle:null,headerClassName:null,contentStyle:null,contentClassName:null});var x=function(l){y(v,t);var f=O(v);function v(e){var t;h(this,v),t=f.call(this,e);var n={id:e.id,backwardIsDisabled:!0,forwardIsDisabled:!1,hiddenTabs:[]};return t.props.onTabChange||(n=T(T({},n),{},{activeIndex:e.activeIndex})),t.state=n,t.navBackward=t.navBackward.bind(d(t)),t.navForward=t.navForward.bind(d(t)),t.onScroll=t.onScroll.bind(d(t)),t}return b(v,[{key:"getActiveIndex",value:function(){return this.props.onTabChange?this.props.activeIndex:this.state.activeIndex}},{key:"isSelected",value:function(e){return e===this.getActiveIndex()}},{key:"shouldTabRender",value:function(e,t){return e&&e.type===C&&this.state.hiddenTabs.every((function(e){return e!==t}))}},{key:"findVisibleActiveTab",value:function(t){var n=this,r=e.Children.map(this.props.children,(function(e,t){if(n.shouldTabRender(e,t))return{tab:e,index:t}}));return r.find((function(e){return!e.tab.props.disabled&&e.index>=t}))||r.reverse().find((function(e){return!e.tab.props.disabled&&t>e.index}))}},{key:"onTabHeaderClose",value:function(e,t){var n,r=this,a=[].concat(s(n=this.state.hiddenTabs)||c(n)||u(n)||p(),[t]);this.setState({hiddenTabs:a},(function(){var n=r.findVisibleActiveTab(t);n&&r.onTabHeaderClick(e,n.tab,n.index)})),this.props.onTabClose&&this.props.onTabClose({originalEvent:e,index:t}),e.preventDefault()}},{key:"onTabHeaderClick",value:function(e,t,n){t.props.disabled||(this.props.onTabChange?this.props.onTabChange({originalEvent:e,index:n}):this.setState({activeIndex:n})),this.updateScrollBar(n),e.preventDefault()}},{key:"onKeyDown",value:function(e,t,n){"Enter"===e.code&&this.onTabHeaderClick(e,t,n)}},{key:"updateInkBar",value:function(){var e=this.getActiveIndex(),t=this["tab_".concat(e)];this.inkbar.style.width=n.getWidth(t)+"px",this.inkbar.style.left=n.getOffset(t).left-n.getOffset(this.nav).left+"px"}},{key:"updateScrollBar",value:function(e){var t=this["tab_".concat(e)];t&&t.scrollIntoView({block:"nearest"})}},{key:"updateButtonState",value:function(){var e=this.content,t=e.scrollLeft,r=e.scrollWidth,a=n.getWidth(e);this.setState({backwardIsDisabled:0===t}),this.setState({forwardIsDisabled:t===r-a})}},{key:"onScroll",value:function(e){this.props.scrollable&&this.updateButtonState(),e.preventDefault()}},{key:"getVisibleButtonWidths",value:function(){return[this.prevBtn,this.nextBtn].reduce((function(e,t){return t?e+n.getWidth(t):e}),0)}},{key:"navBackward",value:function(){var e=this.content,t=n.getWidth(e)-this.getVisibleButtonWidths(),r=e.scrollLeft-t;e.scrollLeft=r<=0?0:r}},{key:"navForward",value:function(){var e=this.content,t=n.getWidth(e)-this.getVisibleButtonWidths(),r=e.scrollLeft+t,a=e.scrollWidth-t;e.scrollLeft=r>=a?a:r}},{key:"reset",value:function(){var e={backwardIsDisabled:!0,forwardIsDisabled:!1,hiddenTabs:[]};this.props.onTabChange?this.props.onTabChange({index:this.props.activeIndex}):e=T(T({},e),{},{activeIndex:this.props.activeIndex}),this.setState(e)}},{key:"componentDidMount",value:function(){this.state.id||this.setState({id:r()}),this.updateInkBar()}},{key:"componentDidUpdate",value:function(e){this.updateInkBar(),e.activeIndex!==this.props.activeIndex&&this.updateScrollBar(this.props.activeIndex)}},{key:"renderTabHeader",value:function(t,n){var r=this,l=this.isSelected(n),s=T(T({},t.props.headerStyle||{}),t.props.style||{}),c=a("p-unselectable-text",{"p-tabview-selected p-highlight":l,"p-disabled":t.props.disabled},t.props.headerClassName,t.props.className),u=this.state.id+"_header_"+n,p=this.state.id+"_content_"+n,d=t.props.disabled?null:0,f=t.props.leftIcon&&e.createElement("i",{className:t.props.leftIcon}),b=e.createElement("span",{className:"p-tabview-title"},t.props.header),h=t.props.rightIcon&&e.createElement("i",{className:t.props.rightIcon}),v=e.createElement("a",{role:"tab",className:"p-tabview-nav-link",onClick:function(e){return r.onTabHeaderClick(e,t,n)},id:u,onKeyDown:function(e){return r.onKeyDown(e,t,n)},"aria-controls":p,"aria-selected":l,tabIndex:d},f,b,h,t.props.closable&&e.createElement("i",{className:"p-tabview-close pi pi-times",onClick:function(e){return r.onTabHeaderClose(e,n)}}),e.createElement(o,null));t.props.headerTemplate&&(v=i.getJSXElement(t.props.headerTemplate,{className:"p-tabview-nav-link",titleClassName:"p-tabview-title",onClick:function(e){return r.onTabHeaderClick(e,t,n)},onKeyDown:function(e){return r.onKeyDown(e,t,n)},leftIconElement:f,titleElement:b,rightIconElement:h,element:v,props:this.props,index:n,selected:l,ariaControls:p}));return e.createElement("li",{ref:function(e){return r["tab_".concat(n)]=e},className:c,style:s,role:"presentation"},v)}},{key:"renderTabHeaders",value:function(){var t=this;return e.Children.map(this.props.children,(function(e,n){if(t.shouldTabRender(e,n))return t.renderTabHeader(e,n)}))}},{key:"renderNavigator",value:function(){var t=this,n=this.renderTabHeaders();return e.createElement("div",{ref:function(e){return t.content=e},id:this.props.id,className:"p-tabview-nav-content",style:this.props.style,onScroll:this.onScroll},e.createElement("ul",{ref:function(e){return t.nav=e},className:"p-tabview-nav",role:"tablist"},n,e.createElement("li",{ref:function(e){return t.inkbar=e},className:"p-tabview-ink-bar"})))}},{key:"renderContent",value:function(){var t=this,n=e.Children.map(this.props.children,(function(e,n){if(t.shouldTabRender(e,n)&&(!t.props.renderActiveOnly||t.isSelected(n)))return t.createContent(e,n)}));return e.createElement("div",{className:"p-tabview-panels"},n)}},{key:"createContent",value:function(t,n){var r=this.isSelected(n),i=T(T({},t.props.contentStyle||{}),t.props.style||{}),o=a(t.props.contentClassName,t.props.className,"p-tabview-panel",{"p-hidden":!r});return e.createElement("div",{id:this.state.id+"_content_"+n,"aria-labelledby":this.state.id+"_header_"+n,"aria-hidden":!r,className:o,style:i,role:"tabpanel"},this.props.renderActiveOnly?r&&t.props.children:t.props.children)}},{key:"renderPrevButton",value:function(){var t=this;return this.props.scrollable&&!this.state.backwardIsDisabled?e.createElement("button",{ref:function(e){return t.prevBtn=e},className:"p-tabview-nav-prev p-tabview-nav-btn p-link",onClick:this.navBackward,type:"button"},e.createElement("span",{className:"pi pi-chevron-left"}),e.createElement(o,null)):null}},{key:"renderNextButton",value:function(){var t=this;if(this.props.scrollable&&!this.state.forwardIsDisabled)return e.createElement("button",{ref:function(e){return t.nextBtn=e},className:"p-tabview-nav-next p-tabview-nav-btn p-link",onClick:this.navForward,type:"button"},e.createElement("span",{className:"pi pi-chevron-right"}),e.createElement(o,null))}},{key:"render",value:function(){var t=a("p-tabview p-component",this.props.className,{"p-tabview-scrollable":this.props.scrollable}),n=this.renderNavigator(),r=this.renderContent(),i=this.renderPrevButton(),o=this.renderNextButton();return e.createElement("div",{className:t},e.createElement("div",{className:"p-tabview-nav-container"},i,n,o),r)}}]),v}();g(x,"defaultProps",{id:null,activeIndex:0,style:null,className:null,renderActiveOnly:!0,onTabChange:null,onTabClose:null,scrollable:!1});export{C as TabPanel,x as TabView};
|