Add customKeyDefinitions prop for custom key rendering#233
Open
drahoslove wants to merge 1 commit intoCarlosNZ:mainfrom
Open
Add customKeyDefinitions prop for custom key rendering#233drahoslove wants to merge 1 commit intoCarlosNZ:mainfrom
drahoslove wants to merge 1 commit intoCarlosNZ:mainfrom
Conversation
Mirrors the existing customNodeDefinitions pattern to allow users to provide custom React components for rendering node keys, via a new customKeyDefinitions prop on JsonEditor.
Author
|
Hello @CarlosNZ, could you please take a look at this feature pull request? |
Owner
|
Hi @drahoslove -- thanks for this, I'll try and have a proper look at it in the next few days. Cheers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
New
customKeyDefinitionsfor defining a custom key componentMirrors the existing
customNodeDefinitionspattern to allow users to provide custom React components for rendering node keys, via a newcustomKeyDefinitionsprop onJsonEditor.Context
Needed an option to customize the rendering of keys. Our use case requires the ability to highlight a key or a substring of it, or wrap it in a clickable link.
The approach I tried first was to use
hideKey: truecombined withcustomNodeDefinitionsto render both a custom key and value together. That approach is questionable on its own, and it doesn't work for collection keys at all - the custom collection wrapper only renders content inside the brackets, with no way to inject anything before the opening bracket.So I decided to make this contribution.
Changes
src/CustomKey.ts(new)CustomKeyDatainterface andgetCustomKey()lookup utility, analogous toCustomNode.tssrc/types.tsCustomKeyProps<T>andCustomKeyDefinition<T>interfaces; extendsJsonEditorPropsandBaseNodePropswithcustomKeyDefinitionsandcustomKeyDatasrc/KeyDisplay.tsxsrc/ValueNodeWrapper.tsx/src/CollectionNode.tsxcustomKeyDataandnodeDatathrough toKeyDisplay;CollectionNodecomputeschildCustomKeyDataper child in the render loopsrc/JsonEditor.tsxcustomKeyDefinitionsprop, computes rootcustomKeyData, propagates both down the treesrc/index.tsCustomKeyPropsandCustomKeyDefinitionUsage
Screenshot
This matches the usage example:
And an example of what I needed to achieve:
Notes
I tried to keep the implementation as close to the existing
customNodeDefinitionspattern as possible to make it feel native to the codebase. That said, I am open to suggestions if anything looks off - whether that is the shape ofCustomKeyProps, the naming, or something else entirely.I would love to get this merged so I can drop my fork and use the package directly, so the sooner we can align on any changes the better. Happy to update the README or add anything else needed to get this across the finish line.