react-native-ui-lib
Version:
<p align="center"> <img src="https://user-images.githubusercontent.com/1780255/105469025-56759000-5ca0-11eb-993d-3568c1fd54f4.png" height="250px" style="display:block"/> </p> <p align="center">UI Toolset & Components Library for React Native</p> <p a
58 lines (51 loc) • 1.27 kB
JavaScript
import _ from 'lodash';
import React from 'react';
import useMiddleIndex from "./helpers/useListMiddleIndex";
const usePresenter = ({
initialValue = 0,
children,
items: propItems,
itemHeight,
preferredNumVisibleRows
}) => {
const extractItemsFromChildren = () => {
const items = React.Children.map(children, child => {
const childAsType = {
value: child?.props.value,
label: child?.props.label
};
return childAsType;
});
return items || [];
};
const items = children ? extractItemsFromChildren() : propItems || [];
const middleIndex = useMiddleIndex({
itemHeight,
listSize: items.length
});
const getSelectedValueIndex = () => {
if (_.isString(initialValue) || _.isNumber(initialValue)) {
return _.findIndex(items, {
value: initialValue
});
}
return _.findIndex(items, {
value: initialValue?.value
});
};
const getRowItemAtOffset = offset => {
const index = middleIndex(offset);
const value = items[index].value;
return {
index,
value
};
};
return {
index: getSelectedValueIndex(),
items,
height: itemHeight * preferredNumVisibleRows,
getRowItemAtOffset
};
};
export default usePresenter;