Add a functionality in ant table - reactjs

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>
</>
);
}

Related

toggle classname according to click

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.

How to use the key (defined from lists items key) as prop on my Modal

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>

No-unused-expressions issue React

I am making an eCommerce website with React. There is a ProductList.js which is listing the code of Product.js. I am using the context API which is created from context.js.
The code of context.js is-
state = {
products: [],
detailProduct,
};
getItem = (id) => {
let product = this.state.products.find((item) => item.id === id);
return product;
};
handleDetail = (id) => {
let product = this.getItem(id);
this.setState(() => {
return { detailProduct: product };
});
};
addToCart = (id) => {
console.log(`Hello from Add to Cart. Id is: ${id}`);
};
render() {
return (
<ProductContext.Provider
value={{
...this.state,
handleDetail: this.handleDetail,
addToCart: this.addToCart
}}
>
{this.props.children}
</ProductContext.Provider>
);
}
This is not the full code for sure. If you need full code, I can give that too.
Code of Product.js is-
<ProductConsumer>
{(value) => {
<div className="p-5 img-container" onClick={() => value.handleDetail(id)}>
<Link to="/details">
<img src={img} className="card-img-top" alt="Product Image" />
</Link>
<button className={inCart ? "in-cart cart-btn" : "cart-btn"} disabled={inCart ? true : false} onClick={() => value.addToCart(id)}>
{inCart ? (
<p className="text-capitalize mb-0" disabled>
Already in cart
</p>
) : (
<i className="fas fa-cart-plus"></i>
)}
</button>
</div>
}}
</ProductConsumer>
The error I am getting--
./src/components/Product.jsx
Line 13:15: Expected an assignment or function call and instead saw an expression no-unused-expressions
Search for the keywords to learn more about each error.
Please help me in fixing this issue...
You are using curly braces { in the consumer block. Change these to parentheses like so:
<ProductConsumer>
{(value) => (
<div className="p-5 img-container" onClick={() => value.handleDetail(id)}>
<Link to="/details">
<img src={img} className="card-img-top" alt="Product Image" />
</Link>
<button className={inCart ? "in-cart cart-btn" : "cart-btn"} disabled={inCart ? true : false} onClick={() => value.addToCart(id)}>
{inCart ? (
<p className="text-capitalize mb-0" disabled>
Already in cart
</p>
) : (
<i className="fas fa-cart-plus"></i>
)}
</button>
</div>
)}
</ProductConsumer>
Read this answer for an explanation: https://stackoverflow.com/a/35440330/1790728

Using react-multi-carousel but unable to use custom dots

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

Delete the user from the List : Ant Design

Can somebody help me to delete data of user list
Code is available below https://codesandbox.io/s/r4try
Please tell me how to delete particular user from the user list. please use delete button on user
here is the solution
Codesandbox Demo
https://codesandbox.io/s/control-between-forms-ant-design-demo-99sus?file=/index.js
<Form.Item
label="User List"
shouldUpdate={(prevValues, curValues) => prevValues.users !== curValues.users}
>
{({ getFieldValue, setFieldsValue }) => {
const users = getFieldValue('users') || [];
return users.length ? (
<ul>
{users.map((user, index) => (
<li key={index} className="user">
<Avatar icon={<UserOutlined />} />
{user.name} - {user.age}
<CloseOutlined onClick={() =>{
const updatedUsers = delete users[index];
setFieldsValue({users: updatedUsers})
}} style={{ paddingLeft: 15 }}/>
</li>
))}
</ul>
) : (
<Typography.Text className="ant-form-text" type="secondary">
( <SmileOutlined /> No user yet. )
</Typography.Text>
);
}}
</Form.Item>

Resources