-
Notifications
You must be signed in to change notification settings - Fork 2.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
Added withTheme HOC #1226
Added withTheme HOC #1226
Conversation
@epicfaace any news regarding this? Very eager to see it in! |
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.
Looks good! Can you please add a page for documentation? I'm thinking you can add an entire new section called "Customizing with other frameworks" or something.
src/withTheme.js
Outdated
function withTheme(data) { | ||
return class extends Component { | ||
render() { | ||
let { templates, widgets, fields, ...restData } = this.props; |
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.
Can you rename restData
to otherProps
?
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.
Sure, I'm very bad at naming variables.
So I added documentation, but I'm pretty bad at writing those things, so it would be nice if someone could look at it, fix typos, spelling errors and so on... |
Thank you! Can you also add some tests? (using |
I can try it, but I've written one test in javascript so far. And I don't have a lot of time, so don't know when I will do it... |
No problem, I can add the tests if you are not comfortable doing so. |
That would be awesome, I have no idea how to test this functionality, so at least I would learn something new |
Any news regarding this? |
Nice! Any news on this? Can't wait to implement the usage in my repo! |
I'll be writing a test for this soon! Then will be ready to merge |
@danbalarin I don't understand why the ability to pass in a custom form component to the theme is needed. Would there really be different kinds of data handling that each theme would require? |
Umm, actually I used it in Bootstrap 4 since there might be needed to add |
@danbalarin Can you remove the custom form option for now and perhaps move that to a later PR? I'm not convinced that a custom form is the right way to go, because then it seems like it involves too much customization; one can pass in an arbitrary component (not even one based on react-jsonschema-form's For the case you mentioned, maybe the option of "adding a class after validation" should be implemented as a feature of react-jsonschema-form itself? |
I've removed custom form. |
Thanks! Can you update the documentation too? Then we should be all good to go. |
Done |
Question: Why require the templates be packaged into a temporary object when the underlying Form component itself does not do so? Think it'd be simpler/more direct to just allow template overrides be passed through the same way the other props are; otherwise, users of the ThemedForm would also need to package any custom field template overrides they want to perform on an instance-by-instance basis inside a makeshift "templates" object (thus diverging from how the native Form does it, and thereby requiring each theme provider to document this usage that differs from/contradicts the original docs). I also don't think this HOC should limit itself to just widgets, fields(, and templates) and should pretty much allow for the passing through of all custom props (the data object). For instance, a "theme" object may want to turn off the native errors list via the
I get that at this point this "withTheme" HOC really just overrides every property (not just what some people would deem "theme" related), but I think it would prove much more useful. Otherwise, any Form customizations would have to extend the native Form or HOC class component in order to attach nice-to-have defaults for props like "showErrorList". Proposed tweaks to this PR:
You may also want to consider a deep merge of the themeProps and the directProps to account for theme providers that want to provide defaults for other object-type properties (e.g. |
@mirajp good idea. What do you mean when you said that "templates [are] packaged into a temporary object"? |
@epicfaace based on how the HOC function is currently designed in this PR:
However, the actual rjsf Form has no such special
Note that in the native rjsf Form, only If the implementation was even less heavy-handed/opinionated (currently only merges together the |
@mirajp that's a good point. The |
@epicfaace I don't think I can modify this PR since I'm not a collaborator of this repo nor an authorized contributor of @danbalarin's master branch that this PR is merging from. All I can independently do is make a PR against his master branch or against rjsf's master branch -- may be simpler to just wait for @danbalarin to check back and copy and paste my snippet. |
Originally I made it this way so you can define theme in a separate file and use default export. An object of this size is negligible because all it does is holding three pointers, so is passing it as a param, its only passing pointer not the whole object. @mirajp I'm sorry, but I have a lot of work that needs to be done, I added you as a collaborator, so you can add proposed changes. |
@danbalarin I believe you should still be able to still the default export on the Theme options/props object, just this object will have all the field templates not nested under a templates object (I think the # of pointers would actually decrease (by 1 -- no pointer to a new templates object) and # of objects would also decrease (by 1 -- no templates object) :P -- though this is pretty insignificant in the grand scheme of things). I can review your branch and modify the PR and confirm your tests still work when I get a chance later today. |
@danbalarin @epicfaace updated the PR. |
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.
Thanks for the changes! Nearly there, just a few doc changes.
Co-Authored-By: Ashwin Ramaswami <aramaswamis@gmail.com>
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.
Thanks -- can you also add a few more test cases?
sandbox.restore(); | ||
}); | ||
|
||
describe("With widgets", () => { |
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.
Can you add some more tests here that test the widgets, templates, and also the overriding?
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.
How do I even get the tests to run with changes? They keep running without any of my changes to the same file lol... (and only runs this one file, none of the other test files) I can literally delete all the tests and it still runs it - is there some weird build step involved and it only runs whatever got built?
I think I got it to work once by running cs-format and cs-check, but can't get npm run test
to pick up any new changes to the file.
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.
@epicfaace added a few tests. I tried to get a custom ObjectFieldTemplate to render, but it never worked/was never called by Form =/ dunno why.
Co-Authored-By: Ashwin Ramaswami <aramaswamis@gmail.com>
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.
Looks good. Thanks for your hard work @mirajp @danbalarin !
It would be good @mirajp if you can look into why the ObjectFieldTemplate is not rendering in your test. Perhaps you can push your test to a separate branch / PR? I will merge this anyway for now.
Reasons for making this change
Implemented withTheme HOC in order to be able to create custom theme without having to create forks. #1222
Example usage:
Theme object can provide widgets, templates, fields and form in order to customize generated form.
Example Bootstrap 4 theme
Checklist
npm run cs-format
on my branch to conform my code to prettier coding style