@xyo-network/react-shared 2.36.3 → 2.37.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,18 @@
1
1
  /// <reference types="react" />
2
2
  import { TableCellProps } from '@mui/material';
3
+ import { WithChildren } from '@xylabs/react-shared';
3
4
  import { To } from 'react-router-dom';
4
5
  export interface EllipsisTableCellProps extends TableCellProps {
6
+ /**
7
+ * Width of the table cell.
8
+ *
9
+ * Note: When using percentages, this value can be different than what you expect
10
+ * if used on a cell that is not the first cell in the first row.
11
+ */
12
+ width?: string | number;
13
+ href?: string;
14
+ to?: To;
5
15
  value?: string;
6
- to?: To | undefined;
7
- href?: string | undefined;
8
16
  }
9
- export declare const EllipsisTableCell: React.FC<EllipsisTableCellProps>;
17
+ export declare const EllipsisTableCell: React.FC<WithChildren<EllipsisTableCellProps>>;
10
18
  //# sourceMappingURL=EllipsisTableCell.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"EllipsisTableCell.d.ts","sourceRoot":"","sources":["../../../../src/components/TableCell/EllipsisTableCell.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAa,cAAc,EAAE,MAAM,eAAe,CAAA;AAGzD,OAAO,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAA;AAOrC,MAAM,WAAW,sBAAuB,SAAQ,cAAc;IAC5D,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,EAAE,CAAC,EAAE,EAAE,GAAG,SAAS,CAAA;IACnB,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC1B;AAED,eAAO,MAAM,iBAAiB,EAAE,KAAK,CAAC,EAAE,CAAC,sBAAsB,CAmF9D,CAAA"}
