react-native-xenon
Version:
A powerful in-app debugging tool for React Native.
41 lines (40 loc) • 1.07 kB
JavaScript
;
import { Children, createElement, useImperativeHandle, useRef } from 'react';
export default function IndexedStack({
children,
defaultIndex,
id,
ref
}) {
const currentIndex = useRef(defaultIndex);
const childrenRefs = useRef([]);
useImperativeHandle(ref, () => ({
getCurrentIndex: () => currentIndex.current,
setCurrentIndex: index => {
currentIndex.current = index;
childrenRefs.current.forEach((childRef, i) => {
if (childRef) {
childRef.setNativeProps({
style: {
display: i === index ? 'flex' : 'none'
}
});
}
});
}
}));
return Children.map(children, (child, i) => {
return /*#__PURE__*/createElement(child.type, {
...child.props,
key: `${id}-${i}`,
style: [child.props.style, {
display: i === defaultIndex ? 'flex' : 'none'
}],
ref: childRef => {
if (!childRef) return;
childrenRefs.current[i] = childRef;
}
});
});
}
//# sourceMappingURL=IndexedStack.js.map