How I can get clickable custom dots of this carousel.
I cannot bind a click event in the list item to move the carousel. I need a proper implementation between onClick and the li item to click on prev and next items in carousel
Here is the full code in the link codebox
const CustomDot = ({onMove,index,onClick,active}) => {
return (
<ol class="carousel-indicators">
<li data-target="#main-slide" data-slide-to="0" className={active ? "active" : "inactive"}
>How t bind the click event list item
onClick={() => onClick()}>{React.Children.slide1}</li>
<li data-target="#main-slide" data-slide-to="1" className={active ? "active" : "inactive"}
onClick={() => onClick()}>{React.Children.slide2} </li>
<li data-target="#main-slide" data-slide-to="2" className={active ? "active" : "inactive"}
onClick={() => onClick()}>{React.Children.slide3} </li>
</ol>
);
};
The problem is that the plugin expects to receive a single element (li for example) as CusomtDot but you pass a list (ol with some children).
The solution, pass a single element, like this:
const CustomDot = ({ onMove, index, onClick, active }) => {
// onMove means if dragging or swiping in progress.
// active is provided by this lib for checking if the item is active or not.
return (
<li
className={active ? "active" : "inactive"}
onClick={() => onClick()}
>
{index + 1}
</li>
);
};
Working demo: https://codesandbox.io/s/react-multi-carousel-customdot-jwkfo
const CustomDot = ({ onMove, index, onClick, active }) => {
return (
<li
className={active ? "active" : "inactive"}
onClick={() => onClick()}
>
<MaximizeIcon />
</li>
);
};
const arrowStyle = {
background: "transparent",
border: 0,
color: "#fff",
fontSize: "80px"
};
const CustomRight = ({ onClick }) => (
<button className="arrow right" onClick={onClick} style={arrowStyle}>
<ArrowForwardIcon style={{ fontSize: "50px" }} />
</button>
);
const CustomLeft = ({ onClick }) => (
<button className="arrow left" onClick={onClick} style={arrowStyle}>
<ArrowBackIcon style={{ fontSize: "50px" }} />
</button>
);
Working demo : https://codesandbox.io/s/react-multi-carousel-customdot-3q0f4?file=/src/App.js:683-1052
Related
I have MUI radio buttons which I use to get filter criteria and I filter an object. Then i render the filtered object in an owl carousal to show. But when I click on a radio button it always takes the previously clicked value of the radio button. I have used ternary conditions to check but it keeps happening I couldb't figure out following is my code.
<FormControl>
<RadioGroup
row
aria-labelledby="demo-row-radio-buttons-group-label"
name="row-radio-buttons-group"
value={movieType}
onChange={movieTypeChange}
>
<FormControlLabel
value="1"
control={<Radio color="error" />}
label="All"
/>
<FormControlLabel
value="2"
control={<Radio color="error" />}
label="Now Showing"
/>
<FormControlLabel
value="3"
control={<Radio color="error" />}
label="Upcoming"
/>
</RadioGroup>
</FormControl>
{filterdMovie.length > 0 ? (
<OwlCarousel
className="owl-theme"
loop={movieCount}
center={movieCount}
margin={1}
autoplay={true}
dots={false}
items={3}
touchDrag={true}
lazyLoad={true}
responsive={state.responsive}
// responsive={"0:{items:1,},600:{items:3,},1000:{items:5,}"}
animateOut={"fadeOut"}
>
{filterdMovie.map((movieBannertop, idx) => {
return (
<div key={idx}>
<div className="item">
<div className="prs_upcom_movie_box_wrapper">
<div className="prs_upcom_movie_img_box">
<img
src={
movieBannertop.thumbnail
// HTMLImageElement.complete
// ?
// `${process.env.REACT_APP_DEV_BASE_URL_IMAGE}/movie/${movieBannertop.id}/thumbnail.jpg`
// : placeholder
}
alt="movie_img"
/>
<div className="prs_upcom_movie_img_overlay"></div>
<div className="prs_upcom_movie_img_btn_wrapper">
<ul>
<li>
{movieBannertop.bookingOpen ? (
<a
href
onClick={() => viewMovie(movieBannertop)}
style={{ textDecoration: "none" }}
>
More Info
</a>
) : null}
</li>
<li>
{!movieBannertop.upcoming ? (
<a
href
onClick={() => viewMovie(movieBannertop)}
style={{ textDecoration: "none" }}
>
Now Showing
</a>
) : (
<a
href
style={{
textDecoration: "none",
backgroundColor: "#2488ed",
}}
onClick={() => viewMovie(movieBannertop)}
>
Coming Soon
</a>
)}
</li>
</ul>
</div>
</div>
<div className="prs_upcom_movie_content_box">
<div className="prs_upcom_movie_content_box_inner">
<Tooltip title={movieBannertop.name}>
<h2>
<a
href
onClick={() => viewMovie(movieBannertop)}
style={{ textDecoration: "none" }}
>
{" "}
{movieBannertop.name.length > 10
? `${movieBannertop.name.substring(0, 10)}...`
: movieBannertop.name}
</a>
</h2>
</Tooltip>
<p>{movieBannertop.genre}</p>
</div>
<div className="prs_upcom_movie_content_box_inner_icon">
{/* <ul>
<li>
<a href="movie_booking.html">
<i className="flaticon-cart-of-ecommerce"></i>
</a>
</li>
</ul> */}
</div>
</div>
</div>
</div>
</div>
);
})}
</OwlCarousel>
) : (
<>
<Stack
direction="column"
justifyContent="center"
alignItems="center"
>
<img src={notFound} alt="" width="50%" />
<h4>No Movies Found</h4>
</Stack>
</>
)}
const movieTypeChange = (e) => {
e.preventDefault();
setMovieType(e.target.value);
const value = e.target.value;
if (value === "1") {
setFileredMovies(onlineMovies);
}
if (value === "2") {
let shows = [];
onlineMovies.forEach((element) => {
if (!element.upcoming) {
shows.push(element);
}
});
setFileredMovies(shows);
console.log(shows);
}
if (value === "3") {
console.log("3");
let shows = [];
onlineMovies.forEach((element) => {
if (element.upcoming) {
shows.push(element);
}
});
setFileredMovies(shows);
}
};
const movieTypeChange = (e) => {
var shows = [];
const value = e.target.value;
if (value === "1") {
setFileredMovies(onlineMovies);
}
if (value === "2") {
onlineMovies.forEach((element) => {
if (!element.upcoming) {
shows.push(element);
}
});
setFileredMovies(shows);
}
if (value === "3") {
onlineMovies.forEach((element) => {
if (element.upcoming) {
shows.push(element);
}
});
setFileredMovies(shows);
}
setMovieType(value);
};
I have this section of code I am trying to render. At different viewports, the button styles change from underline to background color using a themeColor I pull from a JSON file with acting as my database in a sense. How do you write an 'else if' inline in react so when my state is "active" and the media query matches, the styles update accordingly?
const btnMobileStyle = useMediaQuery('(min-width: 375px)')
const btnTabletStyle = useMediaQuery('(min-width: 768px)')
const styles = {
buttonMobile: (btnMobileStyle) => ({
borderBottom: `2px solid ${themeColor}`,
}),
buttonTablet: (btnTabletStyle) => ({
borderBottom: 'none',
backgroundColor: `${themeColor}`,
}),
}
return (
<main className="main">
<div className="main__btn-container">
<button
style={overviewActive ? styles.buttonMobile(btnMobileStyle) : {}}
onClick={updateOverview}
className="main__btn"
>
Overview
</button>
<button
style={structureActive ? styles.buttonMobile(btnMobileStyle) : {}}
onClick={updateStructure}
className="main__btn"
>
Structure
</button>
<button
style={surfaceActive ? styles.buttonMobile(btnMobileStyle) : {}}
onClick={updateSurface}
className="main__btn"
>
Surface
</button>
</div>
I was able to solve the issue using this code:
const btnMobileStyle = useMediaQuery('(max-width: 768px)')
const styles = {
btnMobile: (btnMobileStyle) => ({
borderBottom: btnMobileStyle ? `2px solid ${themeColor}` : {},
backgroundColor: btnMobileStyle ? 'transparent' : themeColor,
}),
}
return (
<main className="main">
<div className="main__btn-container">
<button
style={overviewActive ? styles.btnMobile(btnMobileStyle) : {}}
onClick={updateOverview}
className="main__btn"
>
Overview
</button>
<button
style={structureActive ? styles.btnMobile(btnMobileStyle) : {}}
onClick={updateStructure}
className="main__btn"
>
Structure
</button>
<button
style={surfaceActive ? styles.btnMobile(btnMobileStyle) : {}}
onClick={updateSurface}
className="main__btn"
>
Surface
</button>
</div>
<div className="main__img-container">
<img
className="main__planet-img"
src={state.planetImg}
alt={state.planetName}
/>
<img
className={
surfaceActive
? 'main__planet-overlay main__planet-overlay--active'
: 'main__planet-overlay'
}
src={state.planetImgOverlay}
alt=""
/>
</div>
I want to toggle active classname to particular month clicked and january should be bydefault active and these months are mapping from array
<div className="first6-months"> {Data[5].firstsixmonths.map((item, key) => ( <p className={item === "January" ? "jan" : "feb"} // onClick={(event) => handleClick(event, key)} > {item} </p> ))} </div>
If you want to set an "active" class to the selected item that how I would do it:
const [months, setMonths] = useState('January')
const handleChange = (event) => {
setMonths(event.target.name)
}
return (
<div className="first6-months">
{Data[5].firstsixmonths.map((item, key) => (
<p className={item === month ? "active" : ""}
onClick={handleChange}
name={item}>
{item}
</p>
))}
</div>
)
When looping output, always put a key prop on your element
{Data[5].firstsixmonths.map((item, key) => ( <p key={item} onClick={() => handleClick(key)} > {item} </p> ))}
Use some bit of state to track which month is active
const [activeItem, setActiveItem] = useState(0);
const handleClick = (key) = setActiveItem(key);
Then set your className based upon active
import classnames from `classnames`;
//...
<p className={classnames({
active: item === Data[5].firstsixmonths[activeItem]
})} onClick={() => handleClick(key)}>{item}</p>
This is the basics of what you need, but you'll probably need to play with it a little.
I have defined the Key in my code key={item.id} when mapping through reposit list and I am trying to use it as a prop in my here {openModal && <Modal repo={reposit} my_key={key} setOpenModal={setOpenModal}/> } but it doesn't seems to work. Someone can explain to me how can I access to the key on my Modal by using it as a prop ?
Here is the part of the code :
<List.Item>
<List.Content>
{reposit.map((item) => (
<li key={item.id} onClick={() => {setOpenModal(true)}}>
<i className="folder icon" /> {item.name}
</li>
))}
{openModal && <Modal repo={reposit} my_key={key} setOpenModal={setOpenModal}/> }
</List.Content>
</List.Item>
Thank you for your help.
You have to use state to keep track the key for the clicked list item like below:
import React, { useState } from "react";
const App = () => {
const [myKey, setMyKey] = useState();
return (
<List.Item>
<List.Content>
{reposit.map((item) => (
<li
key={item.id}
onClick={() => {
setMyKey(item.id);
setOpenModal(true);
}}
>
<i className="folder icon" /> {item.name}
</li>
))}
{openModal && (
<Modal repo={reposit} myKey={myKey} setOpenModal={setOpenModal} />
)}
</List.Content>
</List.Item>
);
};
The li key attribute is only defined within the li element, it is not accessible outside of it. Your modal is not a child of the li element so it can't be accessed in the way you are trying. You have to pass item.id to the modal through the li onClick function. One way would be to create state to keep track of what key should be passed to the modal, and change it within the onclick function.
const [modalKey, setModalKey] = useState()
const openModal = (key) => {
setModalKey(key)
setOpenModal(true)
}
...
<List.Item>
<List.Content>
{reposit.map((item) => (
<li key={item.id} onClick={() => openModal(item.id)}>
<i className="folder icon" /> {item.name}
</li>
))}
{openModal && <Modal repo={reposit} my_key={modalKey} setOpenModal={setOpenModal}/> }
</List.Content>
</List.Item>
In my application i use ant design. I want to add the delete row functionality in my table, but a came across some problems with the implementation. I want to add delete functionality functionality, but i can't do this because i can't render 2 render in
{
title: "operation",
dataIndex: "operation",
render: (text, record) => { //first render
const editable = isEditing(record);
return editable ? (
<span>
<a
href="javascript:;"
onClick={() => save(record.key)}
style={{ marginRight: 8 }}
>
Save
</a>
<Popconfirm
title="Sure to cancel?"
onConfirm={() => cancel(record.key)}
>
<a>Cancel</a>
</Popconfirm>
</span>
) : (
<a disabled={editingKey !== ""} onClick={() => edit(record)}>
Edit
</a>
),
}
render: (text, record) => //second render
data.length >= 1 ? (
<Popconfirm
title="Sure to delete?"
onConfirm={() => handleDelete(record.key)}
>
<a>Delete</a>
</Popconfirm>
) : null
},
and i get Duplicate declaration. How to add delete functionality in my app and which is the problem?
It seems that there is an error in setting data, in handleDeleteMethod
Change your handleDeleteMethod as follows
const handleDelete = key => {
setData(data.filter(item => item.key !== key));
};
When you are using hooks, you need to pass the resultant new state that need to be set as a parameter, not the object like we use in setState method
Please feel free to ask doubts if any
EDIT :
For edit to work change the render as follows
render: (text, record) => {
const editable = isEditing(record);
return editable ? (
<span>
<a
href="javascript:;"
onClick={() => save(record.key)}
style={{ marginRight: 8 }}
>
Save
</a>
<Popconfirm
title="Sure to cancel?"
onConfirm={() => cancel(record.key)}
>
<a>Cancel</a>
</Popconfirm>
</span>
) : (
<>
<a disabled={editingKey !== ""} onClick={() => edit(record)}>
Edit
</a>
<Popconfirm
title="Sure to delete?"
onConfirm={() => handleDelete(record.key)}
>
<a>Delete</a>
</Popconfirm>
</>
);
}