UNPKG

tanstack-shadcn-table

Version:

A powerful, feature-rich React table component built on top of TanStack Table v8 with shadcn/ui styling. Optimized bundle size with 55% reduction through peer dependencies.

2 lines (1 loc) 4.67 kB
import{jsx as e,jsxs as l,Fragment as a}from"react/jsx-runtime";import t,{useRef as n,useEffect as o,useCallback as r,useState as i,useMemo as s}from"react";import{I as c,S as u,b as f,c as d,d as m,e as h}from"./ui-components-bd9e2eb9.js";import{s as p,a as v}from"./security-16060df4.js";import{c as b,d as x,a as $}from"./utils-49e5594d.js";function g(e,l,a){const t=n(null),r=n(a),i=n(void 0);o(()=>{r.current=a},[a]),o(()=>(t.current&&clearTimeout(t.current),t.current=setTimeout(()=>{i.current!==e&&(i.current=e,r.current(e))},l),()=>{t.current&&clearTimeout(t.current)}),[e,l])}function N(e=10,l=1e3){const a=n([]);return r(()=>{const t=Date.now();return a.current=a.current.filter(e=>l>t-e),a.current.length<e&&(a.current.push(t),!0)},[e,l])}function y({value:l,onChange:a,debounce:t=500,type:u="text",maxLength:f=1e3,...d}){const[m,h]=i(l),v=N(),b=n(l),x=n(a);o(()=>{x.current=a},[a]),o(()=>{if("number"===u){const e=+l;b.current=isNaN(e)||null==l||""===l?void 0:e}else b.current=l;h(l)},[l,u]),g(m,t,r(e=>{if(!v())return;let l=e;if("text"===u||"search"===u)l="string"==typeof e?p(e):e+"";else if("number"===u){const a=+e;l=isNaN(a)||null==e||""===e?void 0:Math.max(-1e6,Math.min(1e6,a))}l!==b.current&&(b.current=l,x.current(l))},[u,v]));const $=r(e=>{const l=e.target.value;l.length>f||("number"!==u||""===l||/^-?\d*\.?\d*$/.test(l))&&h(l)},[f,u]),y=s(()=>d.autoComplete||"off",[d.autoComplete]);return e(c,{...d,type:u,maxLength:f,value:m,onChange:$,autoComplete:y,spellCheck:!1})}function L({column:n,translations:o=x}){const r=$(o),i=n.getFilterValue(),s=n.columnDef?.filter,c=t.useMemo(()=>"range"===s?.type?[]:Array.from(n.getFacetedUniqueValues().keys()).sort().slice(0,5e3).map(e=>"string"==typeof e?p(e):e),[n.getFacetedUniqueValues(),s?.type]);if(!s?.field)return null;const g=t.useMemo(()=>s?.type?v(i,s.type):i,[i,s?.type]);if("range"===s?.type){const{minPlaceholder:a=r("filters.min"),maxPlaceholder:t=r("filters.max"),showLimit:o=!1,minLimit:i,maxLimit:c}=s??{},u="number"==typeof i?Math.max(-1e6,Math.min(1e6,i)):i,f="number"==typeof c?Math.max(-1e6,Math.min(1e6,c)):c;return l("div",{className:"flex space-x-1 w-full",children:[e(y,{type:"number",min:"faceted"===u?+(n.getFacetedMinMaxValues()?.[0]??""):u,max:"faceted"===f?+(n.getFacetedMinMaxValues()?.[1]??""):f,value:g?.[0]??"",onChange:e=>{const l=""===e||null==e?void 0:+e;void 0!==l&&(isNaN(l)||-1e6>l||l>1e6)||n.setFilterValue(e=>{if(void 0!==l||void 0!==e?.[1])return[l,e?.[1]]})},placeholder:`${p(a)} ${o?"faceted"===u?void 0!==n.getFacetedMinMaxValues()?.[0]?`(${n.getFacetedMinMaxValues()?.[0]})`:"":`(${u})`:""}`,className:"flex-1 min-w-16 h-8"}),e(y,{type:"number",min:"faceted"===u?+(n.getFacetedMinMaxValues()?.[0]??""):u,max:"faceted"===f?+(n.getFacetedMinMaxValues()?.[1]??""):f,value:g?.[1]??"",onChange:e=>{const l=""===e||null==e?void 0:+e;void 0!==l&&(isNaN(l)||-1e6>l||l>1e6)||n.setFilterValue(e=>{if(void 0!==l||void 0!==e?.[0])return[e?.[0],l]})},placeholder:`${p(t)} ${o?"faceted"===f?void 0!==n.getFacetedMinMaxValues()?.[1]?`(${n.getFacetedMinMaxValues()?.[1]})`:"":`(${f})`:""}`,className:"flex-1 min-w-16 h-8"})]})}if("select"===s?.type){const{options:a,optionLabel:t="label",optionValue:o="value",allLabel:i=r("filters.all")}=s??{},v=(a??c).slice(0,1e3);return l(u,{value:g??"all",onValueChange:e=>{const l=p(e);n.setFilterValue("all"===l?"":l)},children:[e(f,{className:"h-8 w-full",children:e(d,{placeholder:p(i)})}),l(m,{children:[e(h,{value:"all",children:p(i)}),v.map((l,a)=>{const n=p("object"==typeof l?l[o]+"":l+""),r=p("object"==typeof l?l[t]+"":l+"");return e(h,{value:n,children:r},`${n}-${a}`)})]})]})}if("boolean"===s?.type){const{trueLabel:a=r("filters.true"),falseLabel:t=r("filters.false"),allLabel:o=r("filters.all")}=s??{};return l(u,{value:g??"all",onValueChange:e=>{["all","true","false"].includes(e)&&n.setFilterValue("all"===e?"":e)},children:[e(f,{className:"h-8 w-full",children:e(d,{placeholder:p(o)})}),l(m,{children:[e(h,{value:"all",children:p(o)}),e(h,{value:"true",children:p(a)}),e(h,{value:"false",children:p(t)})]})]})}if("text"===s?.type)return l(a,{children:[s?.showList&&e("datalist",{id:n.id+"list",children:c.slice(0,100).map((l,a)=>e("option",{value:l},`${l}-${a}`))}),e(y,{className:b("w-full h-8",s?.className),onChange:e=>{const l=p(e+"");n.setFilterValue(l)},placeholder:`${p(s?.field)} ${s?.showTotal?`(${Math.min(n.getFacetedUniqueValues().size,9999)})`:""}`,type:"text",value:g??"",list:s?.showList?n.id+"list":void 0,maxLength:1e3})]});if("custom"===s?.type){const{component:l,...a}=s;return e(l,{column:n,...a})}return null}var C=Object.freeze({__proto__:null,default:L});export{y as D,L as F,N as a,C as f,g as u};