UNPKG

@anoki/fse-ui

Version:

FSE UI components library

3 lines (2 loc) • 8.3 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require('./components/ui/Table/Table.css');const t=require("./index.cjs244.js"),z=require("react"),d=require("./index.cjs246.js");;/* empty css */const se=require("./index.cjs67.js"),ae=require("./index.cjs198.js"),ne=require("./index.cjs199.js"),ie=require("./index.cjs191.js"),le=require("./index.cjs200.js");function oe(e){const s=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const a in e)if(a!=="default"){const i=Object.getOwnPropertyDescriptor(e,a);Object.defineProperty(s,a,i.get?i:{enumerable:!0,get:()=>e[a]})}}return s.default=e,Object.freeze(s)}const f=oe(z),U=f.forwardRef(({className:e,columns:s,data:a,pagination:i,initialCurrentPage:R=1,resultsPerPage:E=10,defaultSortDirection:b="desc",setSelectedSort:l,initialSortConfig:h,serverSidePagination:m=!1,totalItems:N,hasNextPage:T,hasPreviousPage:C,onNextPage:p,onPreviousPage:k,onPageChange:I,...L},D)=>{const[o,M]=f.useState(h||{key:s[0].key||s[0].label,direction:b}),[n,q]=f.useState(R);z.useEffect(()=>{h&&M(h)},[h]);const c=f.useMemo(()=>[...a].sort((x,y)=>{const u=x[o.key],j=y[o.key],_=s.find(r=>(r.key||r.label)===o.key);if(typeof u=="string"&&typeof j=="string"){if((_==null?void 0:_.type)==="date"){const r=W=>{const[X,Y]=W.split(" - "),[Z,K,S]=X.split("/").map(Number),[ee,te]=Y.split(":").map(Number);return new Date(S,K-1,Z,ee,te)},g=r(u),F=r(j);return o.direction==="asc"?g.getTime()-F.getTime():F.getTime()-g.getTime()}if((_==null?void 0:_.type)==="number"){const r=parseFloat(u),g=parseFloat(j);return o.direction==="asc"?r-g:g-r}return o.direction==="asc"?u.localeCompare(j):j.localeCompare(u)}return A(u)&&A(j)?o.direction==="asc"?u.firstLine.localeCompare(j.firstLine):j.firstLine.localeCompare(u.firstLine):0}),[a,o,s]),v=x=>{const y={key:x,direction:o.key===x&&o.direction==="asc"?"desc":"asc"};M(y),l&&l(y)},w=i&&!m?(n-1)*E:0,P=i&&!m?Math.min(w+E,c.length):c.length,Q=m?c:c.slice(w,P);return t.jsxRuntimeExports.jsxs("div",{className:"position-relative w-full",children:[t.jsxRuntimeExports.jsxs("table",{ref:D,className:d.clsx("table",e),role:"table",...L,children:[t.jsxRuntimeExports.jsx(B,{className:"table__header",children:t.jsxRuntimeExports.jsx($,{children:s.map((x,y)=>t.jsxRuntimeExports.jsx(O,{config:x,sortConfig:o,onSort:()=>x.sortable&&v(x.key||x.label),variant:L.variant},`${x.label}-${y}`))})}),t.jsxRuntimeExports.jsx(H,{className:"table__body",children:Q.map((x,y)=>t.jsxRuntimeExports.jsx($,{className:"table__row",children:s.map((u,j)=>{const _=u.key||u.label,r=x[_];return t.jsxRuntimeExports.jsx(V,{content:r||"",type:r&&A(r)?"double-line":r&&G(r)?"action":void 0},`${_}-${j}`)})},y))})]}),i&&t.jsxRuntimeExports.jsx(J,{currentPage:n,totalResults:m?N||0:c.length,currentResultsPerPage:a.length,resultsPerPage:E,setCurrentPage:q,onPageChange:I,serverSide:m,totalItems:N,hasNextPage:T,hasPreviousPage:C,onNextPage:p,onPreviousPage:k})]})});U.displayName="Table";const B=f.forwardRef(({className:e,children:s,...a},i)=>t.jsxRuntimeExports.jsx("thead",{ref:i,className:d.clsx("[&_tr]:border-b-0",e),...a,children:s}));B.displayName="TableHeader";const H=f.forwardRef(({className:e,children:s,...a},i)=>t.jsxRuntimeExports.jsx("tbody",{ref:i,className:d.clsx("[&_tr:last-child]:border-0",e),...a,children:s}));H.displayName="TableBody";const $=f.forwardRef(({className:e,children:s,...a},i)=>t.jsxRuntimeExports.jsx("tr",{ref:i,className:d.clsx("border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted",e),...a,children:s}));$.displayName="TableRow";const O=f.forwardRef(({className:e,config:s,sortConfig:a,onSort:i,...R},E)=>{var N;const b=a.key===(s.key||s.label),l=b?a.direction:null,h=()=>l==="asc"?"ascending":"descending",m=T=>{switch(T){case"info":return t.jsxRuntimeExports.jsx(le.Info,{});case"download":return t.jsxRuntimeExports.jsx(ie.Download,{});default:return T}};return t.jsxRuntimeExports.jsx("th",{ref:E,className:d.clsx("table__cell table__cell--header",{"table__cell--sortable":s.sortable,"table__cell--action":s.action},e),onClick:s.sortable?i:s.action,"aria-label":s.ariaLabel,"aria-sort":b?h():void 0,scope:s.scope||"col",...R,children:t.jsxRuntimeExports.jsxs("div",{className:"table__cell--header--content",children:[t.jsxRuntimeExports.jsx("div",{children:t.jsxRuntimeExports.jsx(se.ActionIconText,{icon:m(s.icon),noIcon:!m(s.icon),textLabel:s.label,iconPosition:s.iconPosition,handleIconAction:()=>{s.iconAction&&s.iconAction()},popupInfo:s.popupInfo,positionPopUp:(N=s.popupInfo)==null?void 0:N.positionPopUp,classNameText:R.variant&&"fw-bold text-slate-28 fs-custom md-fs-6"})}),s.sortable&&t.jsxRuntimeExports.jsxs("span",{className:"sort-indicator",children:[t.jsxRuntimeExports.jsx(ae.TriangleUp,{filled:b&&l==="desc",className:d.clsx("sort-indicator__triangle",{active:b&&l==="desc"})}),t.jsxRuntimeExports.jsx(ne.TriangleDown,{filled:b&&l==="asc",className:d.clsx("sort-indicator__triangle",{active:b&&l==="asc"})})]})]})})});O.displayName="TableHead";const re=({data:e})=>t.jsxRuntimeExports.jsxs(t.jsxRuntimeExports.Fragment,{children:[t.jsxRuntimeExports.jsx("div",{className:"table__cell--firstLine",children:e.firstLine}),t.jsxRuntimeExports.jsx("div",{className:"table__cell--secondLine",children:e.secondLine})]}),ce=({data:e})=>e.type==="link"&&e.link?t.jsxRuntimeExports.jsx("a",{href:e.link,"aria-label":e.ariaLabel,children:e.label}):t.jsxRuntimeExports.jsx("button",{onClick:e.action,"aria-label":e.ariaLabel,children:e.label}),A=e=>typeof e=="object"&&e!==null&&"firstLine"in e&&"secondLine"in e,G=e=>typeof e=="object"&&e!==null&&"label"in e&&"type"in e,xe=({content:e})=>typeof e=="string"?t.jsxRuntimeExports.jsx(t.jsxRuntimeExports.Fragment,{children:e}):A(e)?t.jsxRuntimeExports.jsx(re,{data:e}):G(e)?t.jsxRuntimeExports.jsx(ce,{data:e}):null,V=f.forwardRef(({className:e,content:s,type:a,...i},R)=>t.jsxRuntimeExports.jsx("td",{ref:R,className:d.clsx("table__cell",{"table__cell--double-line":a==="double-line","table__cell--action":a==="action"},e),...i,children:t.jsxRuntimeExports.jsx(xe,{content:s})}));V.displayName="TableCell";const J=({currentPage:e=1,totalResults:s,resultsPerPage:a,currentResultsPerPage:i,setCurrentPage:R,onPageChange:E,className:b,serverSide:l=!1,totalItems:h,hasNextPage:m,hasPreviousPage:N,onNextPage:T,onPreviousPage:C})=>{const p=Math.ceil(l?(h||s)/a:s/a),k=n=>{R(n),E&&E(n)},I=()=>{const n=e+1;l&&T&&T(),k(n)},L=()=>{const n=e-1;l&&C&&C(),k(n)},D=l?!m:e===p,o=l?!N:e===1,M=()=>{const n=[];if(p<=5)for(let c=1;c<=p;c++)n.push(c);else{n.push(1);let c=Math.max(2,e-1),v=Math.min(p-1,e+1);e<=3?v=4:e>=p-2&&(c=p-3),c>2&&n.push("...");for(let w=c;w<=v;w++)n.push(w);v<p-1&&n.push("..."),n.push(p)}return n};return t.jsxRuntimeExports.jsxs("div",{className:d.clsx("pagination",b),children:[t.jsxRuntimeExports.jsxs("div",{className:"pagination__info",children:["Stai visualizzando"," ",t.jsxRuntimeExports.jsx("span",{className:"pagination__info__resultsPerPage",children:l?i:e===p&&s%a!==0?s%a:a})," ","di ",l&&h||s," risultati"]}),t.jsxRuntimeExports.jsxs("div",{className:"pagination__nav",children:[t.jsxRuntimeExports.jsx("button",{className:d.clsx("pagination__button",o&&"pagination__button--disabled"),onClick:L,disabled:o,"aria-label":"Previous page",children:t.jsxRuntimeExports.jsx("svg",{className:"pagination__button-arrow",viewBox:"0 0 24 24","aria-hidden":"true",role:"presentation",children:t.jsxRuntimeExports.jsx("path",{d:"M15 18l-6-6 6-6"})})}),M().map((n,q)=>n==="..."?t.jsxRuntimeExports.jsx("span",{className:"pagination__ellipsis",children:"..."},`ellipsis-${q}`):t.jsxRuntimeExports.jsx("button",{className:d.clsx("pagination__button",e===n&&"pagination__button--active"),onClick:()=>k(n),disabled:n===e,children:n},n)),t.jsxRuntimeExports.jsx("button",{className:d.clsx("pagination__button",D&&"pagination__button--disabled"),onClick:I,disabled:D,"aria-label":"Next page",children:t.jsxRuntimeExports.jsx("svg",{className:"pagination__button-arrow",viewBox:"0 0 24 24","aria-hidden":"true",role:"presentation",children:t.jsxRuntimeExports.jsx("path",{d:"M9 6l6 6-6 6"})})})]})]})};exports.Pagination=J;exports.Table=U;exports.TableBody=H;exports.TableCell=V;exports.TableHead=O;exports.TableHeader=B;exports.TableRow=$; //# sourceMappingURL=index.cjs73.js.map