Trying to change icon for a single list item - css

I'm trying to create an expandable list component. When I click on a list item,
I want the icon to the right of it to change. Right now, when a list item is tapped,
each icon is changed. I only want icon of that particular item to change. Been
thinking about it for 20min and figured I'd reach out for help . Thanks!
import { List, ListItem } from 'react-native-elements';
export default class ExpandingList extends Component {
constructor(props){
super(props)
this.state = {
visibleItems: false
}
}
toggleMenu(){
this.setState({
visibleItems: !this.state.visibleItems
})
};
render() {
const list = "list 1, list 2, list 3";
return (
<View>
<List>
{
list.map((item, i) => (
<ListItem
onPress={ () => this.toggleMenu() }
key={i}
rightIcon={this.state.visibleItems ? iconRight : iconDown}
title={item} />
))
}
</List
</View
)
}
}

You are doing a boolean value and all list items are looking at the same value. To achieve what you want you need to pass a unique value, in this situation i am using the index but ideally you will have a unique identifier besides the index.
Below should be able to acheive what you are looking for
import { List, ListItem } from 'react-native-elements';
export default class ExpandingList extends Component {
constructor(props){
super(props)
this.state = {
visibleItems: null
}
}
toggleMenu(itemIndex){
this.setState({
visibleItems: itemIndex
})
};
render() {
const list = "list 1, list 2, list 3";
return (
<View>
<List>
{
list.map((item, i) => (
<ListItem
onPress={ () => this.toggleMenu(i) }
key={i}
rightIcon={this.state.visibleItems === i ? iconRight : iconDown}
title={item} />
))
}
</List
</View
)
}
}
Note: I am assuming you have this rendering already but for other people. The list const is a comma separated string and string do not have a map function.
Also the code above does not take into account deselecting a list item that was already selected. That can be done by checking the value in the state and if it is the same reseting the value back to null.

