Skip to content

Instantly share code, notes, and snippets.

@ldco2016
Created July 15, 2019 02:14
Show Gist options
  • Save ldco2016/1b49e4fd00b597684d095c47d672ae83 to your computer and use it in GitHub Desktop.
Save ldco2016/1b49e4fd00b597684d095c47d672ae83 to your computer and use it in GitHub Desktop.
Switch component
import PropTypes from "prop-types";
import React from "react";
import Switch from "../ThemedSwitch";
import {
genericObjectSort,
SortProperty,
SORT_DIRECTION
} from "../../../../common/utils/object-sort";
import styles from "./SwitchList.css";
const SwitchList = ({
color,
disabled,
sortKey,
sortDirection = SORT_DIRECTION.ASC,
items,
data,
children,
onChange
}) => {
if (items.length === 0) {
return React.Children.only(children);
}
console.log(this.props);
let sortProp = null;
let sortedItems = items;
if (sortKey) {
sortProp = new SortProperty(sortKey, sortDirection === SORT_DIRECTION.ASC);
sortedItems = items.sort(genericObjectSort(sortProp));
}
return (
<div>
{sortedItems.map(item => {
return (
<Switch
checked={data[item.key]}
label={item.description}
onChange={onChange(item.key)}
key={item.key}
color={color}
disabled={
typeof disabled === "function" ? disabled(item) : disabled
}
className={styles.customSwitch}
/>
);
})}
</div>
);
};
SwitchList.propTypes = {
sortKey: PropTypes.string,
sortDirection: PropTypes.number,
items: PropTypes.arrayOf(PropTypes.object),
data: PropTypes.object,
children: PropTypes.node,
color: PropTypes.string,
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
onChange: PropTypes.func
};
export default SwitchList;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment