【问题标题】:Custom Style to a form created using react-json schema form自定义样式到使用 react-json 模式表单创建的表单
【发布时间】:2019-04-08 11:01:08
【问题描述】:

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

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

【问题讨论】:

    标签: reactjs react-jsonschema-forms


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我使用了 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>
          );
      };

      【讨论】:

        猜你喜欢
        • 2018-08-28
        • 2016-03-25
        • 1970-01-01
        • 1970-01-01
        • 2020-11-20
        • 2017-12-19
        • 1970-01-01
        • 1970-01-01
        • 2015-09-19
        相关资源
        最近更新 更多