@findify/react-components
Version:
Findify react UI components
24 lines (23 loc) • 1.07 kB
JSX
/**
* @module components/common/MapArray
*/
import { useMemo } from 'react';
import { isImmutable } from 'immutable';
/** Default key accessor, used in case no keyAccessor is provided */
const defaultKeyAccessor = (item, index) => item.hashCode ? item.hashCode() : index;
const defaultPropsMapper = () => undefined;
const Item = ({ Component, mapProps, ...rest }) => {
const extraProps = mapProps
? useMemo(() => mapProps(rest.item, rest.index), [
rest.item,
rest.index,
mapProps,
])
: undefined;
return <Component {...rest} {...extraProps}/>;
};
export default ({ array, keyAccessor = defaultKeyAccessor, mapProps = defaultPropsMapper, order, factory, limit, ...rest }) => {
const _array = limit ? array.slice(0, limit || array.length) : array;
const res = _array.map((item, index) => (<Item key={keyAccessor(item, index)} Component={factory} index={index} item={item} mapProps={mapProps} order={order && order(item, index)} {...rest}/>));
return isImmutable(res) ? res.valueSeq() : res;
};