nitro-web 0.3.14 → 0.3.15

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.
@@ -38,6 +38,7 @@ export type TableProps<T> = {
38
38
  rowSideColor?: (row: T|undefined, type: TableRowType) => { className: string, width: number }
39
39
  rowGap?: number
40
40
  rowOnClick?: (row: T) => void
41
+ rowSelectable?: (row: T) => boolean // rows returning false get no checkbox and are skipped by select all
41
42
  rowLink?: (row: T) => string
42
43
  columnGap?: number
43
44
  columnPaddingX?: number
@@ -78,6 +79,7 @@ export function Table<T extends TableRow>({
78
79
  rowLinesMax,
79
80
  rowLink,
80
81
  rowOnClick,
82
+ rowSelectable,
81
83
  rowSideColor,
82
84
  // Class names
83
85
  className,
@@ -120,15 +122,17 @@ export function Table<T extends TableRow>({
120
122
  return cols
121
123
  }, [columnsProp])
122
124
 
125
+ const selectableRows = useMemo(() => (rows ?? []).filter(row => !rowSelectable || rowSelectable(row)), [rows, rowSelectable])
126
+
123
127
  const onSelect = useCallback((idOrAll: string, checked: boolean) => {
124
128
  setSelectedRowIds((o) => {
125
- if (idOrAll == 'all' && checked) return (rows ?? []).map(row => row._id || '')
129
+ if (idOrAll == 'all' && checked) return selectableRows.map(row => row._id || '')
126
130
  else if (idOrAll == 'all' && !checked) return []
127
131
  else if (o.includes(idOrAll) && !checked) return o.filter(id => id != idOrAll)
128
132
  else if (!o.includes(idOrAll) && checked) return [...o, idOrAll]
129
133
  else return o
130
134
  })
131
- }, [selectedRowIds, rows])
135
+ }, [selectedRowIds, selectableRows])
132
136
 
133
137
  const getAlignClass = useCallback((align: TableColumn['align'], _returnJustify?: boolean) => {
134
138
  if (_returnJustify) return align == 'left' ? '' : align == 'center' ? 'justify-center' : 'justify-end'
@@ -141,8 +145,8 @@ export function Table<T extends TableRow>({
141
145
  // Drive the header checkbox's indeterminate state when some (but not all) rows are selected
142
146
  useEffect(() => {
143
147
  const input = document.querySelector<HTMLInputElement>(`input[name="checkbox-all-${rand}"]`)
144
- if (input) input.indeterminate = selectedRowIds.length > 0 && selectedRowIds.length < rows.length
145
- }, [selectedRowIds, rows, rand])
148
+ if (input) input.indeterminate = selectedRowIds.length > 0 && selectedRowIds.length < selectableRows.length
149
+ }, [selectedRowIds, selectableRows, rand])
146
150
 
147
151
  // --- Sorting ---
148
152
 
@@ -218,7 +222,7 @@ export function Table<T extends TableRow>({
218
222
  size={checkboxSize}
219
223
  name={`checkbox-all-${rand}`}
220
224
  hitboxPadding={5}
221
- checked={rows.length > 0 && selectedRowIds.length === rows.length}
225
+ checked={selectableRows.length > 0 && selectedRowIds.length === selectableRows.length}
222
226
  className='!m-0 py-[5px]' // py-5 is required for hitbox (restricted to tabel cell height)
223
227
  checkboxClassName={twMerge('border-foreground shadow-[0_1px_2px_0px_#0000001c]', checkboxClassName)}
224
228
  onChange={(e) => onSelect('all', e.target.checked)}
@@ -309,7 +313,7 @@ export function Table<T extends TableRow>({
309
313
  <div className={isLoading && showLoadingInline ? 'opacity-0 pointer-events-none' : ''}>
310
314
  {
311
315
  col.value == 'checkbox'
312
- ? <Checkbox
316
+ ? (!rowSelectable || rowSelectable(row)) && <Checkbox
313
317
  size={checkboxSize}
314
318
  name={`checkbox-${row._id}`}
315
319
  onChange={(e) => onSelect(row?._id || '', e.target.checked)}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nitro-web",
3
- "version": "0.3.14",
3
+ "version": "0.3.15",
4
4
  "repository": "github:boycce/nitro-web",
5
5
  "homepage": "https://boycce.github.io/nitro-web/",
6
6
  "description": "Nitro is a battle-tested, modular base project to turbocharge your projects, styled using Tailwind 🚀",