Browse Source

fix: add useRef hook

pull/4134/head
Sendya 4 years ago
parent
commit
7ec594c5fc
  1. 14
      components/_util/hooks/useRef.ts
  2. 13
      components/rate/index.tsx
  3. 2
      v2-doc

14
components/_util/hooks/useRef.ts

@ -0,0 +1,14 @@
import { onBeforeUpdate, readonly, ref, DeepReadonly, UnwrapRef } from 'vue';
export type UseRef = [(el: any) => void, DeepReadonly<UnwrapRef<any[]>>];
export const useRef = (): UseRef => {
const refs = ref<any>([]);
const setRef = (el: any) => {
refs.value.push(el);
};
onBeforeUpdate(() => {
refs.value = [];
});
return [setRef, readonly(refs)];
};

13
components/rate/index.tsx

@ -19,6 +19,7 @@ import Tooltip from '../tooltip';
import useConfigInject from '../_util/hooks/useConfigInject';
import Star from './Star';
import { useRef } from '../_util/hooks/useRef';
export const rateProps = {
prefixCls: PropTypes.string,
@ -52,22 +53,16 @@ const Rate = defineComponent({
setup(props, { slots, attrs, emit, expose }) {
const { prefixCls, direction } = useConfigInject('rate', props);
const rateRef = ref();
const starRefs = ref([]);
const [setRef, starRefs] = useRef();
const state = reactive({
sValue: props.value,
focused: false,
cleanedValue: null,
hoverValue: undefined,
});
const saveRef = (el: any) => {
starRefs.value.push(el);
};
onBeforeUpdate(() => {
starRefs.value = [];
});
const getStarDOM = index => {
return findDOMNode(starRefs.value[index]);
return findDOMNode(starRefs[index]);
};
const getStarValue = (index, x) => {
const reverse = direction.value === 'rtl';
@ -200,7 +195,7 @@ const Rate = defineComponent({
for (let index = 0; index < count; index++) {
stars.push(
<Star
ref={saveRef}
ref={setRef}
key={index}
index={index}
count={count}

2
v2-doc

@ -1 +1 @@
Subproject commit 0f6d531d088d5283250c8cec1c7e8be0e0d36a36
Subproject commit 4f1ece5073f736e79c6eb22527a9a83e8c6182b3
Loading…
Cancel
Save