SwitchStacked的onChange事件如何在模态框之后继续?

huangapple go评论94阅读模式
英文:

How the onChange event of a SwitchStacked can continue after modal?

问题

我有一个开关(Switch),当用户将其关闭时,我想显示一个模态框(Modal)警告,其中包括取消和确认按钮。一切都运行良好,我的模态框显示出来了,但是当我按下确认按钮时,我不知道如何继续执行期望的onChange事件,并将开关的值设置为关闭。(在下面的代码中由//TODO标记)

我在子类DetailsForm.js中有一个SwitchStacked组件。

在父类Details.js中,我像这样调用子DetailsForm:

<DetailsForm
    fieldProps={formCommonProps}
    selectProps={selectCommonProps}
    model={model}
    modules={modules}
    areas={areas}
    errors={errors}
    openModal={this.openStatusModal}
    // 将onChange属性传递给DetailsForm组件
    onChange={event => {
        const checked = event.target.checked;
        if (!checked) {
            this.openStatusModal(event); // 触发openModal函数
        }
    }}
/>

openStatusModal函数位于父级Details.js中:

openStatusModal = event => {
    const { dispatch } = this.props;

    const target = event.target;

    dispatch(
        openModal({
            type: 'info',
            title: literals.roles_modal_delete_warning_title,
            actions: {
                general_modal_cancel: () => dispatch(closeModal()),
                general_modal_confirm: () => {
                    dispatch(closeModal());

                    target.checked = false; //TODO:在界面中不起作用,没有错误,没有刷新。我该如何将开关设置为关闭?
                },
            },
        })
    );
};

在DetailsForm.js中,我的SwitchStacked如下:

<SwitchStacked
    {...fieldProps}
    formLabelText={literals.profiles_details_status}
    label={model.isActive ? literals.groups_details_enabled : literals.groups_details_disabled}
    checked={model.isActive}
    onChange={onChange}
    inputProps={{
        id: 'isActive',
        value: !model.isActive,
        'data-boolean': true,
    }}
/>

请注意,我正在使用较旧版本的React。

英文:

I have a Switch and I want when the user Switches it to Off to display a Modal warning with Cancel and Confirm buttons. Everything works fine, my modal is displayed but when I press the Confirm button I don't know how to make the onChange event continue as expected and set the Switch value to Off. (marked by //TODO in the code below)

I have a SwitchStacked component in my child class called DetailsForm.js.

In my parent class called Details.js I call the child DetailsForm like this:

               &lt;DetailsForm
                    fieldProps={formCommonProps}
                    selectProps={selectCommonProps}
                    model={model}
                    modules={modules}
                    areas={areas}
                    errors={errors}
                    openModal={this.openStatusModal}
                    // Pass the onChange prop to the DetailsForm component
                    onChange={event =&gt; {
                        const checked = event.target.checked;
                        if (!checked) {
                            this.openStatusModal(event); // Trigger the openModal function
                        }
                    }}
                /&gt;

The openStatusModal function is inside the parent Details.js:

openStatusModal = (event) =&gt; {
    const { dispatch } = this.props;

    const target = event.target;

    dispatch(
        openModal({
            type: &#39;info&#39;,
            title: literals.roles_modal_delete_warning_title,
            actions: {
                general_modal_cancel: () =&gt; dispatch(closeModal()),
                general_modal_confirm: () =&gt; {
                    dispatch(closeModal());

                    target.checked = false; //TODO: Does not work in ui, no errors, no refresh. How can I make the switch set to off?
                },
            },
        })
    );
};

Inside DetailsForm.js my SwitchStacked is like this:

       &lt;SwitchStacked
            {...fieldProps}
            formLabelText={literals.profiles_details_status}
            label={model.isActive ? literals.groups_details_enabled : literals.groups_details_disabled}
            checked={model.isActive}
            onChange={onChange}
            inputProps={{
                id: &#39;isActive&#39;,
                value: !model.isActive,
                &#39;data-boolean&#39;: true,
            }}
        /&gt;

Please note that I am using an older version of react

答案1

得分: 1

看起来你试图在模态框的确认操作中外部控制开关的状态,这可能不会按预期工作。

标准的React方法是将状态提升到父组件并从那里控制开关。这样,你可以通过设置父组件的状态有效地“继续”onChange事件,然后反映在开关上。

不确定你是否根据代码中的dispatch函数使用Redux。这里是一种可能的方法:

首先,在你的父组件Details.js中:

constructor(props) {
    super(props);
    this.state = {
        isActive: true, // 或者根据你的方式获取初始值
    };
}

然后,在你的openStatusModal函数中,当点击确认按钮时将isActive设置为false:

openStatusModal = (event) => {
    const { dispatch } = this.props;

    dispatch(
        openModal({
            type: 'info',
            title: literals.roles_modal_delete_warning_title,
            actions: {
                general_modal_cancel: () => dispatch(closeModal()),
                general_modal_confirm: () => {
                    dispatch(closeModal());
                    this.setState({ isActive: false });
                },
            },
        })
    );
};

在你的DetailsForm调用中 - 带有onchange事件:

<DetailsForm
    fieldProps={formCommonProps}
    selectProps={selectCommonProps}
    model={model}
    modules={modules}
    areas={areas}
    errors={errors}
    openModal={this.openStatusModal}
    isActive={this.state.isActive}
    setActive={(value) => this.setState({ isActive: value })}
    onChange={(event) => {
        const checked = event.target.checked;
        if (!checked) {
            this.openStatusModal(event);
        } else {
            this.setState({ isActive: true });
        }
    }}
/>

最后,在你的DetailsForm.js文件中,使用isActive属性来控制开关的选中值:

<SwitchStacked
    {...fieldProps}
    formLabelText={literals.profiles_details_status}
    label={model.isActive ? literals.groups_details_enabled : literals.groups_details_disabled}
    checked={this.props.isActive}
    onChange={onChange}
    inputProps={{
        id: 'isActive',
        value: !this.props.isActive,
        'data-boolean': true,
    }}
/>

希望这能帮助你控制开关的状态。

英文:

It seems that you're trying to control the state of the switch externally in the modal's confirm action, which might not work as expected.

The standard React way of doing this would be to lift the state up to the parent component and control the switch from there. This way, you can effectively "continue" the onChange event by setting the state of the parent component, which would then reflect on the switch.

Not sure if your using Redux based on the dispatch function in your code. Here is a possible way to do this:

First, in your parent component Details.js:

constructor(props) {
    super(props);
    this.state = {
        isActive: true, // Or however you get your initial value
    };
}

Then, in your openStatusModal function, you set isActive to false when the confirm button is clicked:

openStatusModal = (event) =&gt; {
    const { dispatch } = this.props;

    dispatch(
        openModal({
            type: &#39;info&#39;,
            title: literals.roles_modal_delete_warning_title,
            actions: {
                general_modal_cancel: () =&gt; dispatch(closeModal()),
                general_modal_confirm: () =&gt; {
                    dispatch(closeModal());
                    this.setState({isActive: false});
                },
            },
        })
    );
};

In your DetailsForm invocation - with an onchange event:

&lt;DetailsForm
    fieldProps={formCommonProps}
    selectProps={selectCommonProps}
    model={model}
    modules={modules}
    areas={areas}
    errors={errors}
    openModal={this.openStatusModal}
    isActive={this.state.isActive}
    setActive={(value) =&gt; this.setState({isActive: value})}
    onChange={event =&gt; {
        const checked = event.target.checked;
        if (!checked) {
            this.openStatusModal(event);
        } else {
            this.setState({isActive: true});
        }
    }}
/&gt;

Finally, in your DetailsForm.js file, use the isActive prop to control the checked value of the switch:

&lt;SwitchStacked
    {...fieldProps}
    formLabelText={literals.profiles_details_status}
    label={model.isActive ? literals.groups_details_enabled : literals.groups_details_disabled}
    checked={this.props.isActive}
    onChange={onChange}
    inputProps={{
        id: &#39;isActive&#39;,
        value: !this.props.isActive,
        &#39;data-boolean&#39;: true,
    }}
/&gt;

huangapple
  • 本文由 发表于 2023年7月13日 19:49:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定