Skip to content
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

Fix table store issues #2867

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/good-cats-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@udecode/plate-core": minor
---

Export `atom` from `jotai`
5 changes: 5 additions & 0 deletions .changeset/smart-poems-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@udecode/plate-table": major
---

Fix: TableProvider is incorrectly shared by all tables in the editor. TableProvider must now be rendered as part of TableElement.
5 changes: 5 additions & 0 deletions .changeset/violet-mugs-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@udecode/plate-table": patch
---

Fix: Row and column size overrides not being applied correctly
19 changes: 19 additions & 0 deletions apps/www/content/docs/components/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ Use the [CLI](https://platejs.org/docs/components/cli) to install the latest ver

## January 2024 #7

### January 8 #7.2

- `table-element` - TableProvider must now be rendered above TableElement

```tsx
// Before
export const TableElement = withRef<typeof PlateElement>(({ className, children, ...props }, ref) => {
// ...
});

// After
export const TableElement = withHOC(
TableProvider,
withRef<typeof PlateElement>(({ className, children, ...props }, ref) => {
// ...
})
);
```

### January 2 #7.1

- `dropdown-menu` - fix: do not exclude `className` in `DropdownMenuContent`
Expand Down
9 changes: 6 additions & 3 deletions apps/www/src/registry/default/plate-ui/table-element.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import {
useEditorSelector,
useElement,
useRemoveNodeButton,
withHOC,
} from '@udecode/plate-common';
import {
mergeTableCells,
TableProvider,
TTableElement,
unmergeTableCells,
useTableBordersDropdownMenuContentState,
Expand Down Expand Up @@ -193,8 +195,9 @@ export const TableFloatingToolbar = withRef<typeof PopoverContent>(
}
);

export const TableElement = withRef<typeof PlateElement>(
({ className, children, ...props }, ref) => {
export const TableElement = withHOC(
TableProvider,
withRef<typeof PlateElement>(({ className, children, ...props }, ref) => {
const { colSizes, isSelectingCell, minColumnWidth, marginLeft } =
useTableElementState();
const { props: tableProps, colGroupProps } = useTableElement();
Expand Down Expand Up @@ -232,5 +235,5 @@ export const TableElement = withRef<typeof PlateElement>(
</div>
</TableFloatingToolbar>
);
}
})
);
1 change: 1 addition & 0 deletions packages/core/src/libs/jotai.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { createAtomStore } from 'jotai-x';
export { atom } from 'jotai';
2 changes: 0 additions & 2 deletions packages/table/src/createTablePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createPluginFactory } from '@udecode/plate-common';

import { onKeyDownTable } from './onKeyDownTable';
import { TableProvider } from './stores';
import { insertTableColumn, insertTableRow } from './transforms/index';
import { TablePlugin, TableStoreCellAttributes } from './types';
import { withTable } from './withTable';
Expand Down Expand Up @@ -41,7 +40,6 @@ export const createTablePlugin = createPluginFactory<TablePlugin>({
_cellIndices: new WeakMap() as TableStoreCellAttributes,
},
withOverrides: withTable,
renderAboveEditable: TableProvider,
plugins: [
{
key: ELEMENT_TR,
Expand Down
11 changes: 5 additions & 6 deletions packages/table/src/stores/tableStore.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';
import { createAtomStore, TElement } from '@udecode/plate-common';
import { atom, createAtomStore, TElement } from '@udecode/plate-common';

export type TableStoreSizeOverrides = Map<number, number>;

export const { tableStore, useTableStore, TableProvider } = createAtomStore(
{
colSizeOverrides: new Map() as TableStoreSizeOverrides,
rowSizeOverrides: new Map() as TableStoreSizeOverrides,
colSizeOverrides: atom(new Map() as TableStoreSizeOverrides),
rowSizeOverrides: atom(new Map() as TableStoreSizeOverrides),
marginLeftOverride: null as number | null,
hoveredColIndex: null as number | null,
selectedCells: null as TElement[] | null,
Expand Down Expand Up @@ -37,15 +37,14 @@ const useOverrideSizeFactory = (
[setOverrides]
);

// jotai supports setting with functions, but createAtomStore doesn't know that
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was true in earlier versions of jotai-x, but is now only true if atom is used explicitly when setting the initial state, and this is accurately reflected in the types.

export const useOverrideColSize = () => {
const setColSizeOverrides = useTableStore().set.colSizeOverrides();
return useOverrideSizeFactory(setColSizeOverrides as unknown as any);
return useOverrideSizeFactory(setColSizeOverrides);
};

export const useOverrideRowSize = () => {
const setRowSizeOverrides = useTableStore().set.rowSizeOverrides();
return useOverrideSizeFactory(setRowSizeOverrides as unknown as any);
return useOverrideSizeFactory(setRowSizeOverrides);
};

export const useOverrideMarginLeft = () =>
Expand Down
Loading