Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(element): fix Radio.Group's change event is triggered twice, Cause functional failure. #3641 #3665

Merged
merged 1 commit into from
Jan 6, 2023

Conversation

ZhiLinLiang
Copy link
Contributor

Before submitting a pull request, please make sure the following is done...

  • Ensure the pull request title and commit message follow the Commit Specific in English.
  • Fork the repo and create your branch from master or formily_next.
  • If you've added code that should be tested, add tests!
  • If you've changed APIs, update the documentation.
  • Ensure the test suite passes (npm test).
  • Make sure your code lints (npm run lint) - we've done our best to make sure these rules match our internal linting guidelines.

Please do not delete the above content


What have you changed?

修复 ElRadioGroup 组件桥接后的 Radio.Group 组件,发生一次点击但触发两次 change 事件的现象,进而导致组件点击切换失效的问题。(ElCheckboxGroup桥接后失效的问题的原因和修复方法可以参考本问题方案)
@formily/elementradio 组件中,其桥接ElRadioGroup的代码逻辑如下:

// @formily\element\esm\radio\index.js
var TransformElRadioGroup = transformComponent(ElRadioGroup, {
    change: 'input',
});

修复结果:

// @formily\element\esm\radio\index.js
var TransformElRadioGroup = transformComponent(ElRadioGroup, {
    change: 'input',
    uselessChange: 'change',
});

解决方法:

在桥接ElRadioGroup组件的时候,屏蔽其 change 事件的干扰即可。

具体原因:

首先,在element-uiElRadio组件的change事件,触发其 modelsetterhandleChange ,而modelsetter 会触发 ElRadioGroup 组件的 input 事件,最后触发 Radio.Groupchange事件,更新Radio.Groupvalue

其次,由于 @formilyschedulerPromise, 所以 handleChange执行的优先级更高,进而二次触发ElRadioGroupchange事件,最后二次触发 Radio.Groupchange事件,更新Radio.Groupvalue

最后,由于 ElRadioGrouphandleChange 事件获得的值this.model 来源于计算属性,而此时@formilyschedulerreactiveRender 尚未执行。 所以此时 ElRadioGroup 组件尚未 update ,所以导致 handleChange 函数执行时,this.model 获取的值是缓存值,即改变前的旧值(与 ElRadioGroup input 事件获得的值不一致)。最终导致Radio.Group的选项切换失败。

// element-ui\packages\radio\src\radio.vue
 methods: {
      handleChange() {
        this.$nextTick(() => {
          this.$emit('change', this.model);
          this.isGroup && this.dispatch('ElRadioGroup', 'handleChange', this.model);
        });
      }
    }
// element-ui\packages\radio\src\radio.vue
 computed: {
      isGroup() {
        let parent = this.$parent;
        while (parent) {
          if (parent.$options.componentName !== 'ElRadioGroup') {
            parent = parent.$parent;
          } else {
            this._radioGroup = parent;
            return true;
          }
        }
        return false;
      },
      model: {
        get() {
          return this.isGroup ? this._radioGroup.value : this.value;
        },
        set(val) {
          if (this.isGroup) {
            this.dispatch('ElRadioGroup', 'input', [val]);
          } else {
            this.$emit('input', val);
          }
          this.$refs.radio && (this.$refs.radio.checked = this.model === this.label);
        }
      },
}

修复由 ElRadioGroup 组件的 change 事件,导致桥接后的 Radio.Group 组件,发生一次点击但触发两次 change 事件的现象,进而导致组件点击切换失效的问题。
@CLAassistant
Copy link

CLAassistant commented Jan 6, 2023

CLA assistant check
All committers have signed the CLA.

@ZhiLinLiang
Copy link
Contributor Author

ZhiLinLiang commented Jan 6, 2023 via email

@janryWang
Copy link
Collaborator

commit message 不符合规范

@codecov
Copy link

codecov bot commented Jan 6, 2023

Codecov Report

Base: 96.57% // Head: 96.57% // No change to project coverage 👍

Coverage data is based on head (c44249f) compared to base (f54ccfb).
Patch has no changes to coverable lines.

Additional details and impacted files
@@              Coverage Diff              @@
##           formily_next    #3665   +/-   ##
=============================================
  Coverage         96.57%   96.57%           
=============================================
  Files               152      152           
  Lines              6603     6603           
  Branches           1836     1836           
=============================================
  Hits               6377     6377           
  Misses              226      226           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants