在删除之前,请使用确认对话框

发布于 2025-01-23 01:12:16 字数 6214 浏览 5 评论 0原文

我想添加React-Admin一个确认消息,例如“您想删除项目吗?”。

因此,我尝试使用 < deletebutton/>带有undoable = {false} 选项和 < deletewithConfirmbutton/> component

但是他们俩都会遇到这样的错误。

unturect typeError:无法读取未定义的属性(阅读'tolowercase')

在object.humanize(furnection.js:828:1)
在DeletewithConfirmbutton(DeletewithConfirmbutton.js:53:1)
在Renderwithhooks(react-dom.development.js:14803:1)。 。 。

问题

看起来像&lt; deletewithConfirmbutton /&gt; < /code>是一个问题。 (&lt; deletebutton undoable = {false} /&gt; < /code>还调用&lt; deletewithConfirmbutton /&

  1. gt; < /code> in northly。 。

  2. 他们调用fundection.humanize &lt;确认.. translateOptions = {} .../&gt;

  3. fundection.humanize使用tolowercase()方法,但是translateOption dose dose dose dose dove dast prospor string(resources。 } .name)。

  4. 发生错误。

是否因为没有资源。$ {Resource} .Name在我的道具中发生吗?我该如何修复?

我的代码

const BottomToolbar = (props: EditActionsProps) => {

  return (
  // eslint-disable-next-line react/jsx-props-no-spreading
    <Toolbar {...props}>
      <SaveButton style={toolBarStyle} label="member.edit.editButton" />
      <DeleteButton label="member.edit.deleteButton" />
      <DeleteWithConfirmButton />
    </Toolbar>
  );
};
export interface EditActionsProps {
    basePath?: string;
    className?: string;
    data?: Record;
    hasCreate?: boolean;
    hasEdit?: boolean;
    hasList?: boolean;
    hasShow?: boolean;
    resource?: string;
}
// my props looks like this
record:
    firstSeenAt: t {seconds: 1502878077, nanoseconds: 0}
    groupId: "ldjapon18073hk"
    id: "pQ--------"
    lastSeenAt: t {seconds: 16000000, nanoseconds: 5700000000}
    managerId: "Me------"
    memberName: "xxx"
    totalTrainedTimeInSec: 37.882999999999996
redirect: "list"
resource: "members"

此代码是&lt; deletewithConfirmbutton/&gt;组件代码。

const {
        basePath,
        classes: classesOverride,
        className,
        confirmTitle = 'ra.message.delete_title',
        confirmContent = 'ra.message.delete_content',
        icon = defaultIcon,
        label = 'ra.action.delete',
        mutationMode,
        onClick,
        record,
        redirect = 'list',
        onSuccess,
        onFailure,
        ...rest
    } = props;
    const translate = useTranslate();
    const classes = useStyles(props);
    const resource = useResourceContext(props);
    const {
        open,
        loading,
        handleDialogOpen,
        handleDialogClose,
        handleDelete,
    } = useDeleteWithConfirmController({
        record,
        redirect,
        basePath,
        mutationMode,
        onClick,
        onSuccess,
        onFailure,
        resource,
    });
return (
        <Fragment>
            <Button
                onClick={handleDialogOpen}
                label={label}
                className={classnames(
                    'ra-delete-button',
                    classes.deleteButton,
                    className
                )}
                key="button"
                {...rest}
            >
                {icon}
            </Button>
            <Confirm
                isOpen={open}
                loading={loading}
                title={confirmTitle}
                content={confirmContent}
                /* Error occur here! */
                translateOptions={{
                    name: translate(`resources.${resource}.forcedCaseName`, {
                        smart_count: 1,
                        _: inflection.humanize(
                            translate(`resources.${resource}.name`, {
                                smart_count: 1,
                                _: inflection.singularize(resource),
                            }),
                            true
                        ),
                    }),
                    id: record.id,
                }}
                onConfirm={handleDelete}
                onClose={handleDialogClose}
            />
        </Fragment>
    );

这是furnection.js

**/**
   * This function adds humanize support to every String object.
   * @public
   * @function
   * @param {String} str The subject string.
   * @param {Boolean} low_first_letter Default is to capitalize the first letter of the results.(optional)
   *                                 Passing true will lowercase it.
   * @returns {String} Lower case underscored words will be returned in humanized form.
   * @example
   *
   *     var inflection = require( 'inflection' );
   *
   *     inflection.humanize( 'message_properties' ); // === 'Message properties'
   *     inflection.humanize( 'message_properties', true ); // === 'message properties'
   */
    humanize : function ( str, low_first_letter ){
      str = str.toLowerCase(); // Line 828 : Error Occur in here!
      str = str.replace( id_suffix, '' );
      str = str.replace( underbar, ' ' );

      if( !low_first_letter ){
        str = inflector.capitalize( str );
      }

      return str;
    },
**

I want to add react-admin a confirmation message like 'Do you want to delete item?'.

So I tried to use <DeleteButton /> with undoable={false} option and <DeleteWithConfirmButton /> component.

But both of them keep occur an error like this.

Uncaught TypeError: Cannot read properties of undefined (reading 'toLowerCase')

at Object.humanize (inflection.js:828:1)
at DeleteWithConfirmButton (DeleteWithConfirmButton.js:53:1)
at renderWithHooks (react-dom.development.js:14803:1) . . .

Problem

It looks like <DeleteWithConfirmButton />is a problem. (<DeleteButton undoable={false} /> also call <DeleteWithConfirmButton /> internally.)

  1. Call <DeleteWithConfirmButton />.

  2. They call inflection.humanize inside the <Confirm .. translateOptions={} ... />.

  3. inflection.humanize use toLowerCase() method, but translateOption dosen't pass proper string(resources.${resource}.name).

  4. Error occurs.

