-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Decoupling data table manager components #2807
Decoupling data table manager components #2807
Conversation
🦋 Changeset detectedLatest commit: 1af83b5 The changes in this PR will be included in the next version bump. This PR includes changesets to release 96 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
After comparing both options, I would go with this option since it does not seem to require the user importing a new Provider or any other form of import. |
@@ -239,8 +239,15 @@ export type TDataTableProps<Row extends TRow = TRow> = { | |||
}; | |||
|
|||
const DataTable = <Row extends TRow = TRow>(props: TDataTableProps<Row>) => { | |||
const columns = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we use this alternative, we would miss the initial state. and it seems to me like we simply just need to pass the columns object just the same way it was used in the data-table-manager. Current solution works well, as expected.
const columns = | |
const [columns, setColumns] = useState<[]>(); | |
const handleCustomEvent = (event) => { | |
const { columns } = event.detail; | |
setColumns(columns); | |
}; | |
useEffect(() => { | |
window.addEventListener(‘DataTaBleManagerEvent’, handleCustomEvent); | |
return () => { | |
window.removeEventListener(‘DataTaBleManagerEvent’, handleCustomEvent); | |
}; | |
}, []); |
@@ -166,7 +166,10 @@ const DataTableManager = <Row extends TRow = TRow>( | |||
[areDisplaySettingsEnabled, props.columns, isWrappingText] | |||
); | |||
|
|||
return ( | |||
// @ts-ignore | |||
window.dataTableColumns = { columns }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use the window.dispatch but it seems like we would be missing the initial state using this approach
window.dataTableColumns = { columns }; | |
useEffect(()=> { | |
const customEvent = new CustomEvent(‘DataTaBleManagerEvent’, { | |
detail: {columns} | |
}) | |
window.dispatchEvent(customEvent) | |
},[columns]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, if we go for the events approach, it will be difficult to get the initial state as the DataTable
component can be rendered after the DataTableManager
would have sent the initial state event and that will imply having some kind of event bus where a consumer can consume past events but that means maybe too much complexity.
On the other hand, using the window
object to communicate data using new properties is risky because of global space pollution.
We would need to be very careful because other libraries we don't know our consumers could be using might use the same attribute name and break things.
Also, it can be also challenging managing the cleanup of that global state.
I believe the provider approach might be a safer one.
We will be supporting both use cases: DataTable
as a children of the DataTableManager
(which won't require the provider) and the DataTable
detached (which will require the provider).
In the second use case, the state have to be lifted to a common parent anyway: it's a matter of leaving that state management up to the consumers or providing some help from our side, which I think the new provider would do well.
So, all in all, I think the provider approach will benefit us in the long term.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the comprehensive feedback. I will push a change with the Provider approach since it is now clear that we would have some security/complexity concerns using the window approach 🙏🏾
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated here: 39a97a4
Thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ddouglasz for working on it, this was a tricky one.
@@ -153,3 +186,97 @@ describe('rendering', () => { | |||
).toBeInTheDocument(); | |||
}); | |||
}); | |||
|
|||
describe('rendering with decoupled data table', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also want to be sure that when the datatable manager is decoupled, all it's behaviours are still intact.
packages/components/data-table-manager/src/data-table-manager.spec.js
Outdated
Show resolved
Hide resolved
packages/components/data-table-manager/src/data-table-manager.tsx
Outdated
Show resolved
Hide resolved
packages/components/data-table-manager/src/data-table-manager.tsx
Outdated
Show resolved
Hide resolved
e02436f
to
909c5c3
Compare
...omponents/data-table-manager/src/data-table-manager-provider/data-table-manager-provider.tsx
Outdated
Show resolved
Hide resolved
packages/components/data-table-manager/src/data-table-manager.story.js
Outdated
Show resolved
Hide resolved
...omponents/data-table-manager/src/data-table-manager-provider/data-table-manager-provider.tsx
Outdated
Show resolved
Hide resolved
...omponents/data-table-manager/src/data-table-manager-provider/data-table-manager-provider.tsx
Outdated
Show resolved
Hide resolved
packages/components/data-table-manager/src/data-table-manager.story.js
Outdated
Show resolved
Hide resolved
packages/components/data-table-manager/src/data-table-manager.spec.js
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for all your work in this PR, Douglas! 🙇
### Detached | ||
|
||
As mentioned earlier, the default behaviour is for the triggering element to be placed above the `<DataTable>` component (top-right corner), but there can be use cases where it would be needed to place that triggering element in a different place in the page. This is also possible but requires the usage of one more component (`DataTableManagerProvider`) in order to share the manager state between the manager panels and the `DataTable` component. | ||
In this mode, you should pass the manager props to the `DataTableManagerProvider` component and the `DataTableManager` does not need to receive any prop; it can be placed anywhere in the component's tree without requiring any prop. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great to include a breaking line between the two paragraphs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you Doulgas! Looks good! Great work overall! 🚀
We want to be able to consume the data table settings in the data table without having so much restrictions to how they are currently structured in the DOM tree.
We want to decouple the DataTableManager such that the settings dropdown component can live anywhere in the DOM tree from the DataTable
The current way it works is one in which the
Datatable
is always directly after the Datatable settings dropdown.The settings dropdown should be flexible to exist anywhere in the component that it is used.
This PR implements this feature using the Provider method, where the provider allows both the data table settings dropdown and the datatable to share state from a common source, allowing us the flexibility we want.
An alternative approach is to managing the state using a Window method as seen in this POC. But this posed to have some complexities regarding how to manage the initial states and also the security implications of storing the state in a global window object
An example of where this can be useful: