UNPKG

@table-library/react-table-library

Version:
3 lines (2 loc) 5.11 kB
import{S as e,a as r}from"./sort-d783cdb4.js";export{S as SortIconPositions,a as SortToggleType}from"./sort-d783cdb4.js";import{o as t}from"./styles-79dd03ea.js";import*as o from"react";import{u as n,H as s}from"./HeaderCell-3621006f.js";import i from"clsx";import{jsx as c}from"@emotion/react";import{d as u}from"./defineProperty-9f9de5d0.js";import{B as l,I as p}from"./IconChevronSingleDown-7d5db393.js";import{s as f}from"./slicedToArray-c92cae3a.js";import{u as v,a as d,b as g}from"./useSyncControlledState-367beeb4.js";import"./toConsumableArray-25e5c43c.js";import"./unsupportedIterableToArray-dc74e326.js";function y(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);r&&(o=o.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,o)}return t}function m(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?y(Object(t),!0).forEach((function(r){u(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):y(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var h=function(r){var t=r.sort,n=r.sortKey,s=r.sortIcon,a=void 0===s?{}:s,u=r.children,p=t.state,f=t.fns,v=m(m({},t.options.sortIcon),a),d=v.position===e.Prefix,g=v.position===e.Suffix,y=function(e,r,t,n,s,i){var c={height:"".concat(t),width:"".concat(t)};return e.sortKey===r&&e.reverse?i?o.cloneElement(i,m({},c)):null:e.sortKey!==r||e.reverse?n?o.cloneElement(n,m({},c)):null:s?o.cloneElement(s,m({},c)):null}(p,n,v.size,v.iconDefault,v.iconUp,v.iconDown);return c(l,{className:i({active:p.sortKey===n}),onClick:function(){return f.onToggleSort({sortKey:n})}},d&&y&&c("span",{style:{marginRight:v.margin}},y),c("div",{title:"string"==typeof u?u:""},u),g&&y&&c("span",{style:{marginLeft:v.margin}},y))},w=["sortKey","sortIcon","children"],O=o.memo((function(e){var r=e.sortKey,o=e.sortIcon,i=void 0===o?{}:o,a=e.children,u=t(e,w),l=n();if(!l)throw new Error("No Sort Context. No return value from useSort provided to Table component.");return c(s,u,c(h,{sort:l,sortKey:r,sortIcon:i},a))}));function b(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);r&&(o=o.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,o)}return t}function j(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?b(Object(t),!0).forEach((function(r){u(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):b(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var P=function(e,t){switch(t.type){case"TOGGLE_SORT":return function(e,t){var o=t.payload.value.sortKey===e.sortKey,n=e.reverse;if(o&&n&&t.payload.options.sortToggleType===r.AlternateWithReset)return{sortKey:"NONE",reverse:!1};var s=o&&!n;return j(j({},t.payload.value),{},{reverse:s})}(e,t);case"SET":return function(e,r){return j(j({},e),r.payload)}(e,t);default:throw new Error}},T={sortKey:"NONE",reverse:!1},x={position:e.Suffix,margin:"4px",size:"14px",iconDefault:c((function(e){var r=e.width,t=e.height,o=e.viewBox,n=e.strokeWidth;return c("svg",{id:"svg-icon-chevron-single-up-down","data-name":"svg-icon-chevron-single-up-down","data-testid":"svg-icon-chevron-single-up-down",xmlns:"http://www.w3.org/2000/svg",width:r||"36rem",height:t||"36rem",viewBox:o||"0 0 36 36",strokeWidth:n||"0rem"},c("polygon",{points:"36 12 36 15 18 6 0 15 0 12 18 3 36 12"}),c("polygon",{points:"0 24 0 21 18 30 36 21 36 24 18 33 0 24"}))}),null),iconUp:c((function(e){var r=e.width,t=e.height,o=e.viewBox,n=e.strokeWidth;return c("svg",{id:"svg-icon-chevron-single-up","data-name":"svg-icon-chevron-single-up","data-testid":"svg-icon-chevron-single-up",xmlns:"http://www.w3.org/2000/svg",width:r||"36rem",height:t||"36rem",viewBox:o||"0 0 36 36",strokeWidth:n||"0rem"},c("polygon",{points:"36 21 36 24 18 15 0 24 0 21 18 12 36 21"}))}),null),iconDown:c(p,null)},E={isServer:!1,sortToggleType:r.Alternate,sortIcon:x,isRecursive:!0},D=function(e,r,t,n){var s=null!=r&&r.state?j(j({},T),r.state):j({},T),i=null!=r&&r.onChange?r.onChange:function(){},c=v(P,s,[],[i],n),a=f(c,2),u=a[0],l=a[1],p=o.useCallback((function(e){return l({type:"TOGGLE_SORT",payload:{value:e,options:t}})}),[t,l]);d(s,u,(function(){return l({type:"SET",payload:s})}));var y=o.useCallback((function(e,r,t){var o=r[u.sortKey]||function(e){return e};return(u.reverse?function(e){return o(e).reverse()}:o)(e).reduce((function(e,o){return t&&o.nodes?e.concat(j(j({},o),{},{nodes:y(o.nodes,r,t)})):e.concat(o)}),[])}),[u]),m=o.useMemo((function(){return{onToggleSort:p}}),[p]);g("sort",n,u);var h=j(j(j({},E),t),{},{sortIcon:j(j({},x),t?t.sortIcon:{})}),w=j(j({},u),{},{sortFn:y});return{state:w,fns:m,options:h,modifier:function(e){return h.isServer?e:w.sortFn(e,h.sortFns,h.isRecursive)},components:{HeaderCellSort:O}}};export{O as HeaderCellSort,D as useSort}; //# sourceMappingURL=sort.js.map