1
+ {"version":3,"file":"EllipsisTableCell.d.ts","sourceRoot":"","sources":["../../../../src/components/TableCell/EllipsisTableCell.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAqB,cAAc,EAAE,MAAM,eAAe,CAAA;AAEjE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AACnD,OAAO,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAA;AA8CrC,MAAM,WAAW,sBAAuB,SAAQ,cAAc;IAC5D;;;;;OAKG;IACH,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;IACvB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,EAAE,CAAC,EAAE,EAAE,CAAA;IACP,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED,eAAO,MAAM,iBAAiB,EAAE,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,sBAAsB,CAAC,CAkB5E,CAAA"}
@@ -1,61 +1,46 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { TableCell } from '@mui/material';
2
+ import { styled, TableCell } from '@mui/material';
3
3
  import { LinkEx } from '@xylabs/react-link';
4
- import { useEffect, useRef, useState } from 'react';
5
- import { getActualPaddingX } from '../../lib';
6
- import { findParent } from './findParent';
7
- import { getRemainingRowWidth } from './getRemainingRowWidth';
8
- import { getSmallestParentWidth } from './getSmallestParentWidth';
9
- export const EllipsisTableCell = ({ children, value, to, href, ...props }) => {
10
- const [calcCellWidth, setCalcCellWidth] = useState(0);
11
- const hashDivRef = useRef(null);
12
- useEffect(() => {
13
- const currentElement = hashDivRef.current?.parentElement;
14
- const cell = findParent('td', currentElement);
15
- const row = findParent('tr', currentElement);
16
- const checkWidth = (cell) => {
17
- const smallestParentWidth = getSmallestParentWidth(cell);
18
- if (smallestParentWidth && row) {
19
- const remainingWidth = getRemainingRowWidth(row);
20
- const actualPaddingX = getActualPaddingX(cell);
21
- const remainderWidth = smallestParentWidth - remainingWidth - actualPaddingX;
22
- cell.style.width = `${remainderWidth}`;
23
- setCalcCellWidth(remainderWidth);
24
- }
25
- };
26
- const onResize = () => {
27
- if (cell) {
28
- checkWidth(cell);
29
- }
30
- };
31
- if (cell) {
32
- checkWidth(cell);
33
- window.addEventListener('resize', onResize);
34
- row?.addEventListener('resize', onResize);
35
- }
36
- return () => {
37
- window.removeEventListener('resize', onResize);
38
- row?.removeEventListener('resize', onResize);
39
- };
40
- }, [hashDivRef]);
41
- return (_jsx(TableCell, { ...props, children: _jsx("div", { ref: hashDivRef, children: children ? (_jsx("span", { style: {
42
- display: 'block',
43
- maxWidth: calcCellWidth,
44
- overflow: 'hidden',
45
- textOverflow: 'ellipsis',
46
- whiteSpace: 'nowrap',
47
- }, children: children })) : href || to ? (_jsx(LinkEx, { style: {
48
- display: 'block',
49
- maxWidth: calcCellWidth,
50
- overflow: 'hidden',
51
- textOverflow: 'ellipsis',
52
- whiteSpace: 'nowrap',
53
- }, to: to, href: href, target: href ? '_blank' : undefined, children: value })) : (_jsx("span", { style: {
54
- display: 'block',
55
- maxWidth: calcCellWidth,
56
- overflow: 'hidden',
57
- textOverflow: 'ellipsis',
58
- whiteSpace: 'nowrap',
59
- }, children: value })) }) }));
4
+ /**
5
+ * Heavily inspired by - https://stackoverflow.com/a/30362531/2803259
6
+ */
7
+ const ComponentName = 'EllipsisTableCell';
8
+ const EllipsisTableCellRoot = styled(TableCell, {
9
+ name: ComponentName,
10
+ shouldForwardProp: (prop) => prop !== 'width',
11
+ slot: 'Root',
12
+ })(({ width = '100%' }) => ({
13
+ '&': {
14
+ // because the cell content ends up absolutely positioned, the cell doesn't know the content height.
15
+ // the pseudo element with a hidden character establishes the proper height of the content and hides it
16
+ ':before': {
17
+ content: "'nbsp;'",
18
+ display: 'block',
19
+ // take the pseudo element out of the `display: block` flow so it won't push against our actual content
20
+ float: 'left',
21
+ visibility: 'hidden',
22
+ },
23
+ width,
24
+ },
25
+ }));
26
+ const EllipsisTableCellInnerWrap = styled('div', {
27
+ name: ComponentName,
28
+ slot: 'innerWrap',
29
+ })(() => ({
30
+ position: 'relative',
31
+ }));
32
+ const EllipsisTableCellContentWrap = styled('span', {
33
+ name: ComponentName,
34
+ slot: 'contentWrap',
35
+ })(() => ({
36
+ left: 0,
37
+ overflow: 'hidden',
38
+ position: 'absolute',
39
+ right: 0,
40
+ textOverflow: 'ellipsis',
41
+ whiteSpace: 'nowrap',
42
+ }));
43
+ export const EllipsisTableCell = ({ children, href, to, value, ...props }) => {
44
+ return (_jsx(EllipsisTableCellRoot, { ...props, children: _jsx(EllipsisTableCellInnerWrap, { children: _jsx(EllipsisTableCellContentWrap, { children: children ? (children) : href || to ? (_jsx(LinkEx, { to: to, href: href, target: href ? '_blank' : undefined, children: value })) : (value) }) }) }));
60
45
  };
61
46
  //# sourceMappingURL=EllipsisTableCell.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"EllipsisTableCell.js","sourceRoot":"","sources":["../../../../src/components/TableCell/EllipsisTableCell.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAkB,MAAM,eAAe,CAAA;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAGnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAA;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAA;AAC7D,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAA;AAQjE,MAAM,CAAC,MAAM,iBAAiB,GAAqC,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,GAAG,KAAK,EAAE,EAAE,EAAE;IAC7G,MAAM,CAAC,aAAa,EAAE,gBAAgB,CAAC,GAAG,QAAQ,CAAS,CAAC,CAAC,CAAA;IAC7D,MAAM,UAAU,GAAG,MAAM,CAAiB,IAAI,CAAC,CAAA;IAE/C,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,cAAc,GAAG,UAAU,CAAC,OAAO,EAAE,aAAa,CAAA;QACxD,MAAM,IAAI,GAAG,UAAU,CAAC,IAAI,EAA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oBAChB,QAAQ,EAAE,aAAa;oBACvB,QAAQ,EAAE,QAAQ;oBAClB,YAAY,EAAE,UAAU;oBACxB,UAAU,EAAE,QAAQ;iBACrB,YAEA,QAAQ,GACJ,CACR,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,CACf,KAAC,MAAM,IACL,KAAK,EAAE;oBACL,OAAO,EAAE,OAAO;oBAChB,QAAQ,EAAE,aAAa;oBACvB,QAAQ,EAAE,QAAQ;oBAClB,YAAY,EAAE,UAAU;oBACxB,UAAU,EAAE,QAAQ;iBACrB,EACD,EAAE,EAAE,EAAE,EACN,IAAI,EAAE,IAAI,EACV,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,YAElC,KAAK,GACC,CACV,CAAC,CAAC,CAAC,CACF,eACE,KAAK,EAAE;oBACL,OAAO,EAAE,OAAO;oBAChB,QAAQ,EAAE,aAAa;oBACvB,QAAQ,EAAE,QAAQ;oBAClB,YAAY,EAAE,UAAU;oBACxB,UAAU,EAAE,QAAQ;iBACrB,YAEA,KAAK,GACD,CACR,GACG,GACI,CACb,CAAA;AACH,CAAC,CAAA"}
1
+ {"version":3,"file":"EllipsisTableCell.js","sourceRoot":"","sources":["../../../../src/components/TableCell/EllipsisTableCell.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,MAAM,EAAE,SAAS,EAAkB,MAAM,eAAe,CAAA;AACjE,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAI3C;;GAEG;AAEH,MAAM,aAAa,GAAG,mBAAmB,CAAA;AAEzC,MAAM,qBAAqB,GAAG,MAAM,CAAC,SAAS,EAAE;IAC9C,IAAI,EAAE,aAAa;IACnB,iBAAiB,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,OAAO;IAC7C,IAAI,EAAE,MAAM;CACb,CAAC,CAAyB,CAAC,EAAE,KAAK,GAAG,MAAM,EAAE,EAAE,EA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C,CACF,KAAK,CACN,GAC4B,GACJ,GACP,CACzB,CAAA;AACH,CAAC,CAAA"}
@@ -0,0 +1,12 @@
1
+ /// <reference types="react" />
2
+ import { TableCellProps } from '@mui/material';
3
+ import { To } from 'react-router-dom';
4
+ export interface EllipsisTableCellProps extends TableCellProps {
5
+ value?: string;
6
+ to?: To | undefined;
7
+ href?: string | undefined;
8
+ forCell?: number;
9
+ }
10
+ /** @deprecated - use new EllipsisTableCell */
11
+ export declare const EllipsisTableCellDeprecated: React.FC<EllipsisTableCellProps>;
12
+ //# sourceMappingURL=EllipsisTableCellDeprecated.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EllipsisTableCellDeprecated.d.ts","sourceRoot":"","sources":["../../../../src/components/TableCell/EllipsisTableCellDeprecated.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAa,cAAc,EAAY,MAAM,eAAe,CAAA;AAGnE,OAAO,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAA;AAOrC,MAAM,WAAW,sBAAuB,SAAQ,cAAc;IAC5D,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,EAAE,CAAC,EAAE,EAAE,GAAG,SAAS,CAAA;IACnB,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IACzB,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED,8CAA8C;AAC9C,eAAO,MAAM,2BAA2B,EAAE,KAAK,CAAC,EAAE,CAAC,sBAAsB,CAuFxE,CAAA"}
@@ -0,0 +1,66 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { TableCell, useTheme } from '@mui/material';
3
+ import { LinkEx } from '@xylabs/react-link';
4
+ import { useEffect, useRef, useState } from 'react';
5
+ import { getActualPaddingX } from '../../lib';
6
+ import { findParent } from './findParent';
7
+ import { getRemainingRowWidth } from './getRemainingRowWidth';
8
+ import { getSmallestParentWidth } from './getSmallestParentWidth';
9
+ /** @deprecated - use new EllipsisTableCell */
10
+ export const EllipsisTableCellDeprecated = ({ children, value, to, forCell, href, ...props }) => {
11
+ const [calcCellWidth, setCalcCellWidth] = useState(0);
12
+ const hashDivRef = useRef(null);
13
+ const theme = useTheme();
14
+ useEffect(() => {
15
+ const currentElement = hashDivRef.current?.parentElement;
16
+ const cell = findParent('td', currentElement);
17
+ const row = findParent('tr', currentElement);
18
+ const checkWidth = (cell) => {
19
+ const smallestParentWidth = getSmallestParentWidth(cell);
20
+ if (smallestParentWidth && row) {
21
+ const remainingWidth = getRemainingRowWidth(row, forCell);
22
+ const actualPaddingX = getActualPaddingX(cell);
23
+ const remainderWidth = smallestParentWidth - remainingWidth - actualPaddingX;
24
+ cell.style.width = `${remainderWidth}`;
25
+ setCalcCellWidth(remainderWidth);
26
+ }
27
+ };
28
+ const onResize = () => {
29
+ if (cell) {
30
+ checkWidth(cell);
31
+ }
32
+ };
33
+ if (cell) {
34
+ checkWidth(cell);
35
+ window.addEventListener('resize', onResize);
36
+ row?.addEventListener('resize', onResize);
37
+ }
38
+ return () => {
39
+ window.removeEventListener('resize', onResize);
40
+ row?.removeEventListener('resize', onResize);
41
+ };
42
+ }, [forCell, hashDivRef]);
43
+ return (_jsx(TableCell, { ...props, children: _jsx("div", { ref: hashDivRef, children: children ? (_jsx("span", { style: {
44
+ display: 'block',
45
+ maxWidth: calcCellWidth,
46
+ minWidth: theme.spacing(10),
47
+ overflow: 'hidden',
48
+ textOverflow: 'ellipsis',
49
+ whiteSpace: 'nowrap',
50
+ }, children: children })) : href || to ? (_jsx(LinkEx, { style: {
51
+ display: 'block',
52
+ maxWidth: calcCellWidth,
53
+ minWidth: theme.spacing(10),
54
+ overflow: 'hidden',
55
+ textOverflow: 'ellipsis',
56
+ whiteSpace: 'nowrap',
57
+ }, to: to, href: href, target: href ? '_blank' : undefined, children: value })) : (_jsx("span", { style: {
58
+ display: 'block',
59
+ maxWidth: calcCellWidth,
60
+ minWidth: theme.spacing(10),
61
+ overflow: 'hidden',
62
+ textOverflow: 'ellipsis',
63
+ whiteSpace: 'nowrap',
64
+ }, children: value })) }) }));
65
+ };
66
+ //# sourceMappingURL=EllipsisTableCellDeprecated.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EllipsisTableCellDeprecated.js","sourceRoot":"","sources":["../../../../src/components/TableCell/EllipsisTableCellDeprecated.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAkB,QAAQ,EAAE,MAAM,eAAe,CAAA;AACnE,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAGnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAA;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAA;AAC7D,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAA;AASjE,8CAA8C;AAC9C,MAAM,CAAC,MAAM,2BAA2B,GAAqC,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,KAAK,EAAE,EAAE,EAAE;IAChI,MAAM,CAAC,aAAa,EAAE,gBAAgB,CAAC,GAAG,QAAQ,CAAS,CAAC,CAAC,CAAA;IAC7D,MAAM,UAAU,GAAG,MAAM,CAAiB,IAAI,CAAC,CAAA;IAC/C,MAAM,KAAK,GAAG,QAAQ,EAAE,CAAA;IAExB,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,cAAc,GAAG,UAAU,CAAC,OAAO,EAAE,aAAa,CAAA;QACxD,MAAM,IAAI,GAAG,UAAU,CAAC,IAAI,EAAE,cAAc,CAAC,CAAA;QAC7C,MAAM,GAAG,GAAG,UAAU,CAAC,IAAI,EAAE,cAAc,CAAC,CAAA;QAE5C,MAAM,UAAU,GAAG,CAAC,IAAiB,EAAE,EAAE;YACvC,MAAM,mBAAmB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AU;oBACxB,UAAU,EAAE,QAAQ;iBACrB,YAEA,QAAQ,GACJ,CACR,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,CACf,KAAC,MAAM,IACL,KAAK,EAAE;oBACL,OAAO,EAAE,OAAO;oBAChB,QAAQ,EAAE,aAAa;oBACvB,QAAQ,EAAE,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC;oBAC3B,QAAQ,EAAE,QAAQ;oBAClB,YAAY,EAAE,UAAU;oBACxB,UAAU,EAAE,QAAQ;iBACrB,EACD,EAAE,EAAE,EAAE,EACN,IAAI,EAAE,IAAI,EACV,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,YAElC,KAAK,GACC,CACV,CAAC,CAAC,CAAC,CACF,eACE,KAAK,EAAE;oBACL,OAAO,EAAE,OAAO;oBAChB,QAAQ,EAAE,aAAa;oBACvB,QAAQ,EAAE,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC;oBAC3B,QAAQ,EAAE,QAAQ;oBAClB,YAAY,EAAE,UAAU;oBACxB,UAAU,EAAE,QAAQ;iBACrB,YAEA,KAAK,GACD,CACR,GACG,GACI,CACb,CAAA;AACH,CAAC,CAAA"}
package/package.json CHANGED
@@ -11,11 +11,11 @@
11
11
  },
12
12
  "dependencies": {
13
13
  "@xylabs/pixel": "^1.4.5",
14
- "@xylabs/react-appbar": "^2.14.20",
15
- "@xylabs/react-button": "^2.14.20",
16
- "@xylabs/react-flexbox": "^2.14.20",
17
- "@xylabs/react-link": "^2.14.20",
18
- "@xylabs/react-shared": "^2.14.20",
14
+ "@xylabs/react-appbar": "^2.15.0",
15
+ "@xylabs/react-button": "^2.15.0",
16
+ "@xylabs/react-flexbox": "^2.15.0",
17
+ "@xylabs/react-link": "^2.15.0",
18
+ "@xylabs/react-shared": "^2.15.0",
19
19
  "tslib": "^2.4.0"
20
20
  },
21
21
  "peerDependencies": {
@@ -69,5 +69,5 @@
69
69
  },
70
70
  "sideEffects": false,
71
71
  "types": "dist/esm/index.d.ts",
72
- "version": "2.36.3"
72
+ "version": "2.37.1"
73
73
  }
@@ -1,100 +1,81 @@
1
- import { TableCell, TableCellProps } from '@mui/material'
1
+ import { styled, TableCell, TableCellProps } from '@mui/material'
2
2
  import { LinkEx } from '@xylabs/react-link'
3
- import { useEffect, useRef, useState } from 'react'
3
+ import { WithChildren } from '@xylabs/react-shared'
4
4
  import { To } from 'react-router-dom'
5
5
 
6
- import { getActualPaddingX } from '../../lib'
7
- import { findParent } from './findParent'
8
- import { getRemainingRowWidth } from './getRemainingRowWidth'
9
- import { getSmallestParentWidth } from './getSmallestParentWidth'
6
+ /**
7
+ * Heavily inspired by - https://stackoverflow.com/a/30362531/2803259
8
+ */
10
9
 
11
- export interface EllipsisTableCellProps extends TableCellProps {
12
- value?: string
13
- to?: To | undefined
14
- href?: string | undefined
15
- }
16
-
17
- export const EllipsisTableCell: React.FC<EllipsisTableCellProps> = ({ children, value, to, href, ...props }) => {
18
- const [calcCellWidth, setCalcCellWidth] = useState<number>(0)
19
- const hashDivRef = useRef<HTMLDivElement>(null)
10
+ const ComponentName = 'EllipsisTableCell'
20
11
 
21
- useEffect(() => {
22
- const currentElement = hashDivRef.current?.parentElement
23
- const cell = findParent('td', currentElement)
24
- const row = findParent('tr', currentElement)
12
+ const EllipsisTableCellRoot = styled(TableCell, {
13
+ name: ComponentName,
14
+ shouldForwardProp: (prop) => prop !== 'width',
15
+ slot: 'Root',
16
+ })<EllipsisTableCellProps>(({ width = '100%' }) => ({
17
+ '&': {
18
+ // because the cell content ends up absolutely positioned, the cell doesn't know the content height.
19
+ // the pseudo element with a hidden character establishes the proper height of the content and hides it
20
+ ':before': {
21
+ content: "'nbsp;'",
22
+ display: 'block',
23
+ // take the pseudo element out of the `display: block` flow so it won't push against our actual content
24
+ float: 'left',
25
+ visibility: 'hidden',
26
+ },
27
+ width,
28
+ },
29
+ }))
25
30
 
26
- const checkWidth = (cell: HTMLElement) => {
27
- const smallestParentWidth = getSmallestParentWidth(cell)
28
- if (smallestParentWidth && row) {
29
- const remainingWidth = getRemainingRowWidth(row)
30
- const actualPaddingX = getActualPaddingX(cell)
31
- const remainderWidth = smallestParentWidth - remainingWidth - actualPaddingX
32
- cell.style.width = `${remainderWidth}`
33
- setCalcCellWidth(remainderWidth)
34
- }
35
- }
31
+ const EllipsisTableCellInnerWrap = styled('div', {
32
+ name: ComponentName,
33
+ slot: 'innerWrap',
34
+ })(() => ({
35
+ position: 'relative',
36
+ }))
36
37
 
37
- const onResize = () => {
38
- if (cell) {
39
- checkWidth(cell)
40
- }
41
- }
38
+ const EllipsisTableCellContentWrap = styled('span', {
39
+ name: ComponentName,
40
+ slot: 'contentWrap',
41
+ })(() => ({
42
+ left: 0,
43
+ overflow: 'hidden',
44
+ position: 'absolute',
45
+ right: 0,
46
+ textOverflow: 'ellipsis',
47
+ whiteSpace: 'nowrap',
48
+ }))
42
49
 
43
- if (cell) {
44
- checkWidth(cell)
45
- window.addEventListener('resize', onResize)
46
- row?.addEventListener('resize', onResize)
47
- }
48
- return () => {
49
- window.removeEventListener('resize', onResize)
50
- row?.removeEventListener('resize', onResize)
51
- }
52
- }, [hashDivRef])
50
+ export interface EllipsisTableCellProps extends TableCellProps {
51
+ /**
52
+ * Width of the table cell.
53
+ *
54
+ * Note: When using percentages, this value can be different than what you expect
55
+ * if used on a cell that is not the first cell in the first row.
56
+ */
57
+ width?: string | number
58
+ href?: string
59
+ to?: To
60
+ value?: string
61
+ }
53
62
 
63
+ export const EllipsisTableCell: React.FC<WithChildren<EllipsisTableCellProps>> = ({ children, href, to, value, ...props }) => {
54
64
  return (
55
- <TableCell {...props}>
56
- <div ref={hashDivRef}>
57
- {children ? (
58
- <span
59
- style={{
60
- display: 'block',
61
- maxWidth: calcCellWidth,
62
- overflow: 'hidden',
63
- textOverflow: 'ellipsis',
64
- whiteSpace: 'nowrap',
65
- }}
66
- >
67
- {children}
68
- </span>
69
- ) : href || to ? (
70
- <LinkEx
71
- style={{
72
- display: 'block',
73
- maxWidth: calcCellWidth,
74
- overflow: 'hidden',
75
- textOverflow: 'ellipsis',
76
- whiteSpace: 'nowrap',
77
- }}
78
- to={to}
79
- href={href}
80
- target={href ? '_blank' : undefined}
81
- >
82
- {value}
83
- </LinkEx>
84
- ) : (
85
- <span
86
- style={{
87
- display: 'block',
88
- maxWidth: calcCellWidth,
89
- overflow: 'hidden',
90
- textOverflow: 'ellipsis',
91
- whiteSpace: 'nowrap',
92
- }}
93
- >
94
- {value}
95
- </span>
96
- )}
97
- </div>
98
- </TableCell>
65
+ <EllipsisTableCellRoot {...props}>
66
+ <EllipsisTableCellInnerWrap>
67
+ <EllipsisTableCellContentWrap>
68
+ {children ? (
69
+ children
70
+ ) : href || to ? (
71
+ <LinkEx to={to} href={href} target={href ? '_blank' : undefined}>
72
+ {value}
73
+ </LinkEx>
74
+ ) : (
75
+ value
76
+ )}
77
+ </EllipsisTableCellContentWrap>
78
+ </EllipsisTableCellInnerWrap>
79
+ </EllipsisTableCellRoot>
99
80
  )
100
81
  }
@@ -0,0 +1,106 @@
1
+ import { TableCell, TableCellProps, useTheme } from '@mui/material'
2
+ import { LinkEx } from '@xylabs/react-link'
3
+ import { useEffect, useRef, useState } from 'react'
4
+ import { To } from 'react-router-dom'
5
+
6
+ import { getActualPaddingX } from '../../lib'
7
+ import { findParent } from './findParent'
8
+ import { getRemainingRowWidth } from './getRemainingRowWidth'
9
+ import { getSmallestParentWidth } from './getSmallestParentWidth'
10
+
11
+ export interface EllipsisTableCellProps extends TableCellProps {
12
+ value?: string
13
+ to?: To | undefined
14
+ href?: string | undefined
15
+ forCell?: number //cell index for ellipsized table cell
16
+ }
17
+
18
+ /** @deprecated - use new EllipsisTableCell */
19
+ export const EllipsisTableCellDeprecated: React.FC<EllipsisTableCellProps> = ({ children, value, to, forCell, href, ...props }) => {
20
+ const [calcCellWidth, setCalcCellWidth] = useState<number>(0)
21
+ const hashDivRef = useRef<HTMLDivElement>(null)
22
+ const theme = useTheme()
23
+
24
+ useEffect(() => {
25
+ const currentElement = hashDivRef.current?.parentElement
26
+ const cell = findParent('td', currentElement)
27
+ const row = findParent('tr', currentElement)
28
+
29
+ const checkWidth = (cell: HTMLElement) => {
30
+ const smallestParentWidth = getSmallestParentWidth(cell)
31
+ if (smallestParentWidth && row) {
32
+ const remainingWidth = getRemainingRowWidth(row, forCell)
33
+ const actualPaddingX = getActualPaddingX(cell)
34
+ const remainderWidth = smallestParentWidth - remainingWidth - actualPaddingX
35
+ cell.style.width = `${remainderWidth}`
36
+ setCalcCellWidth(remainderWidth)
37
+ }
38
+ }
39
+
40
+ const onResize = () => {
41
+ if (cell) {
42
+ checkWidth(cell)
43
+ }
44
+ }
45
+
46
+ if (cell) {
47
+ checkWidth(cell)
48
+ window.addEventListener('resize', onResize)
49
+ row?.addEventListener('resize', onResize)
50
+ }
51
+ return () => {
52
+ window.removeEventListener('resize', onResize)
53
+ row?.removeEventListener('resize', onResize)
54
+ }
55
+ }, [forCell, hashDivRef])
56
+
57
+ return (
58
+ <TableCell {...props}>
59
+ <div ref={hashDivRef}>
60
+ {children ? (
61
+ <span
62
+ style={{
63
+ display: 'block',
64
+ maxWidth: calcCellWidth,
65
+ minWidth: theme.spacing(10),
66
+ overflow: 'hidden',
67
+ textOverflow: 'ellipsis',
68
+ whiteSpace: 'nowrap',
69
+ }}
70
+ >
71
+ {children}
72
+ </span>
73
+ ) : href || to ? (
74
+ <LinkEx
75
+ style={{
76
+ display: 'block',
77
+ maxWidth: calcCellWidth,
78
+ minWidth: theme.spacing(10),
79
+ overflow: 'hidden',
80
+ textOverflow: 'ellipsis',
81
+ whiteSpace: 'nowrap',
82
+ }}
83
+ to={to}
84
+ href={href}
85
+ target={href ? '_blank' : undefined}
86
+ >
87
+ {value}
88
+ </LinkEx>
89
+ ) : (
90
+ <span
91
+ style={{
92
+ display: 'block',
93
+ maxWidth: calcCellWidth,
94
+ minWidth: theme.spacing(10),
95
+ overflow: 'hidden',
96
+ textOverflow: 'ellipsis',
97
+ whiteSpace: 'nowrap',
98
+ }}
99
+ >
100
+ {value}
101
+ </span>
102
+ )}
103
+ </div>
104
+ </TableCell>
105
+ )
106
+ }
@@ -0,0 +1,99 @@
1
+ import { Table, TableBody, TableCell, TableHead, TableRow } from '@mui/material'
2
+ import { ComponentStory, DecoratorFn, Meta } from '@storybook/react'
3
+ import { BrowserRouter } from 'react-router-dom'
4
+
5
+ import { EllipsisTableCell } from './EllipsisTableCell'
6
+
7
+ const TableDecorator: DecoratorFn = (Story, args) => {
8
+ return (
9
+ <BrowserRouter>
10
+ <Table>
11
+ <TableHead>
12
+ <TableRow>
13
+ <TableCell>Heading1</TableCell>
14
+ <TableCell>Heading2</TableCell>
15
+ <TableCell>Heading3</TableCell>
16
+ <TableCell>Heading4</TableCell>
17
+ </TableRow>
18
+ </TableHead>
19
+ <TableBody>
20
+ <Story {...args} />
21
+ </TableBody>
22
+ </Table>
23
+ </BrowserRouter>
24
+ )
25
+ }
26
+
27
+ // eslint-disable-next-line import/no-default-export
28
+ export default {
29
+ component: EllipsisTableCell,
30
+ decorators: [TableDecorator],
31
+ title: 'shared/EllipsisTableCell',
32
+ } as Meta
33
+
34
+ const Template: ComponentStory<typeof EllipsisTableCell> = (props) => {
35
+ return (
36
+ <TableRow>
37
+ <EllipsisTableCell to="/foo" value="As-Link-e0f01ab62384d8d501ff6b2f860b7992c347a6b8ae07bd5fd2fa4a7d47dad915" {...props} />
38
+ <TableCell>d2fa4a7d47dad915501ff6b2f860b7992c347</TableCell>
39
+ <TableCell>d2fa4a7d47dad915</TableCell>
40
+ <TableCell>e0f05</TableCell>
41
+ </TableRow>
42
+ )
43
+ }
44
+
45
+ const TemplateWithMultiple: ComponentStory<typeof EllipsisTableCell> = (props) => {
46
+ return (
47
+ <TableRow>
48
+ <EllipsisTableCell width="50%" {...props}>
49
+ As-children-e0f01ab62384d8d501ff6b2f860b7992c347a6b8ae07bd5fd2fa4a7d47dad915
50
+ </EllipsisTableCell>
51
+ <EllipsisTableCell width="50%" {...props}>
52
+ e0f01ab62384d8d501ff6b2f860b7992c347a6b8ae07bd5fd2fa4a7d47dad915
53
+ </EllipsisTableCell>
54
+ <TableCell>d2fa4a7d47dad915</TableCell>
55
+ <TableCell>e0f05</TableCell>
56
+ </TableRow>
57
+ )
58
+ }
59
+
60
+ const TemplateWithMultipleRows: ComponentStory<typeof EllipsisTableCell> = (props) => {
61
+ return (
62
+ <>
63
+ <TableRow>
64
+ <EllipsisTableCell
65
+ width="50%"
66
+ href="http://foo.com"
67
+ {...props}
68
+ value="As-href-e0f01ab62384d8d501ff6b2f860b7992c347a6b8ae07bd5fd2fa4a7d47dad915"
69
+ />
70
+ <EllipsisTableCell width="50%" {...props}>
71
+ e0f01ab62384d8d501ff6b2f860b7992c347a6b8ae07bd5fd2fa4a7d47dad915
72
+ </EllipsisTableCell>
73
+ <TableCell>d2fa4a7d47dad915</TableCell>
74
+ <TableCell>e0f05</TableCell>
75
+ </TableRow>
76
+ <TableRow>
77
+ <TableCell>d2fa4a7d47dad915</TableCell>
78
+ <TableCell>e0f05</TableCell>
79
+ <EllipsisTableCell width="50%" {...props}>
80
+ e0f01ab62384d8d501ff6b2f860b7992c347a6b8ae07bd5fd2fa4a7d47dad915
81
+ </EllipsisTableCell>
82
+ <EllipsisTableCell width="50%" {...props}>
83
+ e0f01ab62384d8d501ff6b2f860b7992c347a6b8ae07bd5fd2fa4a7d47dad915
84
+ </EllipsisTableCell>
85
+ </TableRow>
86
+ </>
87
+ )
88
+ }
89
+
90
+ const Default = Template.bind({})
91
+ Default.args = {}
92
+
93
+ const WithMultiple = TemplateWithMultiple.bind({})
94
+ WithMultiple.args = {}
95
+
96
+ const WithMultipleRows = TemplateWithMultipleRows.bind({})
97
+ WithMultipleRows.args = {}
98
+
99
+ export { Default, WithMultiple, WithMultipleRows }