Skip to content

Commit 5374a2a

Browse files
fix: column width in list (#5875)
* fix: column width in list * fix: tests * feat: colMode * style: make colMode deprecated --------- Co-authored-by: Dorian Maliszewski <[email protected]>
1 parent 38c6e83 commit 5374a2a

File tree

12 files changed

+2513
-1459
lines changed

12 files changed

+2513
-1459
lines changed

.changeset/clear-steaks-share.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ultraviolet/ui": minor
3+
---
4+
5+
Fix `List.Cell` width when it is in %

packages/plus/src/components/OfferList/__tests__/__snapshots__/index.test.tsx.snap

Lines changed: 344 additions & 344 deletions
Large diffs are not rendered by default.

packages/ui/src/components/List/Cell.tsx

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@ import { assignInlineVars } from '@vanilla-extract/dynamic'
55
import type { CSSProperties, ReactNode } from 'react'
66
import { forwardRef } from 'react'
77
import { useColumnProvider } from './ColumnProvider'
8-
import { listCell } from './styles.css'
9-
import { maxWidthCell, minWidthCell, widthCell } from './variables.css'
8+
import { useListContext } from './ListContext'
9+
import { listCell, listCellStrict } from './styles.css'
10+
import {
11+
listCellPadding,
12+
maxWidthCell,
13+
maxWidthChildrenCell,
14+
minWidthCell,
15+
minWidthChildrenCell,
16+
widthCell,
17+
widthChildrenCell,
18+
} from './variables.css'
1019

1120
type CellProps = {
1221
children?: ReactNode
@@ -18,19 +27,41 @@ type CellProps = {
1827

1928
export const Cell = forwardRef<HTMLTableCellElement, CellProps>(
2029
({ children, className, 'data-testid': dataTestid, colSpan, style }, ref) => {
21-
const { maxWidth, minWidth, width } = useColumnProvider()
30+
const context = useColumnProvider()
31+
const { colMode } = useListContext()
32+
const width = context?.width
33+
const maxWidth = context?.width
34+
const minWidth = context?.width
35+
36+
/** Remove padding from width to avoid overflow since boxSizing = 'content-box' */
37+
const widthChildren = width?.includes('%')
38+
? '100%'
39+
: `calc(${widthCell} - ${listCellPadding} - ${listCellPadding})`
40+
const maxWidthChildren = maxWidth?.includes('%')
41+
? '100%'
42+
: `calc(${maxWidth} - ${listCellPadding} - ${listCellPadding})`
43+
const minWidthChildren = minWidth?.includes('%')
44+
? '100%'
45+
: `calc(${minWidth} - ${listCellPadding} - ${listCellPadding})`
2246

2347
return (
2448
<td
25-
className={cn(className, listCell)}
49+
className={cn(
50+
className,
51+
listCell,
52+
colMode === 'strict' ? listCellStrict : '',
53+
)}
2654
colSpan={colSpan}
2755
data-testid={dataTestid}
2856
ref={ref}
2957
style={{
3058
...assignInlineVars({
31-
[widthCell]: width,
32-
[minWidthCell]: minWidth,
33-
[maxWidthCell]: maxWidth,
59+
[widthCell]: width ?? 'auto',
60+
[minWidthCell]: minWidth ?? 'auto',
61+
[maxWidthCell]: maxWidth ?? 'none',
62+
[widthChildrenCell]: width ? widthChildren : 'auto',
63+
[maxWidthChildrenCell]: maxWidth ? maxWidthChildren : 'none',
64+
[minWidthChildrenCell]: minWidth ? minWidthChildren : 'auto',
3465
}),
3566
...style,
3667
}}

packages/ui/src/components/List/ColumnProvider.tsx

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { createContext, useContext } from 'react'
33

44
type ContextType =
55
| {
6-
width: string
7-
maxWidth: string
8-
minWidth: string
6+
width?: string
7+
maxWidth?: string
8+
minWidth?: string
99
}
1010
| undefined
1111

@@ -25,26 +25,14 @@ export const ColumnProvider = ({
2525
}: ColumnProviderProps) => (
2626
<ColumnContext.Provider
2727
value={{
28-
maxWidth: maxWidth ?? 'none',
29-
minWidth: minWidth ?? 'auto',
30-
width: width ?? 'auto',
28+
maxWidth,
29+
minWidth,
30+
width,
3131
}}
3232
>
3333
{children}
3434
</ColumnContext.Provider>
3535
)
3636

3737
// oxlint-disable-next-line react/only-export-components
38-
export const useColumnProvider = () => {
39-
const context = useContext(ColumnContext)
40-
41-
if (!context) {
42-
return {
43-
maxWidth: 'none',
44-
minWidth: 'auto',
45-
width: 'auto',
46-
}
47-
}
48-
49-
return context
50-
}
38+
export const useColumnProvider = () => useContext(ColumnContext)

packages/ui/src/components/List/ListContext.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export type ListContextValue = {
5050
refList: RefObject<HTMLInputElement>[]
5151
setRefList: Dispatch<SetStateAction<RefObject<HTMLInputElement>[]>>
5252
handleOnChange: (value: string, checked: boolean) => void
53+
colMode?: 'strict' | 'flexible'
5354
}
5455

5556
const ListContext = createContext<ListContextValue | undefined>(undefined)
@@ -61,6 +62,7 @@ export type ListProviderProps = {
6162
expandButton: boolean
6263
onSelectedChange?: Dispatch<SetStateAction<string[]>>
6364
columns: ColumnProps[]
65+
colMode?: 'strict' | 'flexible'
6466
}
6567

6668
const checkStateOfCheckboxs = (ids: RowState) => {
@@ -86,6 +88,7 @@ export const ListProvider = ({
8688
expandButton,
8789
onSelectedChange,
8890
columns,
91+
colMode,
8992
}: ListProviderProps) => {
9093
const [expandedRowIds, setExpandedRowIds] = useState<RowState>({})
9194
const [selectedRowIds, setSelectedRowIds] = useState<RowState>({})
@@ -331,6 +334,7 @@ export const ListProvider = ({
331334
() => ({
332335
allRowSelectValue,
333336
collapseRow,
337+
colMode,
334338
columns,
335339
expandButton,
336340
expandedRowIds,
@@ -370,6 +374,7 @@ export const ListProvider = ({
370374
unselectAll,
371375
unselectRow,
372376
handleOnChange,
377+
colMode,
373378
],
374379
)
375380

0 commit comments

Comments
 (0)