【问题标题】:How to richly style AOR Edit page如何丰富样式的 AOR 编辑页面
【发布时间】:2017-07-05 14:01:31
【问题描述】:

必须创建一个编辑页面,在“故事”资源的实例上编辑多个参数。

但是,添加任何元素(例如 MUI 卡甚至 div)都会导致应用以各种方式冻结。

这些是我尝试过的方法。

1) 添加卡片组件或将我的元素放置在 div 中以进行样式设置

export const EditorEditTale = (props) => {
  return (
  <Edit {...props} title="Tale Editor">
    <SimpleForm >
      <div>
        <Image />
        <TaleCardHeader props={ props } style={taleCardHeaderStyle.editor} />
      </div>
    </SimpleForm>
  </Edit>
  )
};

这不会导致任何渲染。

第二种方法,假设记录和 basePath 没有完全传播给孩子。尝试使用如下组件。

const Input = ({record, basePath}) => {
  return (
    <div>
      <LongTextInput source="taleText" />
    </div>
  )
}

这导致页面无法在某种锁定循环中呈现所有内容并出现错误 - 无法读取未定义的属性。

我应该如何创建具有复杂输入和样式的自定义编辑页面。

更新:一直在尝试编写自定义表单来替换 SimpleForm 组件,但到目前为止没有运气。

【问题讨论】:

    标签: redux-form admin-on-rest


    【解决方案1】:

    要创建自定义表单,您可以按照以下步骤操作:

    1. SimpleForm 复制到您的项目中。
    2. 将 SimpleForm 重命名为您想要的名称。
    3. 修复所有相关导入。
    4. 测试新表单,直到它起作用为止。

    我根据当前master分支的SimpleForm做了一个最小工作表

    import React, { Children, Component } from 'react';
    import PropTypes from 'prop-types';
    import { reduxForm, Field } from 'redux-form';
    import { connect } from 'react-redux';
    import compose from 'recompose/compose';
    import getDefaultValues from 'admin-on-rest/mui/form/getDefaultValues';
    import FormField from 'admin-on-rest/mui/form/FormField';
    import Toolbar from 'admin-on-rest/mui/form/Toolbar';
    
    const formStyle = { padding: '0 1em 1em 1em' };
    
    export class PostForm extends Component {
        handleSubmitWithRedirect = (redirect = this.props.redirect) => this.props.handleSubmit(values => this.props.save(values, redirect));
    
        render() {
            const { children, invalid, record, resource, basePath, submitOnEnter, toolbar } = this.props;
            return (
                <form className="simple-form">
                    <Field name="name_of_a_field" component="input" />
                    {toolbar && React.cloneElement(toolbar, {
                        handleSubmitWithRedirect: this.handleSubmitWithRedirect,
                        invalid,
                        submitOnEnter,
                    })}
                </form>
            );
        }
    }
    
    PostForm.propTypes = {
        basePath: PropTypes.string,
        children: PropTypes.node,
        defaultValue: PropTypes.oneOfType([
            PropTypes.object,
            PropTypes.func,
        ]),
        handleSubmit: PropTypes.func, // passed by redux-form
        invalid: PropTypes.bool,
        record: PropTypes.object,
        resource: PropTypes.string,
        redirect: PropTypes.oneOfType([
            PropTypes.string,
            PropTypes.bool,
        ]),
        save: PropTypes.func, // the handler defined in the parent, which triggers the REST submission
        submitOnEnter: PropTypes.bool,
        toolbar: PropTypes.element,
        validate: PropTypes.func,
    };
    
    PostForm.defaultProps = {
        submitOnEnter: true,
        toolbar: <Toolbar />,
    };
    
    const enhance = compose(
        connect((state, props) => ({
            initialValues: getDefaultValues(state, props),
        })),
        reduxForm({
            form: 'record-form',
            enableReinitialize: true,
        }),
    );
    
    export default enhance(PostForm);
    

    以上代码适用于 AOR 的example

    我希望这会有所帮助。

    (当您将 AOR 作为 npm 依赖项时,导入可能会略有不同:

    import getDefaultValues from 'admin-on-rest/lib/mui/form/getDefaultValues';
    import FormField from 'admin-on-rest/lib/mui/form/FormField';
    import Toolbar from 'admin-on-rest/lib/mui/form/Toolbar';
    

    )

    【讨论】:

    • 我在一天结束时已经成功编写了一个自定义表单。将建立在您的建议之上。再次感谢您的意见。
    【解决方案2】:

    记录我的最终答案。你必须创建一个自定义的 Redux 表单。您可以直接使用 AOR 输入组件。它们为 Redux Form 预先包装。

    import { Field, reduxForm } from 'redux-form';
    import compose from 'recompose/compose';
    import { connect } from 'react-redux';
    
    class StyledForm extends Component {
    // Newer version of aor needs this function defined and passed to save buttons. All props are being passed by parent List component. 
    handleSubmitWithRedirect = (redirect = this.props.redirect) => this.props.handleSubmit(values => this.props.save(values, redirect));
    
    render() {  
    const { handleSubmit, invalid, record, resource, basePath } = this.props
    return (<div>
        <form onSubmit={handleSubmit} >
          <Card >
            <CardText >
    
              //This component simply displays data, something not possible very easily with SimpleForm. 
              <HeaderComp basePath={basePath} record={record} />
    
              <Field source="category_id"
                     optionText="categoryName"
                     reference="categories"
                     resource={resource}
                     record={record}
                     basePath={basePath}
                     name="NAME OF THE FIELD IN YOUR REDUX DATASTORE"
                     component={REFERENCEFIELDCOMP} />
              //create complex div structures now. 
              <div >
                <span>Tale</span>
                <Field resource={resource} record={record} basePath={basePath} name="taleText" component={TextInput} />
              </div>
    
             </CardText >
    
              <MuiToolbar>
                <ToolbarGroup>
                  <SaveButton handleSubmitWithRedirect={this.handleSubmitWithRedirect}/>
                  //Add custom buttons with custom actions
                  <Field record={record} name="status" component={EditButtons} />
    
                </ToolbarGroup>
              </MuiToolbar>
          </Card>
        </form>
      </div>)
        }
    };
    
    const enhance = compose(
        connect((state, props) => ({
            initialValues: getDefaultValues(state, props),
        })),
        reduxForm({
            form: 'record-form',
            enableReinitialize: true,
        }),
    );
    
    export default enhance(StyledForm);
    

    您必须从节点模块中的 AOR 导入或复制 getDefaultValues。 我把它复制到下面的文件中。

    import getDefaultValues from '../functions/getDefaultValues';
    

    如果您需要在您的领域中使用 referenceField。然后将其包装在如下所示的自定义组件中

    const DropDownSelector = ({optionText, ...props}) => {
      return (
          <ReferenceInput {...props} label="" >
            <SelectInput optionText={optionText} />
          </ReferenceInput>
      )
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 2014-11-30
      • 2010-10-20
      相关资源
      最近更新 更多