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

Accept type-keyed react render functions #5244

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

MichaelDarr
Copy link
Contributor

Status

The contents of this PR represent an in-progress implementation of a proposal. If it is decided to move forward in this direction, tests, a changeset, and additional documentation will be written and added to this PR.

Description
These changes allow type-specific render functions to passed into the Editable component directly. The renderElement and renderLeaf properties now accept either a single render function (the current standard) or an object mapping the type property of the relevant Descendant to its render function.

Example

This PR uses the new pattern for element renderers in the inlines example:

custom-types.d.ts

export type LinkElement = { type: 'link'; url: string; children: Descendant[] }
export type ButtonElement = { type: 'button'; children: Descendant[] }

declare module 'slate' {
    interface CustomTypes {
        Element: ButtonElement | LinkElement
    }
}

inlines.tsx

const EditableButtonComponent: SlateReact.ElementRenderer<'button'> = ({
  attributes,
  children,
  element,
}) => {
  // `element` is `ButtonElement`
  //...
}

// ...

const LinkComponent: SlateReact.ElementRenderer<'link'> = ({
  attributes,
  children,
  element,
}) => {
  // `element` is `LinkElement`
  //...
}

// ...

<Editable
  renderElement={{
    button: EditableButtonComponent,
    link: LinkComponent,
  }}
  // ...
/>

Context
In my experience, defining and maintaining Text and Element render functions is tedious. As seen in several of the examples, these render functions are usually just a huge, awkward switch statements.

These changes provide a more direct, less cumbersome method of passing typed render functions to Slate. Care has been taken to make these changes non-breaking.

Checks

  • The new code matches the existing patterns and styles.
  • The tests pass with yarn test.
  • The linter passes with yarn lint. (Fix errors with yarn fix.)
  • The relevant examples still work. (Run examples with yarn start.)
  • You've added a changeset if changing functionality. (Add one with yarn changeset add.)

Signed-off-by: Michael Darr <michael.e.darr@gmail.com>
Signed-off-by: Michael Darr <michael.e.darr@gmail.com>
Signed-off-by: Michael Darr <michael.e.darr@gmail.com>
Signed-off-by: Michael Darr <michael.e.darr@gmail.com>
Signed-off-by: Michael Darr <michael.e.darr@gmail.com>
Signed-off-by: Michael Darr <michael.e.darr@gmail.com>
Signed-off-by: Michael Darr <michael.e.darr@gmail.com>
Signed-off-by: Michael Darr <michael.e.darr@gmail.com>
Signed-off-by: Michael Darr <michael.e.darr@gmail.com>
This reverts commit ef390cc.
@changeset-bot
Copy link

changeset-bot bot commented Jan 4, 2023

⚠️ No Changeset found

Latest commit: 029565a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Signed-off-by: Michael Darr <michael.e.darr@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant