'Custom Style to a form created using react-json schema form
I am trying to create a form using react-json schema-form. I am new to the custom templates for the same. I would like to have all the widgets in the form in a single row. How to do that ?
i have tried the following (component) , which was from the custom Object from their website, but couldn't get the desired result.
import React from 'react';
import Form from 'react-jsonschema-form';
/* this is my schma*/
const AdHocCheckSchema = {
title: "search",
type: "object",
required: ["searchKeyword", "country"],
properties: {
searchKeyWord: {
type: "string",
title: "Search Keyword"
},
country: {
type: "string",
title: "country",
enum: [
"a",
"b"
],
enumNames: [
"a",
"b"
]
}
}
};
/*this is the ui schema*/
const adHocCheckUiSchema = {
"ui:order": [
"searchKeyWord",
"country"
],
"country": {
"ui:widget": "select"
}
};
function CustomTemplate(props)
{
return (
<div>
{props.title}
{props.description}
{props.properties.map(
element =>
<div className="property-wrapper">{element.content}</div>)}
</div>
);
}
const AdHocCheckComponent = () => {
return (
<Form
className="tp-adhoccheck__horizontal"
schema={AdHocCheckSchema}
uiSchema={adHocCheckUiSchema}
CustomTemplate={CustomTemplate}
/>
);
};
export default AdHocCheckComponent;
i have no idea how to make the input field , select widget and also the button in same line. As of now its looking as in a default form one line after another.
Solution 1:[1]
You can customize the look and feel of each field via their templates. Given that the form submits as an object, you'd want to tweak the ObjectFieldTemplate: https://react-jsonschema-form.readthedocs.io/en/latest/advanced-customization/#object-field-template
In fact, if you go to their playground (https://mozilla-services.github.io/react-jsonschema-form/, "Custom Object" tab link on top), you'll see all the fields in a single row (if your screen resolution is high enough, otherwise they will wrap over into subsequent rows). Their source code for that effect (via a custom ObjectFieldTemplate component( is located here: https://github.com/mozilla-services/react-jsonschema-form/blob/master/playground/samples/customObject.js
function ObjectFieldTemplate({ TitleField, properties, title, description }) {
return (
<div>
<TitleField title={title} />
<div className="row">
{properties.map(prop => (
<div
className="col-lg-2 col-md-4 col-sm-6 col-xs-12"
key={prop.content.key}>
{prop.content}
</div>
))}
</div>
{description}
</div>
);
}
Solution 2:[2]
i used customFieldTemplate and flex-box and could make it in a row
export const customFieldTemplate = (props) => {
const {id, label, classNames, required, errors, children} = props;
return (
<div className={classNames}>
<label className="field_label" htmlFor={id}>
<span className="required-field">
{required ? '*' : null}
</span>
{label}
</label>
{children}
{errors}
</div>
);
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | mirage |
Solution 2 | roma |