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(radio): 选项内容变化后样式问题修复 #2936

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/radio/group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ export default mixins(classPrefixMixins).extend({
mounted() {
this.calcBarStyle();
const observer = new MutationObserver(this.calcBarStyle);
observer.observe(this.$el, { childList: true, attributes: true, subtree: true });
observer.observe(this.$el, {
childList: true,
attributes: true,
subtree: true,
characterData: true,
});
this.observer = observer;
this.addKeyboardListeners();
},
Expand Down Expand Up @@ -137,7 +142,7 @@ export default mixins(classPrefixMixins).extend({
emitEvent<Parameters<TdRadioGroupProps['onChange']>>(this, 'change', value, context);
},

calcDefaultBarStyle(): void {
calcDefaultBarStyle() {
const defaultNode = this.$el.cloneNode(true);
const div = document.createElement('div');
div.setAttribute('style', 'position: absolute; visibility: hidden;');
Expand All @@ -146,22 +151,26 @@ export default mixins(classPrefixMixins).extend({

const defaultCheckedRadio: HTMLElement = div.querySelector(this.checkedClassName);
const { offsetWidth, offsetLeft } = defaultCheckedRadio;
this.barStyle = { width: `${offsetWidth}px`, left: `${offsetLeft}px` };
document.body.removeChild(div);
return { offsetWidth, offsetLeft };
},
calcBarStyle(): void {
if (this.variant === 'outline') return;

const checkedRadio: HTMLElement = this.$el.querySelector(this.checkedClassName);
if (!checkedRadio) return;

const { offsetWidth, offsetLeft } = checkedRadio;
let { offsetWidth, offsetLeft } = checkedRadio;
// current node is not rendered,fallback to default render
if (!offsetWidth) {
this.calcDefaultBarStyle();
} else {
this.barStyle = { width: `${offsetWidth}px`, left: `${offsetLeft}px` };
const { offsetWidth: width, offsetLeft: left } = this.calcDefaultBarStyle();
offsetWidth = width;
offsetLeft = left;
}
const width = `${offsetWidth}px`;
const left = `${offsetLeft}px`;
if (this.barStyle.width === width && this.barStyle.left === left) return;
this.barStyle = { width, left };
},
},
});
Loading