score:1
Here is your edited CopePen and here is the main code that I modified :
TodoList.js :
import { makeStyles, List } from "@material-ui/core";
import React from "react";
import { useSelector } from "react-redux";
import TodoItem from "./TodoItem";
const useStyles = makeStyles({
rootDiv: {
display: "flex",
flexDirection: "column",
justifyContent: "center",
marginLeft: "10px",
marginRight: "10px"
},
p: {
fontSize: "30px",
fontweight: "bolder"
}
});
export default function TodoList() {
const classes = useStyles();
const todos = useSelector((state) => state.TodoReducer);
const filter = useSelector((state) => state.viewFilterReducer);
const getVisibleTodos = (todos, filter) => {
switch (filter) {
case "SHOW_ALL":
return todos;
case "SHOW_ACTIVE":
return todos.filter((todo) => !todo.done);
case "SHOW_COMPLETED":
return todos.filter((todo) => todo.done);
default:
return todos;
}
};
const visibleTodos = getVisibleTodos(todos, filter);
return (
<div className={classes.rootDiv}>
{visibleTodos?.length === 0 ? (
<p className={classes.p}>No Todo Item Here</p>
) : (
<List>
{/*
I moved the list here, because you need to create only one list
and loop over you items
*/}
{visibleTodos.map((todo, i) => (
<TodoItem key={i} todo={todo} />
))}
</List>
)}
</div>
);
}
TodoItem.js :
import React, { useState } from "react";
import { useDispatch } from "react-redux";
import { todoDeleted, todoUpdated, todoCompleted } from "../Slices/TodoSlice";
import {
Button,
Checkbox,
List,
ListItem,
ListItemSecondaryAction,
ListItemText,
ListItemIcon,
makeStyles
} from "@material-ui/core";
/*
Since we fixed the issue about displaying multiple <List>,
you only need to style the items.
You can now properly define the height of your items, and set whatever you want as style,
the default flex layout is kept
*/
const useStyles = makeStyles({
item: {
height: "35px",
alignItems: "center",
backgroundColor: "#fce4ec",
border: "1px solid #aaaaaa",
borderRadius: "20px",
margin: "5px 5px"
}
});
export default function TodoItem({ todo, i }) {
const classes = useStyles();
const dispatch = useDispatch();
const [edit, setEdit] = useState(false);
const [text, setText] = useState(todo.text);
const handleUpdate = () => {
dispatch(
todoUpdated({
id: todo.id,
text
})
);
if (edit) {
setText(todo.text);
}
setEdit(!edit);
};
return (
<ListItem className={classes.item}>
<Checkbox
value={todo.id}
onChange={(e) => dispatch(todoCompleted(+e.target.value))}
defaultChecked={todo.done}
/>
<ListItemText>
{todo.done ? (
<p>
<del>{todo.text}</del>
</p>
) : (
<h4>
{edit ? (
<input
type="text"
placeholder={text}
value={text}
onChange={(e) => setText(e.target.value)}
/>
) : (
todo.text
)}
</h4>
)}
</ListItemText>
<ListItemSecondaryAction>
<button
variant="contained"
color="primary"
edge="end"
onClick={() => handleUpdate()}
disabled={todo.done}
>
{edit ? "Update" : "Edit"}
</button>
<button
variant="contained"
color="primary"
onClick={() => dispatch(todoDeleted(todo.id))}
>
Delete
</button>
</ListItemSecondaryAction>
</ListItem>
);
}
I mainly changed the following things :
- Moved the
<List>
component in the parent. You were creating a list for each item, which were creating multiple CSS problems. Pay attention to what your component is supposed to display ! - Removed the CSS
listRoot
and thediv
node as well. It's not needed anymore - Moved the checkbox as the first child of the listItem so it respect the order you asked for
The layout has its own padding value. I can only HIGHLY advice you to keep it as it is since it's clean, but if you really want to center your items'content, you can still add paddings to you list items and second element, something like :
const useStyles = makeStyles({
item: {
// ...
},
itemSpacing: { padding: 0 25% }
});
And then:
<ListItem className={[classes.item, classes.itemSpacing]}>
and :
<ListItemSecondaryAction className={classes.itemSpacing}>
But be really carefull, it will break if your item text is too long, 1) because you set a hard coded height to the item, and 2) because ListItemSecondaryAction
is in position: absolute
score:0
It might help if you'll add the css file - therefore we can check the current status.
But anyway, have a look at CSS grid layout. This could be the right thing for you.
With CSS grid layout your able to assign an grid position to an element for e.g. checkbox 1.
Then you can select with display: grid
that id 1 should be displayed first and so on.
Source: stackoverflow.com
Related Query
- How to get buttons to the end of material ui list and checkbox to the start of the list?
- Reactjs How to get the value of Start date and End date?
- Get table pagination buttons and rows per page to the left on Material UI
- How do I access the index of a selected item in list using React and Material UI?
- How to loop through an array of strings backwards x amount of times and start over when the end is reached?
- How to get start and end date value in React date range picker?
- How to get the text next to my checkbox inline, and next to checkbox?
- How to get the index of the selected item from a dropdown in ReactJs and material UI?
- How can I get selected data list and structure the data list
- How can I get the checkbox and label to line up in a form with React?
- How to get the simplest Material UI Select to work with React and Typescript?
- How to change the checkbox selection and get the updated id's of checkbox items in form submit in React Component?
- How can I add a checkbox to each element in the list and to be able to control it through state in my react app?
- how to get the values of a checked checkbox and the output it on a input field
- How do I get the name and id of a dropdown list and append to formdata in React?
- How to get start and end timestamps of every month in selected year
- React Date Picker - How to handle if user only selects the start date in range and does not selects the end date?
- How to place text at the start and another at end in the same alert tage in react bootstrap
- how to calculate the different between start date and end date with out holidays ? JavaScript
- How to ensure Buttons from material ui all have the same length and height
- How to validate End date which is less than the Start date in react and add error message in banner if the end date is less than start date in react
- How to get the label and values from multiple TextArea material ui with same onchange function for all?
- how can i get the more than one data row value based on selected checkbox and store in useState (react-table-boostrap2)
- How can I animate a react.js component onclick and detect the end of the animation
- In Firebase when using push() How do I get the unique ID and store in my database
- How to hide the OK and Cancel buttons of antd Modal?
- <Fade> in material-ui just disables the visibility of the component . How to get the fade effect and actually hide the component ?
- react - how do I get the size of a component child elements and reposition them
- How do I use React and forms to get an array of checked checkbox values?
- How to get the marquee effect with list view in react native?
More Query from same tag
- Jest testing hook state update with setTimeout
- React app freezing due to useEffect hook in 16.9 while working fine in 16.8
- How to set an array to empty array when using prevState in Reactjs
- dispatch() from inside child route causes infinite loop
- While creating a react app I'm getting following error or errors
- PapaParse gets data in console.log but don't know how to make a new array that outputs on main screen on a table
- apply css to Js strings-Js/CSS
- Browser rendering an empty Page
- Is there any way to export multiple slices from a single slice file? in redux-toolkit
- What is causing Error: Too many re-renders in this code?
- how to get info about page loading from server
- ReactJS: Where does "props" come from?
- How to pass item's data to another component, edit data and save data in React.js?
- ReactJS: using spread element to update state
- How to control the open state of two modals in sequence
- Can't display data from my state/props in react-redux
- Using buttons within mapped details throws error "Nothing was returned from render"
- How do we call recursive function on react js in class based components
- fetching random json getting first undefined then result
- How to I update state when props are set using mapStateToProps?
- How to generate svg client-side with d3 without attaching it to the DOM (using with React.js)
- How to get a data from an object inside of a function (JS)
- How to display keystroke on page using react
- React: Export function to use in another component
- Cannot render child components with React Router (Nested Routes)
- How can I give an empty material UI text field a value of null, when submitting the form, instead of an empty string?
- Unhandled Rejection (TypeError): Cannot read property of undefined
- How to get label text instead of the value of the antd slider
- change the value of useState with setInterval
- Visual Studio Code extensions stopped working suddenly