Here's a way to do it also:
import React from "react";
import ReactDOM from "react-dom";
class App extends React.Component {
state = {
visibleItem: 0
};
select = i => {
this.setState({
visibleItem: i
});
};
render() {
const items = [1, 2, 3, 4];
return (
<React.Fragment>
{items.map((v, i) => {
return (
<div
style={{ color: this.state.visibleItem === i ? "red" : "black" }}
onClick={() => this.select(i)}
>
{v}
</div>
);
})}
</React.Fragment>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Working example here.

Related

Change the style of the item in rectjs and remove it if I clicked another item

I wanted to change the style of an item with one click in a list and remove it if I click another item
I did like this but when I click on the second it doesn’t change to the first
const ref = useRef();
const handleClick = () => {
if (ref.current.style.backgroundColor) {
ref.current.style.backgroundColor = '';
ref.current.style.color = '';
} else {
ref.current.style.backgroundColor = 'green';
ref.current.style.color = 'white';
}
};
<Card ref={ref} elevation={6} style={{ marginBottom: "5px"}} onClick={()=>{ handleClick()}} >
<CardContent style={{ height: "10px" }}>
<Typography >
{user}
</Typography>
</CardContent>
</Card>
);
};
any help please!
I like using a package like classnames ( yarn add classnames or npm i classnames) to apply conditional styling through classes rather than having to inline the element's styling directly.
You could pass the selected attribute to your Card component using React's useState (or Redux) and then apply conditional styling to selected cards (i.e. <Card selected />).
Component.js
import { useState } from 'react';
import { useSelector } from 'react-redux';
import Card from './Card';
const Component = () => {
const [selectedId, setSelectedId] = useState(null);
const items = useSelector(state => state.items);
const handleClick = (id) => {
setSelectedId(id);
};
return items.map(({id}) =>
<Card
key={id}
onClick={() => handleClick(id)}
selected={id === selectedId}
>
...
</Card>
);
};
export default Component;
Card.js
import { classNames } from 'classnames';
const Card = ({ children, onClick, selected = false }) => (
<div
className={
classNames('Card', {
'Card--Selected': selected
})
}
onClick={onClick}
>
{ children }
</div>
);
export default Card;
Card.scss
.Card {
// Card styling...
&--Selected {
// Selected card styling...
}
}

Change style of functional component after click

The AnswerScreen component is loading a set of MyButton components. I want to change the style (basically the backgroundColor of the MyButton that was click. The code doesn't work as intended. I see that on a class component I would be able to do it with this.className but in a functional component I cannot use this (although I have the key). Any tips?
import React from 'react';
import classes from './AnswerScreen.module.css';
import MyButton from '../../../../utils/MyButton';
const AnswerScreen = (props) => {
const buttonClickHandler = (index, value) => {
if (props.myValue !== value) {
classes.AnswerScreenButton = {
fontSize: '3rem',
cursor: 'pointer',
backgroundColor: 'red'
}
}
}
return (
<div className={classes.AnswerScreen}>
{props.buttons.map((value, index) => {
return <MyButton
className={classes.AnswerScreenButton}
clickHandler={() => buttonClickHandler(index, value)}
key={index}
num = {value} />
})}
</div>
);
}
export default AnswerScreen;
I assume you want to keep track of which button was pressed in the end? I suggest you add a state variable to keep track of the selected button. You can use that state variable to set the correct classname of the button. Add the class AnswerScreenButtonSelected that contains css for a selected button. I removed your usage of index, they should be avoided when setting keys in mapped arrays in React.
import React from 'react';
import classes from './AnswerScreen.module.css';
import MyButton from '../../../../utils/MyButton';
const AnswerScreen = (props) => {
const [selectedButton, setSelectedButton] = useState(null);
return (
<div className={classes.AnswerScreen}>
{props.buttons.map( value =>
<MyButton
key={value}
className={selectedButton === value ? classes.AnswerScreenButtonSelected : classes.AnswerScreenButton}
clickHandler={() => setSelectedButton(value)}
num = {value} />
)}
</div>
);
}
export default AnswerScreen;

changing Material-ui table style in row.getRowProps()

In my react application I'm using Material-UI enhanced table which is based on react-table and I would like to change the style of their rows.
Reading from documentation (https://material-ui.com/api/table-row/) in the component TableRow should be used the prop "classes" to change the style, but in the MaterialUi code props are read this way:
<TableRow {...row.getRowProps()}>
My question is how can I use the prop classes if the TableRow props are added automatically? I thought I needed to have this:
<TableRow classes="rowStyle ">
where rowStyle is:
const styles = {
rowStyle : {
padding: 10,
border: "1px solid red"
}
};
But obviously I can't this way, how can I add "classes" to the getRowProps() and the new style in it?
I couldn't find an explanation or a good example in official documentation or stackOverflow
Many thanks for the help
EnhancedTable.js:
import React from "react";
import Checkbox from "#material-ui/core/Checkbox";
import MaUTable from "#material-ui/core/Table";
import PropTypes from "prop-types";
import TableBody from "#material-ui/core/TableBody";
import TableCell from "#material-ui/core/TableCell";
import TableContainer from "#material-ui/core/TableContainer";
import TableFooter from "#material-ui/core/TableFooter";
import TableHead from "#material-ui/core/TableHead";
import TablePagination from "#material-ui/core/TablePagination";
import TablePaginationActions from "./TablePaginationActions";
import TableRow from "#material-ui/core/TableRow";
import TableSortLabel from "#material-ui/core/TableSortLabel";
import TableToolbar from "./TableToolbar";
import {
useGlobalFilter,
usePagination,
useRowSelect,
useSortBy,
useTable,
} from "react-table";
const IndeterminateCheckbox = React.forwardRef(
({ indeterminate, ...rest }, ref) => {
const defaultRef = React.useRef();
const resolvedRef = ref || defaultRef;
React.useEffect(() => {
resolvedRef.current.indeterminate = indeterminate;
}, [resolvedRef, indeterminate]);
return (
<div>
<Checkbox ref={resolvedRef} {...rest} />
</div>
);
}
);
const inputStyle = {
padding: 0,
margin: 0,
border: 0,
background: "transparent",
};
// Create an editable cell renderer
const EditableCell = ({
value: initialValue,
row: { index },
column: { id },
updateMyData, // This is a custom function that we supplied to our table instance
}) => {
// We need to keep and update the state of the cell normally
const [value, setValue] = React.useState(initialValue);
const onChange = (e) => {
setValue(e.target.value);
};
// We'll only update the external data when the input is blurred
const onBlur = () => {
updateMyData(index, id, value);
};
// If the initialValue is changed externall, sync it up with our state
React.useEffect(() => {
setValue(initialValue);
}, [initialValue]);
return (
<input
style={inputStyle}
value={value}
onChange={onChange}
onBlur={onBlur}
/>
);
};
EditableCell.propTypes = {
cell: PropTypes.shape({
value: PropTypes.any.isRequired,
}),
row: PropTypes.shape({
index: PropTypes.number.isRequired,
}),
column: PropTypes.shape({
id: PropTypes.number.isRequired,
}),
updateMyData: PropTypes.func.isRequired,
};
// Set our editable cell renderer as the default Cell renderer
const defaultColumn = {
Cell: EditableCell,
};
const EnhancedTable = ({
columns,
data,
setData,
updateMyData,
skipPageReset,
}) => {
const {
getTableProps,
headerGroups,
prepareRow,
page,
gotoPage,
setPageSize,
preGlobalFilteredRows,
setGlobalFilter,
state: { pageIndex, pageSize, selectedRowIds, globalFilter },
} = useTable(
{
columns,
data,
defaultColumn,
autoResetPage: !skipPageReset,
// updateMyData isn't part of the API, but
// anything we put into these options will
// automatically be available on the instance.
// That way we can call this function from our
// cell renderer!
updateMyData,
},
useGlobalFilter,
useSortBy,
usePagination,
useRowSelect,
(hooks) => {
hooks.allColumns.push((columns) => [
// Let's make a column for selection
{
id: "selection",
// The header can use the table's getToggleAllRowsSelectedProps method
// to render a checkbox. Pagination is a problem since this will select all
// rows even though not all rows are on the current page. The solution should
// be server side pagination. For one, the clients should not download all
// rows in most cases. The client should only download data for the current page.
// In that case, getToggleAllRowsSelectedProps works fine.
Header: ({ getToggleAllRowsSelectedProps }) => (
<div>
<IndeterminateCheckbox {...getToggleAllRowsSelectedProps()} />
</div>
),
// The cell can use the individual row's getToggleRowSelectedProps method
// to the render a checkbox
Cell: ({ row }) => (
<div>
<IndeterminateCheckbox {...row.getToggleRowSelectedProps()} />
</div>
),
},
...columns,
]);
}
);
const handleChangePage = (event, newPage) => {
gotoPage(newPage);
};
const handleChangeRowsPerPage = (event) => {
setPageSize(Number(event.target.value));
};
const removeByIndexs = (array, indexs) =>
array.filter((_, i) => !indexs.includes(i));
const deleteUserHandler = (event) => {
const newData = removeByIndexs(
data,
Object.keys(selectedRowIds).map((x) => parseInt(x, 10))
);
setData(newData);
};
const addUserHandler = (user) => {
const newData = data.concat([user]);
setData(newData);
};
// Render the UI for your table
return (
<TableContainer>
<TableToolbar
numSelected={Object.keys(selectedRowIds).length}
deleteUserHandler={deleteUserHandler}
addUserHandler={addUserHandler}
preGlobalFilteredRows={preGlobalFilteredRows}
setGlobalFilter={setGlobalFilter}
globalFilter={globalFilter}
/>
<MaUTable {...getTableProps()}>
<TableHead>
{headerGroups.map((headerGroup) => (
<TableRow {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map((column) => (
<TableCell
{...(column.id === "selection"
? column.getHeaderProps()
: column.getHeaderProps(column.getSortByToggleProps()))}
>
{column.render("Header")}
{column.id !== "selection" ? (
<TableSortLabel
active={column.isSorted}
// react-table has a unsorted state which is not treated here
direction={column.isSortedDesc ? "desc" : "asc"}
/>
) : null}
</TableCell>
))}
</TableRow>
))}
</TableHead>
<TableBody>
{page.map((row, i) => {
prepareRow(row);
return (
<TableRow {...row.getRowProps()}>
{row.cells.map((cell) => {
return (
<TableCell {...cell.getCellProps()}>
{cell.render("Cell")}
</TableCell>
);
})}
</TableRow>
);
})}
</TableBody>
<TableFooter>
<TableRow>
<TablePagination
rowsPerPageOptions={[
5,
10,
25,
{ label: "All", value: data.length },
]}
colSpan={3}
count={data.length}
rowsPerPage={pageSize}
page={pageIndex}
SelectProps={{
inputProps: { "aria-label": "rows per page" },
native: true,
}}
onChangePage={handleChangePage}
onChangeRowsPerPage={handleChangeRowsPerPage}
ActionsComponent={TablePaginationActions}
/>
</TableRow>
</TableFooter>
</MaUTable>
</TableContainer>
);
};
EnhancedTable.propTypes = {
columns: PropTypes.array.isRequired,
data: PropTypes.array.isRequired,
updateMyData: PropTypes.func.isRequired,
setData: PropTypes.func.isRequired,
skipPageReset: PropTypes.bool.isRequired,
};
export default EnhancedTable;
Correct me if i'm wrong, but i think the first step to your solution is the correct definition of styles for the material-ui element. I can't say for certain if there is another way to generate these styles, but a simple object such as:
const inputStyle = { padding: 0, margin: 0, border: 0, background: "transparent", };
will probably not work. You probably have to use the material styles for that.
import { makeStyles } from "#material-ui/core/styles";
const useStyles = makeStyles({
root: {
border: "1px solid red",
padding: 10
},
});
and then you have to use the style hook in the component definition:
const classes = useStyles();
When overriding a material-ui definition this part is the important one to define which part is going to overriden (not allowed to include pictures yet, sorry):
Material-Ui CSS keys for Table Row
Then you can override the style <TableRow classes={{ root: classes.root }}> or in your case maybe more like <TableRow classes={{ root: classes.root }} {...row.getRowProps()}>
An additional problem you might face is that you have to override the style of the TableCells too, because the overlap the TableRow border.
I have here a code sandbox that is by no means perfect, but should help you on the right track: https://codesandbox.io/s/material-demo-535zq?file=/demo.js
import { makeStyles } from '#material-ui/core/styles';
const useStyles = makeStyles(() => ({
rowStyle : {
padding: 10,
border: "1px solid red"
}
}));
const EnhancedTable = ()=>{
const classes = useStyles();
return(
<TableRow className={classes.rowStyle}/>
)
}

React-Admin - How to create a Datagrid without a List in React-Admin

How to create a Datagrid without a List in React-Admin?
In React-admin to be able to use Datagrid to show the values you need to nest it inside a List like that:
<List {...props}>
<Datagrid>
<Textfield source"name" />
<Datagrid />
<List/>
But I need to use the dDatagrid outside of the List. The values of the Datagrid must to be shown in a screen of the type inside a Tab.
Here how I solved that problem using React-Admin 2.9
First I used The import { Query } from 'react-admin'; to fetch the data from the endpoint data/product and after that It converted the IDs to the key of it's own data.
To the commponent CustomDatagrid was passed two arrays the will be used to show the values. The first is a array of IDs and the second array is the array that contains the data and uses IDs as its key to be able to access the values.
ProductShow.js
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import {
TextField,
Query,
showNotification,
// ReferenceField,
DateField,
} from 'react-admin';
import CustomDatagrid from './CustomDatagrid';
const convertKeyToIndice = (datas) => {
const { data } = datas;
let ids = [];
const dataKey = [];
if (data) {
ids = data.map((elemento) => {
dataKey[elemento.id] = elemento;
return elemento.id;
});
return ({ ids, dataKey });
}
return ({});
};
const ProductShow = (props) => {
const { record } = props;
const { productId, dateValue } = record;
const [newPage, setNewPage] = useState(0);
const [rowsPerPage, setRowsPerPage] = useState(50);
const filter = {
pagination: { page: newPage + 1, perPage: rowsPerPage },
sort: { field: 'id', order: 'ASC' },
filter: { productId, dateValue: dateValue },
};
return (
<Query type="GET_LIST" resource="data/product" payload={filter}>
{(datas) => {
if (datas.loading && !datas.error) { return <div>Wait...</div>; }
if (datas.error) {
props.showNotification('Not Found');
return <div />;
}
const dataGridValues = {
...convertKeyToIndice(datas),
total: datas.total,
rowsPerPage,
setRowsPerPage,
newPage,
setNewPage,
};
return (
<>
<CustomDatagrid dataGridValues={dataGridValues}>
{/* <ReferenceField
sortBy="product.id"
source="productId"
reference="product"
linkType={false}
allowEmpty
{...props}
label="product"
>
<TextField source="name" />
</ReferenceField> */}
<DateField label="Date" source="valueDate" showTime />
<TextField label="Value1" source="value1" />
<TextField label="Value2" source="value2" />
<TextField label="Value3" source="value3" />
<TextField label="Value4" source="value4" />
<TextField label="Value5" source="value5" />
</CustomDatagrid>
</>
);
}}
</Query>
);
};
ProductShow.propTypes = {
productId: PropTypes.string.isRequired,
dateValue: PropTypes.string.isRequired,
showNotification: PropTypes.func.isRequired,
record: PropTypes.oneOfType([PropTypes.object]).isRequired,
};
export default connect(null, { showNotification })(ProductShow);
In the TableHead it shows the label of each column following the order that you wrap the Components in the CustomDatagrid inside the parent ProductShow component.
To Show The Components Like the List of React-Admin would show, is used the
{React.cloneElement(child,
{ record: dataKey[id] },
(child.props.children ? child.props.children : null))}
CustomDatagrid.js
import React from 'react';
import {
Table,
TableRow,
TableHead,
TableCell,
TableBody,
TablePagination,
} from '#material-ui/core';
import PropTypes from 'prop-types';
const CustomDatagrid = (propsDataGrid) => {
const { children, dataGridValues } = propsDataGrid;
const {
dataKey,
ids,
total,
rowsPerPage,
setRowsPerPage,
setNewPage,
newPage,
} = dataGridValues;
const handleChangePage = (event, page) => {
setNewPage(page);
};
const handleChangeRowsPerPage = (event) => {
const vlrDecimal = parseInt(event.target.value, 10);
setRowsPerPage(vlrDecimal);
setNewPage(0);
};
return (
<>
<Table style={{ width: '100%' }}>
<TableHead>
<TableRow>
{children && children.map(({ props }) => (
<TableCell>
{props && props.label ? props.label : props.source}
</TableCell>
))
}
</TableRow>
</TableHead>
<TableBody>
{ids && ids.map(id => (
<TableRow>
{React.Children.map(children,
child => (
<TableCell>
{React.cloneElement(child,
{ record: dataKey[id] },
(child.props.children ? child.props.children : null))}
</TableCell>
))}
</TableRow>
))
}
</TableBody>
</Table>
<TablePagination
rowsPerPageOptions={[50, 100, 250]}
component="div"
count={total}
rowsPerPage={rowsPerPage}
page={newPage}
onChangePage={handleChangePage}
onChangeRowsPerPage={handleChangeRowsPerPage}
labelRowsPerPage="Results per Page:"
/>
</>
);
};
CustomDatagrid.propTypes = {
source: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
};
export default CustomDatagrid;

React Native: Dynamically change style with AsyncStorage and States

I want to implement dark mode and black mode in my app and the way I have it is that the user toggles on dark/black mode from one class in which I want the state to be updated to all the classes, the toggle class is as followed:
AppearanceToggle Class
state = {
BlackModeValue: null,
DarkModeValue: null
};
componentDidMount = () => {
AsyncStorage.getItem('DarkModeValue').then(value => this.setState({ DarkModeValue: JSON.parse(value) }));
AsyncStorage.getItem('BlackModeValue').then(value => this.setState({ BlackModeValue: JSON.parse(value) }));
};
//AsyncStorage.setItem .........
render() {
return (
<ScrollView style={[ styles.View , this.state.DarkModeValue ? darkmode.ASView : null || this.state.BlackModeValue ? blackmode.ASView : null ]}>
<SettingsList borderColor='#c8c7cc' defaultItemSize={50}>
<SettingsList.Item
hasSwitch={true}
switchState={this.state.DarkModeValue}
switchOnValueChange={//Goes to asyncStorage.setItem method}
title='Dark Mode'
/>
<SettingsList.Item
hasSwitch={true}
switchState={this.state.BlackModeValue}
switchOnValueChange={//Goes to asyncStorage.setItem method}
title='Black Mode'
/>
</SettingsList>
</ScrollView>
);
}
}
And then in the class (which is SettingsScreen.js, this is the screen that navigates to AppearanceToggle ) that I want to .getItem and change the state is as followed:
state = {
switchValue: false,
rated: false,
DarkModeValue:null,
BlackModeValue:null,
};
componentDidMount = () => {
AsyncStorage.getItem('DarkModeValue').then(value => this.setState({ DarkModeValue: JSON.parse(value) }));
AsyncStorage.getItem('BlackModeValue').then(value => this.setState({ BlackModeValue: JSON.parse(value) }));
};
render() {
return (
<ScrollView style={[ styles.View , this.state.DarkModeValue ? styles.DMView : null || this.state.BlackModeValue ? styles.BMView : null ]}>
..........
</ScrollView>
The problem I have is that when I change the switch, it affects the AppearanceToggleScreen Class instantly but not the SettingsScreen UNLESS I refresh the app. Is there a way to do it so all of them get affected instantly?
Perhaps the best way to propagate it is to listen for the changes in your AppComponent using Context or root component. e.g.
So you would create a theme context like :
export const themes = {
blackMode: {
foreground: '#000000',
background: '#eeeeee',
},
darkMode: {
foreground: '#2f4f4ff',
background: '#222222',
},
};
export const ThemeContext = React.createContext(
themes.darkMode // default value
)
;
Your AppearanceToggle class would have something like :
import {ThemeContext} from './theme-context';
class ThemedButton extends Component {
render() {
let props = this.props;
let theme = this.context;
return (
<button
{...props}
style={{backgroundColor: theme.background}}
/>
);
}
}
ThemedButton.contextType = ThemeContext;
export default ThemedButton;
And then your AppComponent could be
import {ThemeContext, themes} from './theme-context';
import ThemedButton from './themed-button';
function Toolbar(props) {
// Render your customized toolbar here and bind the changeTheme function to it
}
class App extends Component {
constructor(props) {
super(props);
};
componentDidMount = () => {
AsyncStorage.getItem('selectedTheme').then(value => this.setState({ selectedTheme: JSON.parse(value) }));
};
this.toggleTheme = () => {
this.setState(state => ({
theme:
state.theme === themes.darkMode
? themes.blackMode
: themes.darkMode,
}));
};
}
render() {
// The ThemedButton button inside the ThemeProvider
// uses the theme from state while the one outside uses
// the default dark theme
return (
<Page>
<ThemeContext.Provider value={this.state.theme}>
<Toolbar changeTheme={this.toggleTheme} />
</ThemeContext.Provider>
<Section>
<ThemedButton />
</Section>
</Page>
);
}
}
For more read
The thing is that in the AppearanceToggleScreen you're changing the state, therefore the component is rerendered (with the new theme), but because the SettingsScreen is already in the navigation stack (because that's where you're navigating from) the componentDidMount is not executing again.
Now, maybe you want to use the Context API to access globally to the values, or do something like this.

Resources