rsuite
Version:
A suite of react components
65 lines • 1.73 kB
JavaScript
'use client';
import { useImperativeHandle, useRef } from 'react';
import { useEventCallback } from "../../hooks/index.js";
import { RSUITE_PICKER_TYPE } from "../../symbols.js";
/**
* A hook of the exposed method of Picker
*/
function usePickerRef(ref) {
const trigger = useRef(null);
const root = useRef(null);
const target = useRef(null);
const overlay = useRef(null);
const list = useRef(null);
const searchInput = useRef(null);
const treeView = useRef(null);
const handleOpen = useEventCallback(() => {
trigger?.current?.open();
});
const handleClose = useEventCallback(() => {
trigger?.current?.close();
});
const handleUpdatePosition = useEventCallback(() => {
trigger?.current?.updatePosition();
});
useImperativeHandle(ref, () => {
return {
get root() {
return (root?.current || trigger?.current?.root) ?? null;
},
get overlay() {
if (!overlay?.current) {
throw new Error('The overlay is not found. Please confirm whether the picker is open.');
}
return overlay?.current ?? null;
},
get target() {
return target?.current ?? null;
},
get list() {
if (!list?.current) {
throw new Error(`
The list is not found.
1.Please set virtualized for the component.
2.Please confirm whether the picker is open.
`);
}
return list?.current;
},
type: RSUITE_PICKER_TYPE,
updatePosition: handleUpdatePosition,
open: handleOpen,
close: handleClose
};
});
return {
trigger,
root,
overlay,
target,
list,
searchInput,
treeView
};
}
export default usePickerRef;