-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Change <Slate>
to a controlled component
#3216
Conversation
<Slate>
to a controlled component
Looks great, well done man! |
@@ -0,0 +1,41 @@ | |||
import React, { useMemo } from 'react' | |||
import { Editor, Node, Operation, Range } from 'slate' |
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.
moving this to a different module was a good decision i think, it took a little bit to find it just diving through github.
editor.selection = selection | ||
return [editor] | ||
}, [value, selection, ...Object.values(rest)]) | ||
|
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 wonder if there will be some gotchas around memoizing around the extra props. for the most part this is probably what a caller is going to need but this seems like a subtle trap for users who are not paying attention
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.
Yeah, although by including them it's less restrictive (not more) so I think it's alright? Otherwise implementing your own custom editor properties becomes impossible I think?
This looks pretty good to me. I'm happy that this still works well with the context based distribution work flow. |
Looks good to me, also, thank you for all your work @ianstormtaylor. |
How can I get operations for collaborative editing after this MR? |
I would prefer that the entire state gets controlled as
|
Not really been following this - is it possible to use Slate as an uncontrolled component after this? Or is this fully switching it over? I guess you can always have a wrapper component to make it uncontrolled. |
@ianstormtaylor https://docs.slatejs.org/concepts/xx-migrating#plugins So plugin deps on operation won't work anymore? What's the new way to build a plugin? |
I think your question doesn't belong here @Jokcy, try slack, this PR is for making slate into a controlled component again. @ianstormtaylor , just out of curiosity, since this is a breaking change, how long do you think it will take to get this out? Not sure what the policy about stuff like this is with slate. Thanks! |
You can access them from |
This fully switches to a controlled component. I think using React's own helpers you can make it "uncontrolled" kind of. But I'd rather have only a single way to do it. |
I think docs still refer to the old API (an un-controlled). Not sure if it is a known issue, though |
Is this adding or improving a feature or fixing a bug?
Improvement.
What's the new behavior?
It changes
<Slate>
to be a controlled component.How does this change work?
It takes
value=
andselection=
props and ensures they are updated on the editor whenever they change, such that people can use their ownuseState
to store them. People must now store state for both of these.Currently it is:
With this PR it is:
For an example in context, check out the changes to the
rich-text
example on this branch.Have you checked that...?
yarn test
.yarn lint
. (Fix errors withyarn fix
.)yarn start
.)Does this fix any issues or need any specific reviewers?
Fixes: #3209