-
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
Merged
Merged
Added withTheme HOC #1226
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5626813
Added withTheme HOC
danbalarin f6a43bb
prettier run
danbalarin d1580e2
renamed restData variable
danbalarin bb0a0f7
data.Fields -> data.fields
danbalarin a95c11d
Created documentation
danbalarin ae687d8
Merge branch 'master' of https://github.com/danbalarin/react-jsonsche…
epicfaace 0643609
Merge branch 'master' into danbalarin-master
epicfaace 58f19f2
doc: update doc
epicfaace 3e4e16f
test: add test
epicfaace 88b7907
removed custom form
danbalarin e7638b4
removed custom form from documentation
danbalarin 3a141fe
Updated withTheme + docs to show pass-through nature of all the props
mirajp 9de51ca
Apply suggestions from code review
mirajp e3f7e17
Update test/withTheme_test.js
mirajp 12ba00c
Updated tests
mirajp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
## Customizing with other frameworks | ||
|
||
### withTheme Higher-Order Component | ||
The `withTheme` component provides an easy way to extend the functionality of react-jsonschema-form by passing in a theme object that defines custom/overridden widgets and fields, as well as any of the other possible properties of the standard rjsf `Form` component. This theme-defining object is passed as the only parameter to the HOC (`withTheme(ThemeObj)`), and the HOC will return a themed-component which you use instead of the standard `Form` component. | ||
|
||
### Usage | ||
|
||
```jsx | ||
import React, { Component } from 'react'; | ||
import { withTheme } from 'react-jsonschema-form'; | ||
import Bootstrap4Theme from 'react-jsonschema-form-theme-bs4'; | ||
|
||
const ThemedForm = withTheme(Bootstrap4Theme); | ||
class Demo extends Component { | ||
render() { | ||
return <ThemedForm schema={{...}} uiSchema={{...}} /> | ||
} | ||
} | ||
``` | ||
|
||
### Theme object properties | ||
The Theme object consists of the same properties as the rjsf `Form` component (such as **widgets** and **fields**). The themed-Form component merges together any theme-specific **widgets** and **fields** with the default **widgets** and **fields**. For instance, providing a single widget in **widgets** will merge this widget with all the default widgets of the rjsf `Form` component, but overrides the default if the theme's widget's name matches the default widget's name. Thus, for each default widget or field not specified/overridden, the themed-form will rely on the defaults from the rjsf `Form`. Note that you are not required to pass in either custom **widgets** or **fields** when using the custom-themed HOC component; you can make the essentially redefine the default Form by simply doing `const Form = withTheme({});`. | ||
|
||
#### Widgets and fields | ||
**widgets** and **fields** should be in the same format as shown [here](/advanced-customization/#custom-widgets-and-fields). | ||
|
||
Example theme with custom widget: | ||
```jsx | ||
const MyCustomWidget = (props) => { | ||
return ( | ||
<input type="text" | ||
className="custom" | ||
value={props.value} | ||
required={props.required} | ||
onChange={(event) => props.onChange(event.target.value)} /> | ||
); | ||
}; | ||
|
||
const myWidgets = { | ||
myCustomWidget: MyCustomWidget | ||
}; | ||
|
||
const ThemeObject = {widgets: myWidgets}; | ||
export default ThemeObject; | ||
``` | ||
|
||
The above can be similarly done for **fields**. | ||
|
||
#### Templates | ||
Each template should be passed directly into the theme object just as you would into the rjsf Form component. Here is an example of how to use a custom [ArrayFieldTemplate](/advanced-customization/#array-field-template) and [ErrorListTemplate](/advanced-customization/#error-list-template) in the theme object: | ||
```jsx | ||
function MyArrayFieldTemplate(props) { | ||
return ( | ||
<div> | ||
{props.items.map(element => element.children)} | ||
{props.canAdd && <button type="button" onClick={props.onAddClick}></button>} | ||
</div> | ||
); | ||
} | ||
|
||
function MyErrorListTemplate(props) { | ||
const {errors} = props; | ||
return ( | ||
<ul> | ||
{errors.map(error => ( | ||
<li key={error.stack}> | ||
{error.stack} | ||
</li> | ||
))} | ||
</ul> | ||
); | ||
} | ||
|
||
const ThemeObject = { | ||
ArrayFieldTemplate: MyArrayFieldTemplate, | ||
ErrorList: MyErrorListTemplate, | ||
widgets: myWidgets | ||
}; | ||
|
||
export default ThemeObject; | ||
``` | ||
|
||
### Overriding other Form props | ||
Just as the theme can override **widgets**, **fields**, any of the field templates, and set default values to properties like **showErrorList**, you can do the same with the instance of the withTheme() Form component. | ||
```jsx | ||
const ThemeObject = { | ||
ArrayFieldTemplate: MyArrayFieldTemplate, | ||
fields: myFields, | ||
showErrorList: false, | ||
widgets: myWidgets | ||
}; | ||
``` | ||
|
||
Thus, the user has higher priority than the withTheme HOC, and the theme has higher priority than the default values of the rjsf Form component (**User** > **Theme** > **Defaults**). |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import Form from "./components/Form"; | ||
import withTheme from "./withTheme"; | ||
|
||
export { withTheme }; | ||
export default Form; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React, { Component } from "react"; | ||
import PropTypes from "prop-types"; | ||
import Form from "./"; | ||
|
||
function withTheme(themeProps) { | ||
return class extends Component { | ||
render() { | ||
let { fields, widgets, ...directProps } = this.props; | ||
fields = { ...themeProps.fields, ...fields }; | ||
widgets = { ...themeProps.widgets, ...widgets }; | ||
return ( | ||
<Form | ||
{...themeProps} | ||
{...directProps} | ||
fields={fields} | ||
widgets={widgets} | ||
/> | ||
); | ||
} | ||
}; | ||
} | ||
|
||
withTheme.propTypes = { | ||
widgets: PropTypes.object, | ||
fields: PropTypes.object, | ||
}; | ||
|
||
export default withTheme; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
import { expect } from "chai"; | ||
import React from "react"; | ||
|
||
import { withTheme } from "../src"; | ||
import { createComponent, createSandbox } from "./test_utils"; | ||
|
||
describe("withTheme", () => { | ||
let sandbox; | ||
|
||
beforeEach(() => { | ||
sandbox = createSandbox(); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
describe("With fields", () => { | ||
it("should use the withTheme field", () => { | ||
const fields = { | ||
StringField() { | ||
return <div className="string-field" />; | ||
}, | ||
}; | ||
const schema = { | ||
type: "object", | ||
properties: { | ||
fieldA: { | ||
type: "string", | ||
}, | ||
fieldB: { | ||
type: "string", | ||
}, | ||
}, | ||
}; | ||
const uiSchema = {}; | ||
let { node } = createComponent(withTheme({ fields }), { | ||
schema, | ||
uiSchema, | ||
}); | ||
expect(node.querySelectorAll(".string-field")).to.have.length.of(2); | ||
}); | ||
|
||
it("should use withTheme field and the user defined field", () => { | ||
const themeFields = { | ||
StringField() { | ||
return <div className="string-field" />; | ||
}, | ||
}; | ||
const userFields = { | ||
NumberField() { | ||
return <div className="number-field" />; | ||
}, | ||
}; | ||
const schema = { | ||
type: "object", | ||
properties: { | ||
fieldA: { | ||
type: "string", | ||
}, | ||
fieldB: { | ||
type: "number", | ||
}, | ||
}, | ||
}; | ||
const uiSchema = {}; | ||
let { node } = createComponent(withTheme({ fields: themeFields }), { | ||
schema, | ||
uiSchema, | ||
fields: userFields, | ||
}); | ||
expect(node.querySelectorAll(".string-field")).to.have.length.of(1); | ||
expect(node.querySelectorAll(".number-field")).to.have.length.of(1); | ||
}); | ||
|
||
it("should use only the user defined field", () => { | ||
const themeFields = { | ||
StringField() { | ||
return <div className="string-field" />; | ||
}, | ||
}; | ||
const userFields = { | ||
StringField() { | ||
return <div className="form-control" />; | ||
}, | ||
}; | ||
const schema = { | ||
type: "object", | ||
properties: { | ||
fieldA: { | ||
type: "string", | ||
}, | ||
fieldB: { | ||
type: "string", | ||
}, | ||
}, | ||
}; | ||
const uiSchema = {}; | ||
let { node } = createComponent(withTheme({ fields: themeFields }), { | ||
schema, | ||
uiSchema, | ||
fields: userFields, | ||
}); | ||
expect(node.querySelectorAll(".string-field")).to.have.length.of(0); | ||
expect(node.querySelectorAll(".form-control")).to.have.length.of(2); | ||
}); | ||
}); | ||
|
||
describe("With widgets", () => { | ||
it("should use the withTheme widget", () => { | ||
const widgets = { | ||
TextWidget: () => <div id="test" />, | ||
}; | ||
const schema = { | ||
type: "string", | ||
}; | ||
const uiSchema = {}; | ||
let { node } = createComponent(withTheme({ widgets }), { | ||
schema, | ||
uiSchema, | ||
}); | ||
expect(node.querySelectorAll("#test")).to.have.length.of(1); | ||
}); | ||
|
||
it("should use the withTheme widget as well as user defined widget", () => { | ||
const themeWidgets = { | ||
TextWidget: () => <div id="test-theme-widget" />, | ||
}; | ||
const userWidgets = { | ||
DateWidget: () => <div id="test-user-widget" />, | ||
}; | ||
const schema = { | ||
type: "object", | ||
properties: { | ||
fieldA: { | ||
type: "string", | ||
}, | ||
fieldB: { | ||
format: "date", | ||
type: "string", | ||
}, | ||
}, | ||
}; | ||
const uiSchema = {}; | ||
let { node } = createComponent(withTheme({ widgets: themeWidgets }), { | ||
schema, | ||
uiSchema, | ||
widgets: userWidgets, | ||
}); | ||
expect(node.querySelectorAll("#test-theme-widget")).to.have.length.of(1); | ||
expect(node.querySelectorAll("#test-user-widget")).to.have.length.of(1); | ||
}); | ||
|
||
it("should use only the user defined widget", () => { | ||
const themeWidgets = { | ||
TextWidget: () => <div id="test-theme-widget" />, | ||
}; | ||
const userWidgets = { | ||
TextWidget: () => <div id="test-user-widget" />, | ||
}; | ||
const schema = { | ||
type: "object", | ||
properties: { | ||
fieldA: { | ||
type: "string", | ||
}, | ||
}, | ||
}; | ||
const uiSchema = {}; | ||
let { node } = createComponent(withTheme({ widgets: themeWidgets }), { | ||
schema, | ||
uiSchema, | ||
widgets: userWidgets, | ||
}); | ||
expect(node.querySelectorAll("#test-theme-widget")).to.have.length.of(0); | ||
expect(node.querySelectorAll("#test-user-widget")).to.have.length.of(1); | ||
}); | ||
}); | ||
|
||
describe("With templates", () => { | ||
it("should use the withTheme template", () => { | ||
const themeTemplates = { | ||
FieldTemplate() { | ||
return <div className="with-theme-field-template" />; | ||
}, | ||
}; | ||
const schema = { | ||
type: "object", | ||
properties: { | ||
fieldA: { | ||
type: "string", | ||
}, | ||
fieldB: { | ||
type: "string", | ||
}, | ||
}, | ||
}; | ||
const uiSchema = {}; | ||
let { node } = createComponent(withTheme({ ...themeTemplates }), { | ||
schema, | ||
uiSchema, | ||
}); | ||
expect( | ||
node.querySelectorAll(".with-theme-field-template") | ||
).to.have.length.of(1); | ||
}); | ||
|
||
it("should use only the user defined template", () => { | ||
const themeTemplates = { | ||
FieldTemplate() { | ||
return <div className="with-theme-field-template" />; | ||
}, | ||
}; | ||
const userTemplates = { | ||
FieldTemplate() { | ||
return <div className="user-field-template" />; | ||
}, | ||
}; | ||
|
||
const schema = { | ||
type: "object", | ||
properties: { foo: { type: "string" }, bar: { type: "string" } }, | ||
}; | ||
let { node } = createComponent(withTheme({ ...themeTemplates }), { | ||
schema, | ||
...userTemplates, | ||
}); | ||
expect( | ||
node.querySelectorAll(".with-theme-field-template") | ||
).to.have.length.of(0); | ||
expect(node.querySelectorAll(".user-field-template")).to.have.length.of( | ||
1 | ||
); | ||
}); | ||
}); | ||
}); |
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.
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.