Is this error occur because there's no resources.${resource}.name in my props? How can I fix it?

My code

const BottomToolbar = (props: EditActionsProps) => {

  return (
  // eslint-disable-next-line react/jsx-props-no-spreading
    <Toolbar {...props}>
      <SaveButton style={toolBarStyle} label="member.edit.editButton" />
      <DeleteButton label="member.edit.deleteButton" />
      <DeleteWithConfirmButton />
    </Toolbar>
  );
};
export interface EditActionsProps {
    basePath?: string;
    className?: string;
    data?: Record;
    hasCreate?: boolean;
    hasEdit?: boolean;
    hasList?: boolean;
    hasShow?: boolean;
    resource?: string;
}
// my props looks like this
record:
    firstSeenAt: t {seconds: 1502878077, nanoseconds: 0}
    groupId: "ldjapon18073hk"
    id: "pQ--------"
    lastSeenAt: t {seconds: 16000000, nanoseconds: 5700000000}
    managerId: "Me------"
    memberName: "xxx"
    totalTrainedTimeInSec: 37.882999999999996
redirect: "list"
resource: "members"

This code is <DeleteWithConfirmButton /> component code.

const {
        basePath,
        classes: classesOverride,
        className,
        confirmTitle = 'ra.message.delete_title',
        confirmContent = 'ra.message.delete_content',
        icon = defaultIcon,
        label = 'ra.action.delete',
        mutationMode,
        onClick,
        record,
        redirect = 'list',
        onSuccess,
        onFailure,
        ...rest
    } = props;
    const translate = useTranslate();
    const classes = useStyles(props);
    const resource = useResourceContext(props);
    const {
        open,
        loading,
        handleDialogOpen,
        handleDialogClose,
        handleDelete,
    } = useDeleteWithConfirmController({
        record,
        redirect,
        basePath,
        mutationMode,
        onClick,
        onSuccess,
        onFailure,
        resource,
    });
return (
        <Fragment>
            <Button
                onClick={handleDialogOpen}
                label={label}
                className={classnames(
                    'ra-delete-button',
                    classes.deleteButton,
                    className
                )}
                key="button"
                {...rest}
            >
                {icon}
            </Button>
            <Confirm
                isOpen={open}
                loading={loading}
                title={confirmTitle}
                content={confirmContent}
                /* Error occur here! */
                translateOptions={{
                    name: translate(`resources.${resource}.forcedCaseName`, {
                        smart_count: 1,
                        _: inflection.humanize(
                            translate(`resources.${resource}.name`, {
                                smart_count: 1,
                                _: inflection.singularize(resource),
                            }),
                            true
                        ),
                    }),
                    id: record.id,
                }}
                onConfirm={handleDelete}
                onClose={handleDialogClose}
            />
        </Fragment>
    );

This is inflection.js

**/**
   * This function adds humanize support to every String object.
   * @public
   * @function
   * @param {String} str The subject string.
   * @param {Boolean} low_first_letter Default is to capitalize the first letter of the results.(optional)
   *                                 Passing true will lowercase it.
   * @returns {String} Lower case underscored words will be returned in humanized form.
   * @example
   *
   *     var inflection = require( 'inflection' );
   *
   *     inflection.humanize( 'message_properties' ); // === 'Message properties'
   *     inflection.humanize( 'message_properties', true ); // === 'message properties'
   */
    humanize : function ( str, low_first_letter ){
      str = str.toLowerCase(); // Line 828 : Error Occur in here!
      str = str.replace( id_suffix, '' );
      str = str.replace( underbar, ' ' );

      if( !low_first_letter ){
        str = inflector.capitalize( str );
      }

      return str;
    },
**

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月竹挽风 2025-01-30 01:12:16

我认为,因为您没有给DeletewithConfirmbutton提供道具,因此它

在您的代码中有错误:

const BottomToolbar = (props: EditActionsProps) => {

  return (
  // eslint-disable-next-line react/jsx-props-no-spreading
    <Toolbar {...props}>
      <SaveButton style={toolBarStyle} label="member.edit.editButton" />
      <DeleteButton label="member.edit.deleteButton" />
      <DeleteWithConfirmButton props={props}/>
      // In here, you need provider prop for DeleteWithConfirmButton component
    </Toolbar>
  );
};

I think because you didn't give props to DeleteWithConfirmButton, so it has error

In your code:

const BottomToolbar = (props: EditActionsProps) => {

  return (
  // eslint-disable-next-line react/jsx-props-no-spreading
    <Toolbar {...props}>
      <SaveButton style={toolBarStyle} label="member.edit.editButton" />
      <DeleteButton label="member.edit.deleteButton" />
      <DeleteWithConfirmButton props={props}/>
      // In here, you need provider prop for DeleteWithConfirmButton component
    </Toolbar>
  );
};
狼亦尘 2025-01-30 01:12:16

对于在同一问题上挣扎的未来用户。

我在项目中使用```react-admin'':“ 3.13.4”。

deletewithConfirmbutton.proptypes 中没有,而且translateOptions也没有默认值。因此,我无法直接传递TranslateOptions,也没有默认值。这就是为什么我的错误发生。

值得庆幸的是,此错误已修复v4.0.0.0中。我更新我的版本和&lt; deletewithConfirmbutton /&gt; < /code> works。

For future users who struggle with same problem ..

I use `"react-admin": "3.13.4" in my project.

There's no translateOptions in DeleteWithConfirmButton.propTypes and also there's no default value for translateOptions. So I can't pass translateOptions directly and there's no default value. That's why my error occur.

Thankfully, this error fixed in v4.0.0. I update my version and <DeleteWithConfirmButton /> works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文