{"size": 1197, "ext": "js", "max_stars_repo_path": "src/index.js", "max_stars_repo_name": "stepanzabelin/react-order", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/index.js", "max_issues_repo_name": "stepanzabelin/react-order", "max_issues_repo_issues_event_min_datetime": "2020-09-04T22:09:23.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-08T22:44:37.000Z", "max_forks_repo_path": "src/index.js", "max_forks_repo_name": "stepanzabelin/react-order", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 19, "max_forks_count": null, "avg_line_length": 19.0, "max_line_length": 93, "alphanum_fraction": 0.6315789474}
import React, { Component } from 'react'
import PropTypes from 'prop-types'

 
const Order = (props) => {
	const { children, list }  = props;
		
	let keysOrders = {};

	if(Array.isArray(list)) {
		for(let item in list) {
			keysOrders[list[item].toString()] = item.toString();
		}
	} else {
		for(let item in list) {
			keysOrders[item.toString()] = list[item] || 0;
		}
	}

  const newList = React.Children.toArray(children).map(function (child, i) {
		let props  = {...child.props};

		if(!props.hasOwnProperty('key')) {
			props.key = props.orderkey || Math.random().toString();
		}
 
		if(!props.hasOwnProperty('orderkey')) {
			props.orderkey = i.toString();
		}

		if(!props.hasOwnProperty('order')) {
		  props.order = keysOrders.hasOwnProperty(props.orderkey) ? keysOrders[props.orderkey] : i;
		}

		return React.cloneElement(child, props);

	}).sort((a, b) => {
    return (a.props.order - b.props.order);
  })
 
  return (
		<React.Fragment>
			{newList}
		</React.Fragment>
	)
};


Order.defaultProps = {
	children: [],
	list: {}
}

Order.propTypes = {
	children: PropTypes.array,
	list: PropTypes.oneOfType([
    PropTypes.array,
    PropTypes.object,
  ]),
};


export default Order;