-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #612 from Neovici/feat/update-deps
Recompute css when columns change
- Loading branch information
Showing
7 changed files
with
1,933 additions
and
1,808 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
npx --no-install commitlint --edit $1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,158 +1,167 @@ | ||
import { useEffect, useLayoutEffect, useState } from '@pionjs/pion'; | ||
import { useLayoutEffect, useState } from '@pionjs/pion'; | ||
import { memooize } from '@neovici/cosmoz-utils/memoize'; | ||
|
||
const columnSymbol = Symbol('column'), | ||
verifyColumnSetup = (columns) => { | ||
let ok = true; | ||
const columnNames = columns.map((c) => c.name); | ||
// Check if column names are set | ||
columns.forEach((column) => { | ||
if (column.name != null) { | ||
return; | ||
} | ||
ok = false; | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
'The name attribute needs to be set on all columns! Missing on column', | ||
column, | ||
); | ||
}); | ||
|
||
columns.forEach((column) => { | ||
if ( | ||
columnNames.indexOf(column.name) === | ||
columnNames.lastIndexOf(column.name) | ||
) { | ||
return; | ||
} | ||
ok = false; | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
'The name attribute needs to be unique among all columns! Not unique on column', | ||
column, | ||
); | ||
}); | ||
return ok; | ||
}, | ||
// eslint-disable-next-line max-lines-per-function | ||
domColumnsToConfig = (host, { enabledColumns }) => { | ||
const domColumns = host.shadowRoot | ||
.querySelector('#columnsSlot') | ||
.assignedElements({ flatten: true }) | ||
.filter((child) => child.isOmnitableColumn && !child.hidden); | ||
|
||
if (!verifyColumnSetup(domColumns)) { | ||
return []; | ||
const columnSymbol = Symbol('column'); | ||
const verifyColumnSetup = (columns) => { | ||
let ok = true; | ||
const columnNames = columns.map((c) => c.name); | ||
// Check if column names are set | ||
columns.forEach((column) => { | ||
if (column.name != null) { | ||
return; | ||
} | ||
|
||
const columns = Array.isArray(enabledColumns) | ||
? domColumns.filter((column) => enabledColumns.includes(column.name)) | ||
: domColumns.filter((column) => !column.disabled); | ||
|
||
// eslint-disable-next-line max-lines-per-function | ||
return columns.map((column) => { | ||
const valuePath = column.valuePath ?? column.name; | ||
|
||
return { | ||
name: column.name, | ||
title: column.title, | ||
|
||
valuePath, | ||
groupOn: column.groupOn ?? valuePath, | ||
sortOn: column.sortOn ?? valuePath, | ||
|
||
minWidth: column.minWidth, | ||
width: column.width, | ||
flex: column.flex, | ||
priority: column.priority, | ||
|
||
getString: column.getString, | ||
getComparableValue: column.getComparableValue, | ||
serializeFilter: column.serializeFilter, | ||
deserializeFilter: column.deserializeFilter, | ||
toXlsxValue: column.toXlsxValue, | ||
|
||
renderHeader: column.renderHeader, | ||
renderCell: column.renderCell, | ||
renderEditCell: column.renderEditCell, | ||
renderGroup: column.renderGroup, | ||
cellTitleFn: column.cellTitleFn, | ||
getFilterFn: column.getFilterFn, | ||
headerCellClass: column.headerCellClass, | ||
cellClass: column.cellClass, | ||
|
||
editable: column.editable, | ||
|
||
values: column.values, | ||
source: memooize(column.computeSource), | ||
|
||
noLocalFilter: column.noLocalFilter, | ||
|
||
mini: column.mini, | ||
renderMini: column.renderMini, | ||
|
||
// @deprecated | ||
loading: column.loading, | ||
externalValues: column.externalValues, | ||
computeSource: column.computeSource, | ||
|
||
// boolean columns | ||
trueLabel: column.trueLabel, | ||
falseLabel: column.falseLabel, | ||
|
||
// list columns | ||
valueProperty: column.valueProperty, | ||
textProperty: column.textProperty, | ||
emptyLabel: column.emptyLabel, | ||
emptyValue: column.emptyValue, | ||
|
||
// range columns | ||
min: column.min, | ||
max: column.max, | ||
locale: column.locale, | ||
autoupdate: column.autoupdate, | ||
|
||
// number columns | ||
maximumFractionDigits: column.maximumFractionDigits, | ||
minimumFractionDigits: column.minimumFractionDigits, | ||
|
||
// amount columns | ||
currency: column.currency, | ||
rates: column.rates, | ||
autodetect: column.autodetect, | ||
|
||
// treenode columns | ||
ownerTree: column.ownerTree, | ||
keyProperty: column.keyProperty, | ||
|
||
...column.getConfig?.(column), | ||
|
||
[columnSymbol]: column, | ||
}; | ||
}); | ||
}, | ||
useDOMColumns = (host, { enabledColumns }) => { | ||
const [columns, setColumns] = useState([]); | ||
|
||
useLayoutEffect(() => { | ||
setColumns(domColumnsToConfig(host, { enabledColumns })); | ||
}, []); | ||
|
||
useEffect(() => { | ||
const slot = host.shadowRoot.querySelector('#columnsSlot'), | ||
handler = () => | ||
setColumns(domColumnsToConfig(host, { enabledColumns })); | ||
|
||
handler(); | ||
slot.addEventListener('slotchange', handler); | ||
host.addEventListener('cosmoz-column-prop-changed', handler); | ||
return () => { | ||
slot.removeEventListener('slotchange', handler); | ||
host.removeEventListener('cosmoz-column-prop-changed', handler); | ||
}; | ||
}, [enabledColumns]); | ||
|
||
return columns; | ||
ok = false; | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
'The name attribute needs to be set on all columns! Missing on column', | ||
column, | ||
); | ||
}); | ||
|
||
columns.forEach((column) => { | ||
if ( | ||
columnNames.indexOf(column.name) === columnNames.lastIndexOf(column.name) | ||
) { | ||
return; | ||
} | ||
ok = false; | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
'The name attribute needs to be unique among all columns! Not unique on column', | ||
column, | ||
); | ||
}); | ||
return ok; | ||
}; | ||
|
||
// eslint-disable-next-line max-lines-per-function | ||
const normalizeColumn = (column) => { | ||
const valuePath = column.valuePath ?? column.name; | ||
|
||
return { | ||
name: column.name, | ||
title: column.title, | ||
|
||
valuePath, | ||
groupOn: column.groupOn ?? valuePath, | ||
sortOn: column.sortOn ?? valuePath, | ||
|
||
minWidth: column.minWidth, | ||
width: column.width, | ||
flex: column.flex, | ||
priority: column.priority, | ||
|
||
getString: column.getString, | ||
getComparableValue: column.getComparableValue, | ||
serializeFilter: column.serializeFilter, | ||
deserializeFilter: column.deserializeFilter, | ||
toXlsxValue: column.toXlsxValue, | ||
|
||
renderHeader: column.renderHeader, | ||
renderCell: column.renderCell, | ||
renderEditCell: column.renderEditCell, | ||
renderGroup: column.renderGroup, | ||
cellTitleFn: column.cellTitleFn, | ||
getFilterFn: column.getFilterFn, | ||
headerCellClass: column.headerCellClass, | ||
cellClass: column.cellClass, | ||
|
||
editable: column.editable, | ||
|
||
values: column.values, | ||
source: memooize(column.computeSource), | ||
|
||
noLocalFilter: column.noLocalFilter, | ||
|
||
mini: column.mini, | ||
renderMini: column.renderMini, | ||
|
||
// @deprecated | ||
loading: column.loading, | ||
externalValues: column.externalValues, | ||
computeSource: column.computeSource, | ||
|
||
// boolean columns | ||
trueLabel: column.trueLabel, | ||
falseLabel: column.falseLabel, | ||
|
||
// list columns | ||
valueProperty: column.valueProperty, | ||
textProperty: column.textProperty, | ||
emptyLabel: column.emptyLabel, | ||
emptyValue: column.emptyValue, | ||
|
||
// range columns | ||
min: column.min, | ||
max: column.max, | ||
locale: column.locale, | ||
autoupdate: column.autoupdate, | ||
|
||
// number columns | ||
maximumFractionDigits: column.maximumFractionDigits, | ||
minimumFractionDigits: column.minimumFractionDigits, | ||
|
||
// amount columns | ||
currency: column.currency, | ||
rates: column.rates, | ||
autodetect: column.autodetect, | ||
|
||
// treenode columns | ||
ownerTree: column.ownerTree, | ||
keyProperty: column.keyProperty, | ||
|
||
...column.getConfig?.(column), | ||
|
||
[columnSymbol]: column, | ||
}; | ||
}; | ||
|
||
const isVisibleColumn = (child) => child.isOmnitableColumn && !child.hidden; | ||
|
||
const collectDomColumns = (slot) => { | ||
const domColumns = slot | ||
.assignedElements({ flatten: true }) | ||
.filter(isVisibleColumn); | ||
|
||
if (!verifyColumnSetup(domColumns)) return []; | ||
return domColumns; | ||
}; | ||
|
||
const normalizeColumns = (domColumns, enabledColumns) => { | ||
const columns = Array.isArray(enabledColumns) | ||
? domColumns.filter((column) => enabledColumns.includes(column.name)) | ||
: domColumns.filter((column) => !column.disabled); | ||
|
||
return columns.map(normalizeColumn); | ||
}; | ||
|
||
export const useDOMColumns = (host, { enabledColumns }) => { | ||
const [columns, setColumns] = useState([]); | ||
|
||
useLayoutEffect(() => { | ||
let sched; | ||
const slot = host.shadowRoot.querySelector('#columnsSlot'); | ||
const update = () => { | ||
setColumns(normalizeColumns(collectDomColumns(slot), enabledColumns)); | ||
}; | ||
const scheduleUpdate = () => { | ||
cancelAnimationFrame(sched); | ||
sched = requestAnimationFrame(update); | ||
}; | ||
|
||
scheduleUpdate(); | ||
|
||
slot.addEventListener('slotchange', scheduleUpdate); | ||
host.addEventListener('cosmoz-column-prop-changed', scheduleUpdate); | ||
|
||
return () => { | ||
slot.removeEventListener('slotchange', scheduleUpdate); | ||
host.removeEventListener('cosmoz-column-prop-changed', scheduleUpdate); | ||
cancelAnimationFrame(sched); | ||
}; | ||
}, [enabledColumns]); | ||
|
||
return columns; | ||
}; | ||
|
||
export { columnSymbol, useDOMColumns }; | ||
export { columnSymbol }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.