自定义样式到使用react-json模式表单创建的表单

时间:2019-04-08 11:01:09

标签: reactjs react-jsonschema-forms

我正在尝试使用react-json schema-form创建一个表单。我是自定义模板的新手。我想将所有小部件都放在一行中。该怎么做?

我尝试了以下(组件),该组件来自其网站上的自定义对象,但无法获得所需的结果。

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;

我不知道如何输入字段,选择窗口小部件以及同一行中的按钮。到目前为止,它看起来像是默认格式,一行一行。

2 个答案:

答案 0 :(得分:0)

您可以通过其模板自定义每个字段的外观。鉴于表单提交为对象,您需要调整ObjectFieldTemplate: https://react-jsonschema-form.readthedocs.io/en/latest/advanced-customization/#object-field-template

实际上,如果您去他们的操场(https://mozilla-services.github.io/react-jsonschema-form/,“自定义对象”标签链接位于顶部),您将在一行中看到所有字段(如果屏幕分辨率足够高,否则它们将包装成后续的行)。其实现该效果的源代码(通过自定义ObjectFieldTemplate组件(位于此处: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>
  );
}

答案 1 :(得分:0)

我使用了customFieldTemplate和flex-box,可以使其连续显示

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>
    );
};