如何丰富 AOR 编辑页面的样式

How to richly style AOR Edit page

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

但是,添加任何元素(例如 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 没有完全传播到 children。尝试使用如下组件。

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

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

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

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

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

  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';

)

记录我的最终答案。您必须创建自定义 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>
  )
}