-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add ValueComponent
to viewerOptions
to allow customizing examples, enums, consts, and defaults
#224
Conversation
... to display CodeBlock examples with formatted/indented JSON instead of all in one line
✅ Deploy Preview for delicate-torrone-5d35ee ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
Hello @gnidan , Welcome ;) I thought a bit about your issue and a smarter approach I have in mind would be the introduction of something called In essence, it is similar to the DescriptionComponent I introduced in 1.4.0 for people putting markdown description instead of plain text : export default function CreateDescription(props: Props): JSX.Element {
const { description } = props
const { DescriptionComponent } = useJSVOptionsContext()
return (
<div style={{ marginTop: "var(--ifm-table-cell-padding)" }}>
{DescriptionComponent ? (
<DescriptionComponent description={description} />
) : (
description
)}
</div>
) so
What do you think ? Let me know if you need something |
Great thinking. I much prefer your suggested approach. I will switch this PR to that method when I get back on my computer tomorrow. Thanks for the quick eyes! |
Looking into this now. I notice that descriptions don't seem to be implemented via the "qualifier messages" system, so I'm trying to reconcile my understanding of all this. I assume you expect to see a (I'll be hacking a bit this weekend because I've got some time; I'll plan to push up more commits in time for your Monday morning so you can tell me where I'm going wrong :) |
ValueComponent
to viewerOptions
to allow customizing examples, enums, consts, and defaults
- Add `ValueComponent` as JSVOption - Replace use of hardcoded `printSchemaType` with new `<CreateValue />` component for the following qualifier messages: - enum - constant - examples - default
d45ed4b
to
d859a1e
Compare
I've pushed a first pass at this approach and it seems to work fine. In the current implementation here, I'm leaving the simple/complex object distinction to be a concern of Here's what I mean<JSONSchemaViewer
viewerOptions={{
showExamples: true,
ValueComponent: ({ value }) => {
// deal with simple types first
if (["string", "number", "bigint", "boolean"].includes(typeof value)) {
return <code>{
(value as string | number | bigint | boolean).toString()
}</code>;
}
// for complex types use a whole CodeBlock
return <CodeBlock language="json">{`${
JSON.stringify(value, undefined, 2)
}`}</CodeBlock>;
}
}} /> I can see the pros/cons here... there's simplicity in just allowing one I'm leaving this as draft because I think there's two open concerns still:
|
Hello @gnidan , A small explanation about "qualifier messages" system : You see, the rendering component takes parameters - inside So the changes I have in mind :
examples: {
match: ({ schema, options }) =>
options.showExamples === true && schema.examples !== undefined,
Component: (props) => (
<QMS.ExamplesQM key={"examples"} {...props} />
),
},
.....
I think the standalone
To answer your concern :
Feel free to share your thoughts ;) |
Quick comment re: enums,
I'm not sure Tabs are the best way to do this. Even if I want to customize ValueComponent for most things, I might not want to display enum values in a big horizontal list like that. Besides, what value does a TabItem body offer besides the label? OK, I might have complex enum values... Maybe we just want a regular (Anyway, I haven't fully digested the rest of your message, but I'll follow-up tomorrow (as in: Sunday). I appreciate your taking the time to get back to me on the weekend!) |
Guess we can give a try to the regular |
I might be able to tweak some things further, but I think my implementation is back to you now for review.
I think that's all I've got for now. Looking forward to reviewing what you came up with in #225 to see if it covers my use case :) Thank you! |
It could work so I will give it a chance ;)
Good question. You know what ? Let's use the
Thanks.
Mmm I think it would be better to have a new category called "🛠️ Customizations" instead of put it in For info, I changed a bit the structure of the docs to use tabs (so that users are not overwhelmed of info, specially in case where the JSON Schema is quite long) # Annotations
<Tabs>
<TabItem value="Viewer" label="Viewer" default>
<JSONSchemaViewer
schema={ Schema }
viewerOptions={{
showExamples: true,
DescriptionComponent: ({description}) => <ReactMarkdown children={description} />
}}
/>
</TabItem>
<TabItem value="viewerOptions" label='viewerOptions'>
```js
{
showExamples: true,
DescriptionComponent: ({description}) => <ReactMarkdown children={description} />
}
```
</TabItem>
<TabItem value="JSON Schema" label='JSON Schema'>
<CodeBlock language="json">{JSON.stringify(Schema, null, 2)}</CodeBlock>
</TabItem>
</Tabs> About the schema, I think this one could fit - feel free to update it to your wishes : {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "CustomizationOptions",
"description": "JSON schema for customized options",
"type": "object",
"properties": {
"customField": {
"type": "string",
"description": "A customized or personalized field",
"enum": ["palette", "teddyBear", "tools", "laptop", "thread", "phone", "puzzle", "scissors", "hammer", "note"],
"default": "palette",
"examples": ["tools", "note"]
}
},
"required": ["customField"],
"additionalProperties": false
} For UnresolvedRefsComponent, I added an example in the docs as well |
... and update test snapshots
- Move to new home in "Customizations" section - Switch to using tabs - Define example schema that features enum, default, and const - Include example ValueComponent that references schema in logic
I can't request reviewers in this repo, so I'll pass this back to you by way of a comment :)
How's it all look? Thanks for your help guiding me with this one. |
Thanks @gnidan ,with our joint efforts, we've come up with something that should satisfy every use case that might come to mind. I will create the release in a couple of minutes. For your example, I will update it in another MR turn from |
🎉 This PR is included in version 1.9.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This PR adds the
ValueComponent
option toviewerOptions
to allow customizing how raw values get rendered. This applies to use via"examples"
,"const"
,"enum"
, and"default"
.