Skip to content

Commit 9b699da

Browse files
authored
fix(radio): fix radio warning (#3685)
1 parent 84b700c commit 9b699da

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/radio/group.tsx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { VNode, CreateElement } from 'vue';
22
import { isNumber, isString, throttle } from 'lodash-es';
3-
import { ref } from '@vue/composition-api';
4-
import useResizeObserver from '../hooks/useResizeObserver';
53
import props from './radio-group-props';
64
import {
75
TdRadioGroupProps, RadioOptionObj, RadioOption, RadioValue,
@@ -35,7 +33,7 @@ export default mixins(classPrefixMixins).extend({
3533

3634
data() {
3735
return {
38-
radioGroupEl: ref(null),
36+
groupResizeObserver: null,
3937
barStyle: { width: '0px', left: '0px' },
4038
};
4139
},
@@ -98,9 +96,8 @@ export default mixins(classPrefixMixins).extend({
9896

9997
mounted() {
10098
this.calcBarStyle();
101-
this.radioGroupEl.value = this.$el;
102-
useResizeObserver(
103-
this.radioGroupEl,
99+
this.groupResizeObserver = this.addResizeObserver(
100+
this.$el,
104101
throttle(async () => {
105102
this.$nextTick(() => this.calcBarStyle());
106103
}, 300),
@@ -111,6 +108,7 @@ export default mixins(classPrefixMixins).extend({
111108

112109
beforeDestroy() {
113110
this.removeKeyboardListeners();
111+
this.cleanupResizeObserver(this.groupResizeObserver, this.$el);
114112
},
115113

116114
methods: {
@@ -123,6 +121,21 @@ export default mixins(classPrefixMixins).extend({
123121
off(this.$el, 'keydown', this.checkRadioInGroup);
124122
},
125123

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

0 commit comments

Comments
 (0)