Skip to content

Instantly share code, notes, and snippets.

@riyastir
Created August 22, 2021 21:55
Show Gist options
  • Save riyastir/48322cffcebad8737dbdfc2f9fa594df to your computer and use it in GitHub Desktop.
Save riyastir/48322cffcebad8737dbdfc2f9fa594df to your computer and use it in GitHub Desktop.
Working Class Component
import React, { Component } from "react";
import ReactDOM from "react-dom";
import PropTypes from "prop-types";
import ScrollMenu from "react-horizontal-scrolling-menu";
import "./styles.css";
let list = [
{ name: "item1" },
{ name: "item2" },
{ name: "item3" },
{ name: "item4" },
{ name: "item5" },
{ name: "item6" },
{ name: "item7" },
{ name: "item8" },
{ name: "item9" },
{ name: "item10" },
{ name: "item11" },
{ name: "item12" },
{ name: "item13" },
{ name: "item14" },
{ name: "item15" },
{ name: "item16" },
{ name: "item17" },
{ name: "item18" },
{ name: "item19" },
{ name: "item20" },
{ name: "item21" },
{ name: "item22" },
{ name: "item23" },
{ name: "item24" },
{ name: "item25" },
];
const MenuItem = ({ text, selected }) => {
return <div className={`menu-item ${selected ? "active" : ""}`}>{text}</div>;
};
export const Menu = (list, selected) =>
list.map((el) => {
const { name } = el;
return <MenuItem text={name} key={name} selected={selected} />;
});
const Arrow = ({ text, className }) => {
return <div className={className}>{text}</div>;
};
Arrow.propTypes = {
text: PropTypes.string,
className: PropTypes.string,
};
export const ArrowLeft = Arrow({ text: "<", className: "arrow-prev" });
export const ArrowRight = Arrow({ text: ">", className: "arrow-next" });
class App extends Component {
state = {
alignCenter: true,
clickWhenDrag: false,
dragging: true,
hideArrows: true,
hideSingleArrow: true,
itemsCount: list.length,
scrollToSelected: false,
selected: "item1",
translate: 0,
transition: 0.3,
wheel: true,
};
constructor(props) {
super(props);
this.menu = null;
this.menuItems = Menu(list.slice(0, list.length), this.state.selected);
}
onFirstItemVisible = () => {
console.log("first item is visible");
};
onLastItemVisible = () => {
console.log("last item is visible");
};
onUpdate = ({ translate }) => {
console.log(`onUpdate: translate: ${translate}`);
this.setState({ translate });
};
onSelect = (key) => {
console.log(`onSelect: ${key}`);
this.setState({ selected: key });
};
// setSelected = (ev) => {
// const { value } = ev.target;
// this.setState({ selected: String(value) });
// };
render() {
const { selected, translate } = this.state;
const menu = this.menuItems;
return (
<div className="App">
<ScrollMenu
alignCenter={true}
arrowLeft={ArrowLeft}
arrowRight={ArrowRight}
clickWhenDrag={false}
data={menu}
dragging={true}
hideArrows={true}
hideSingleArrow={true}
onFirstItemVisible={this.onFirstItemVisible}
onLastItemVisible={this.onLastItemVisible}
onSelect={this.onSelect}
onUpdate={this.onUpdate}
scrollToSelected={false}
selected={selected}
translate={translate}
wheel={true}
/>
</div>
);
}
}
export default App;
ReactDOM.render(<App />, document.getElementById("root"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment