Skip to content

Commit

Permalink
Merge pull request #2881 from johnrazeur/fix/editor_id_store
Browse files Browse the repository at this point in the history
fix plate store id with editor prop
  • Loading branch information
zbeyens authored Jan 14, 2024
2 parents 0306da9 + eeb2ec3 commit 902aab8
Showing 3 changed files with 43 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-papayas-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@udecode/plate-core": patch
---

fix plate store id when plate use the editor prop.
44 changes: 37 additions & 7 deletions packages/core/src/components/Plate.spec.tsx
Original file line number Diff line number Diff line change
@@ -236,6 +236,21 @@ describe('Plate', () => {
expect(result.current).toBe('test');
});
});

describe('when Plate has an editor', () => {
it('should be editor id', async () => {
const editor = createPlateEditor({ id: 'test' });

const wrapper = ({ children }: any) => (
<Plate editor={editor}>{children}</Plate>
);
const { result } = renderHook(() => usePlateSelectors().id(), {
wrapper,
});

expect(result.current).toBe('test');
});
});
});

describe('usePlateEditorStore', () => {
@@ -249,14 +264,29 @@ describe('Plate', () => {
renderHook(() => useEditorRef().isFallback, { wrapper }).result.current;

describe('when Plate exists', () => {
it('returns the store', () => {
const wrapper = ({ children }: any) => (
<Plate id="test">{children}</Plate>
);
describe('when editor is defined', () => {
it('returns the store', async () => {
const editor = createPlateEditor({ id: 'test' });

const wrapper = ({ children }: any) => (
<Plate editor={editor}>{children}</Plate>
);
expect(getStore(wrapper)).toBeDefined();
expect(getId(wrapper)).toBe('test');
expect(getIsFallback(wrapper)).toBe(false);
});
});

expect(getStore(wrapper)).toBeDefined();
expect(getId(wrapper)).toBe('test');
expect(getIsFallback(wrapper)).toBe(false);
describe('when editor is not defined', () => {
it('returns the store', () => {
const wrapper = ({ children }: any) => (
<Plate id="test">{children}</Plate>
);

expect(getStore(wrapper)).toBeDefined();
expect(getId(wrapper)).toBe('test');
expect(getIsFallback(wrapper)).toBe(false);
});
});
});

2 changes: 1 addition & 1 deletion packages/core/src/components/Plate.tsx
Original file line number Diff line number Diff line change
@@ -98,7 +98,7 @@ function PlateInner<
primary,
maxLength,
}: PlateProps<V, E>) {
const [id] = React.useState(() => idProp ?? nanoid());
const [id] = React.useState(() => editorProp?.id ?? idProp ?? nanoid());

const editor: E = React.useMemo(
() =>

0 comments on commit 902aab8

Please sign in to comment.