@deepdub/react-arborist
Version:
26 lines (25 loc) • 1.06 kB
JavaScript
import { useSimpleTree } from "./use-simple-tree";
export function useValidatedProps(props) {
if (props.initialData && props.data) {
throw new Error(`React Arborist Tree => Provide either a data or initialData prop, but not both.`);
}
if (props.initialData &&
(props.onCreate || props.onDelete || props.onMove || props.onRename)) {
throw new Error(`React Arborist Tree => You passed the initialData prop along with a data handler.
Use the data prop if you want to provide your own handlers.`);
}
if (props.initialData) {
/**
* Let's break the rules of hooks here. If the initialData prop
* is provided, we will assume it will not change for the life of
* the component.
*
* We will provide the real data and the handlers to update it.
* */
const [data, controller] = useSimpleTree(props.initialData);
return Object.assign(Object.assign(Object.assign({}, props), controller), { data });
}
else {
return props;
}
}