Skip to content

Commit c74efb5

Browse files
committed
fix(radio): fix radio warning (#3685)
1 parent 8fa581c commit c74efb5

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/radio/group.tsx

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { CreateElement, VNode, ref } from 'vue';
1+
import { CreateElement, VNode } from 'vue';
22
import { isNumber, isString, throttle } from 'lodash-es';
33
import { CHECKED_CODE_REG } from '../_common/js/common';
44
import { TNodeReturnValue } from '../common';
55
import { getClassPrefixMixins } from '../config-provider/config-receiver';
6-
import useResizeObserver from '../hooks/useResizeObserver';
76
import { off, on } from '../utils/dom';
87
import { emitEvent } from '../utils/event';
98
import mixins from '../utils/mixins';
@@ -34,6 +33,7 @@ export default mixins(classPrefixMixins).extend({
3433

3534
data() {
3635
return {
36+
groupResizeObserver: null,
3737
barStyle: { width: '0px', left: '0px' },
3838
};
3939
},
@@ -100,19 +100,17 @@ export default mixins(classPrefixMixins).extend({
100100

101101
mounted() {
102102
this.calcBarStyle();
103-
this.addKeyboardListeners();
104-
105-
const radioGroupRef = ref(this.$refs.radioGroupRef as HTMLElement);
106-
useResizeObserver(
107-
radioGroupRef,
108-
throttle(() => {
103+
this.groupResizeObserver = this.addResizeObserver(
104+
this.$el,
105+
throttle(async () => {
109106
this.$nextTick(() => this.calcBarStyle());
110107
}, 300),
111108
);
112109
},
113110

114111
beforeDestroy() {
115112
this.removeKeyboardListeners();
113+
this.cleanupResizeObserver(this.groupResizeObserver, this.$el);
116114
},
117115

118116
methods: {
@@ -129,6 +127,21 @@ export default mixins(classPrefixMixins).extend({
129127
}
130128
},
131129

130+
addResizeObserver(el: Element, callback: (data: ResizeObserverEntry[]) => void): ResizeObserver {
131+
const isSupport = typeof window !== 'undefined' && window.ResizeObserver;
132+
if (!isSupport) return;
133+
134+
const containerObserver = new ResizeObserver(callback);
135+
containerObserver.observe(el);
136+
137+
return containerObserver;
138+
},
139+
cleanupResizeObserver(observer: ResizeObserver, container: Element) {
140+
if (!observer || !container) return;
141+
observer.unobserve(container);
142+
observer.disconnect();
143+
},
144+
132145
// 注意:此处会还原区分 数字 和 数字字符串
133146
checkRadioInGroup(e: KeyboardEvent) {
134147
const isCheckedCode = CHECKED_CODE_REG.test(e.key) || CHECKED_CODE_REG.test(e.code);

0 commit comments

Comments
 (0)