0

我正在尝试使用 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;

我不知道如何制作输入字段、选择小部件以及同一行中的按钮。到目前为止,它看起来就像一个接一个的默认形式。

4

2 回答 2

2

您可以通过其模板自定义每个字段的外观。鉴于表单作为对象提交,您需要调整 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>
  );
}
于 2019-05-12T21:22:55.270 回答
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>
    );
};

于 2019-07-08T11:12:53.440 回答