so i have this simple divs of names:
i just want to press on one of them and get a background color of green and when pressing on another one the first one will be canceled so just one will be colored at a time. what i simply need is inline style or i don't know i'm stuck.
first.js:
import React from 'react';
function SidebarComponents({name,title,selected,onSelect}) {
const style={
cursor: "pointer"
};
const classes = {
selected: {
backgroundColor: '#00ff00'
}
}
return (
<div
name={name}
title = {title}
style={style}
>
{name}
</div>
)
}
export default SidebarComponents;
second.js:
import React, { useEffect, useState } from "react";
import SidebarComponents from "../SidebarComponents/SidebarComponents";
import 'bootstrap/dist/css/bootstrap.min.css';
import '../Sidebar1/Sidebar.css';
function Sidebar({ onChange }) {
const [selectedComponent, setSelectedComponent] = useState({
componentsName: [
{ name: "John Smith", title: "John Smith" },
{ name: "Male, 26 years old", title: "Gender and age" },
{ name: "john", title: "Alerts" },
{ name: "claude", title: "Recent" },
{ name: "edward", title: "Blood pressure" },
{ name: "mira", title: "Body weight" },
{ name: "alex", title: "Glucose" },
{ name: "zac", title: "SPO2" }
]
});
return (
<div>
{selectedComponent.componentsName.map(component => {
return (
<div className="row align-items-start sidebar-components">
<div className="col">
<SidebarComponents
name={component.name}
title={component.title}
/>
</div>
</div>
);
})}
</div>
);
}
export default Sidebar;
on Sidebar:
const [selectedName, setSelectedName] = useState(null);
//...
<SidebarComponents
name={component.name}
title={component.title}
selected={component.name === selectedName}
onSelect={setSelectedName}
/>
on SidebarComponents:
const selectedClassName = selected ? 'selected' : '';
//...
<div
name={name}
title={title}
style={style}
className={`sidebar ${selectedClassName}`} //you must add sidebar and selected classes to your styles
onClick={() => onSelect(name)}
>
{name}
</div>
Add key attribute to div, inside the map.
Handel onClick event, to store the selected element index/value in your state.
Apply style using conditional rendering of className.
second.js
<div>
{selectedComponent.componentsName.map((component, index) => {
return (
<div key={index} onClick={() => handelOnClick(index)} className="row align-items-start sidebar-components">
<div className="col">
<SidebarComponents
name={component.name}
title={component.title}
className={selectedIndex === index ? 'highlight' : ''}
/>
</div>
</div>
);
})}
</div>
As you are rendering text in first.js no need to use div wrapper, use 'p', 'span' tag
In second.js instead of iterating entire div block, use ul li
Related
i have a react.js component in which i am displaying states and under i am displaying districts. To display this list it's working fine. The problem i want when i press any sates only that particular state sublist should display not all states sublist.
import React,{useState} from "react"
Const [open,,setOpen]=useState(false);
//Wrapper component
</div>
{states.map((city, index) => {
return <StateList state={state} key={index} />;
})}
</div>
//state component
<div onClick={()=>setOpen(!open)}>
<span >{state.name}</span>
<svg
viewBox="0 0 24 24"
className={`
${open ? "rotate-180" : ""}
`}
>
</svg>
</h2>
{open && <AreaList area={city} />}
</div>
//district component
const AreaList = ({ state }) => {
return state.districts.map((district) => (
<li>
<span className="ml-2 text-outer-space">
{district.name}
</span>
</li>
));
};
Here is working solution (without styles):
Codesandbox
import { useState } from "react";
const data = [
{
name: "Fujairah",
districts: [
{ name: "Al Buthna" },
{ name: "Al Bedia" },
{ name: "Town Center" },
{ name: "Wadi Al Sedr" }
]
},
{
name: "Abu Dhabi",
districts: [{ name: "Al Aman" }, { name: "Al Bahya" }]
}
];
const App = () => {
return (
<div>
{data.map((city) => {
return <City key={city.name} city={city} />;
})}
</div>
);
};
const City = ({ city }) => {
const [open, setOpen] = useState(false);
return (
<div onClick={() => setOpen(!open)}>
<h2>
<span>{city.name}</span>
</h2>
{open && <DistrictList city={city} />}
</div>
);
};
const DistrictList = ({ city }) => {
return city.districts.map((district) => (
<li key={district.name}>
<span>{district.name}</span>
</li>
));
};
export default App;
I'm learning React, JS and I'm trying to use the .map method to display some data in an array. For each element in the array I create a button to show/hide a description with css and I'm looking for a way to only show the description of one element instead of all descriptions. It is probably unclear but here is the code :
import "./styles.css";
import React, { useState } from "react";
export default function App() {
const [showDescription, setshowDescription] = useState(false);
const [anArray] = useState([
{ title: "First Div", description: "First description"},
{ title: "Antoher Div", description: "Another description"}
]);
return (
<div className="App">
{anArray.map((val, index) => {
return (
<div className="div" key={index}>
<div className="title">{val.title}</div>
<button className="btn" onClick={() => setshowDescription(!showDescription)}>
Show description
</button>
<div id={showDescription ? "display" : "hidden"}>
<div className="description">{val.description}</div>
</div>
</div>
);
})}
</div>
);
}
Do you have any idea please ?
Issue
You've a single boolean showDescription state that is toggling every description.
Solution
Store an index of the description you want to toggle. Use the index to match the currently mapped element. Conditionally render the description
export default function App() {
const [showId, setShowId] = useState(null);
// curried function to toggle to new index or back to null to hide
const toggleDescription = id => () => setShowId(showId => showId === id ? null : id);
const [anArray] = useState([
{ title: "First Div", description: "First description"},
{ title: "Antoher Div", description: "Another description"}
]);
return (
<div className="App">
{anArray.map((val, index) => {
return (
<div className="div" key={index}>
<div className="title">{val.title}</div>
<button className="btn" onClick={toggleDescription(index)}> // <-- pass index
Show description
</button>
{showId === index && ( // <-- check for index match
<div>
<div className="description">{val.description}</div>
</div>
)}
</div>
);
})}
</div>
);
}
You can use index to show/hide your div. The issue is you are using only one Boolean value to handle it.
export default function App() {
const [showDescriptionIndex, setshowDescriptionIndex] = useState(-1);
const [anArray] = useState([
{ title: "First Div", description: "First description"},
{ title: "Antoher Div", description: "Another description"}
]);
return (
<div className="App">
{anArray.map((val, index) => {
return (
<div className="div" key={index}>
<div className="title">{val.title}</div>
<button className="btn" onClick={() => setshowDescriptionIndex(index)}>
Show description
</button>
<div id={showDescriptionIndex === index ? "display" : "hidden"}>
<div className="description">{val.description}</div>
</div>
</div>
);
})}
</div>
);
}
Try It
import "./styles.css";
import React, { useState } from "react";
export default function App() {
const [showDescription, setshowDescription] = useState({});
const [anArray] = useState([
{ title: "First Div", description: "First description"},
{ title: "Antoher Div", description: "Another description"}
]);
return (
<div className="App">
{anArray.map((val, index) => {
return (
<div className="div" key={index}>
<div className="title">{val.title}</div>
<button className="btn" onClick={() => setshowDescription({...showDescription, [index]: !showDescription[index]})}>
Show description
</button>
<div id={showDescription && showDescription[index] ? "display" : "hidden"}>
{showDescription[index]}
<div className="description">{val.description}</div>
</div>
</div>
);
})}
</div>
);
}
Tip: Use class="show/hide" instead of id="show/hide"
i wrote code to expand "more info" block after clicking button, but function invoking only for first item.
Is it happening beacuse i use let more = document.getElementById("more"); ?
How can i change code for expanding only specifed item?
const Currency = ({ filteredItems, isLoading }) => {
const addListeners = () => {
let more = document.querySelectorAll(".more-info");
more.forEach(item => {
item.addEventListener("click", toggle)
})
console.log(more)
}
const toggle = () => {
let more = document.getElementById("more");
if (more.className === "more-info") {
more.className = "more-info-active";
} else {
more.className = "more-info";
}
}
return isLoading ? (<div className="loader">Loading...</div>) : (
<div items={filteredItems}>
{filteredItems.map((item) => (
<div key={item.id} className="item-wrapper">
<div className="item">
<h2>{item.name}</h2>
<img src={item.image} alt="crypto symbol"></img>
<h3>{item.symbol}</h3>
<p>{item.current_price} pln</p>
<button onLoad={addListeners} onClick={toggle} className="info-btn" id="item-btn" >➜</button>
</div>
<div id="more" className="more-info">
<div className="more-data">
<div className="info-text">
<p>high_24: {item.high_24h}</p>
<p>low_24: {item.low_24h}</p>
</div>
<div>
<p>price_change_24h: {item.price_change_24h}</p>
<p>price_change_percentage_24h: {item.price_change_percentage_24h}</p>
</div>
<div>
<Sparklines className="sparkline" height={60} margin={10} data={item.sparkline_in_7d.price}>
<SparklinesLine style={{fill:"none"}} color="#b777ff" />
</Sparklines>
</div>
</div>
</div>
</div>
))}
</div>
);
}
Dont use document.getElement... , this is a Real DOM but React uses Virtual DOM.
Instead create a state with an array and on onClick event pass item as an argument and store in state , you can store only id e.g.
Last step, check in JSX if state includes item.id , if true then expand
this is an example , keep in mind this is not the only solution. Just simple example.
import React, { useState } from "react";
const fakeData = [
{
id: "123123-dfsdfsd",
name: 'Title One',
description: "Description bla bla bla One"
},
{
id: "343434-dfsdfsd",
name: 'Title Two',
description: "Description bla bla bla Two"
},
{
id: "6767676-dfsdfsd",
name: 'Title Three',
description: "Description bla bla bla Three"
}
]
function App() {
const [tabs, setTabs] = useState([]);
function _onToggle(item) {
const isExist = tabs.includes(item.id)
if (isExist) {
setTabs(prevData => prevData.filter(pd => pd !== item.id))
} else {
setTabs(prevData => [item.id, ...prevData])
}
}
return (
<div className="app">
<div>
{
fakeData.map((item, i) => (
<div key={i}>
<h3 onClick={() => _onToggle(item)}>{item.name}</h3>
<p style={{ display: tabs.includes(item.id) ? 'block' : 'none' }}>
{ item.description }
</p>
</div>
))
}
</div>
</div>
);
}
export default App;
I just started learning reactjs, and i can't do something simple as taking a value from an input and add to an existing variable like when im using basic javascript DOM. I tried using DOM but i guess it doesn't really works on reactjs.
I want to make a button that replaces value of 'name' property individually inside of state with input[type='text'] value, which in this case there are 3 inputs for each property.
how do i do that?
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
state = {
data :[
{Name: 'rommy',
Ages: 24,
Occupation: 'coder'
},
{Name: 'andi',
Ages: 43,
Occupation: 'Teacher'
},
{Name: 'susilo',
Ages: 42,
Occupation: 'Mobile Dev'
}
]
}
renderBiodata = () => {
let {data} = this.state;
return data.map((val) =>{
return(
<div style={{border: '1px solid black'}}>
<div>
{val.Name}
</div>
<div>
{val.Ages}
</div>
<div>
{val.Occupation}
</div>
<div>
<input type="text"/>
</div>
<div>
<input type="button" value="Change Name" onClick="changeName"/>
</div>
<div></div>
<div></div>
</div>
)
})
}
render() {
return (
<div style={{display:'flex', justifyContent: 'center'}}>
{this.renderBiodata()}
</div>
)
}
}
export default App;
I created an example of how to update the state from an input.
https://codesandbox.io/s/gallant-cohen-lv8p0?fontsize=14&hidenavigation=1&theme=dark
I prefer the functional component way but also created a class component with the same behavior.
Let me know if you need more clarity.
You are using an Array of Object concept over here. Changing values in an Array of Object is bit complex. If you understand it well then it's good otherwise I will suggest you to read about it. :)
Suggestion - How to manage React State with Arrays
Code below is solution to your problem.
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
state = {
data: [
{ Name: "rommy", Ages: 24, Occupation: "coder", inputVal: "" },
{ Name: "andi", Ages: 43, Occupation: "Teacher", inputVal: "" },
{ Name: "susilo", Ages: 42, Occupation: "Mobile Dev", inputVal: "" }
]
};
renderBiodata = () => {
let { data } = this.state;
return data.map((val, i) => {
return (
<div style={{ border: "1px solid black" }}>
<div>{val.Name}</div>
<div>{val.Ages}</div>
<div>{val.Occupation}</div>
<div>
<input
type="text"
value={val.inputVal}
onChange={e => {
let _val = Object.assign({}, val);
_val.inputVal = e.target.value;
this.setState((state, prop) => ({
data: state.data.map((x, j) => {
if (i === j) return _val;
return x;
})
}));
}}
/>
</div>
<div>
<input
type="button"
value="Change Name"
onClick={() => {
let _val = Object.assign({}, val);
_val.Name = val.inputVal;
this.setState((state, prop) => ({
data: state.data.map((x, j) => {
if (i === j) return _val;
return x;
})
}));
}}
/>
</div>
<div />
<div />
</div>
);
});
};
render() {
return (
<div style={{ display: "flex", justifyContent: "center" }}>
{this.renderBiodata()}
</div>
);
}
}
export default App;
I am trying to create a form using react-json schema-form. I am new to the custom templates for the same. I would like to have all the widgets in the form in a single row. How to do that ?
i have tried the following (component) , which was from the custom Object from their website, but couldn't get the desired result.
import React from 'react';
import Form from 'react-jsonschema-form';
/* this is my schma*/
const AdHocCheckSchema = {
title: "search",
type: "object",
required: ["searchKeyword", "country"],
properties: {
searchKeyWord: {
type: "string",
title: "Search Keyword"
},
country: {
type: "string",
title: "country",
enum: [
"a",
"b"
],
enumNames: [
"a",
"b"
]
}
}
};
/*this is the ui schema*/
const adHocCheckUiSchema = {
"ui:order": [
"searchKeyWord",
"country"
],
"country": {
"ui:widget": "select"
}
};
function CustomTemplate(props)
{
return (
<div>
{props.title}
{props.description}
{props.properties.map(
element =>
<div className="property-wrapper">{element.content}</div>)}
</div>
);
}
const AdHocCheckComponent = () => {
return (
<Form
className="tp-adhoccheck__horizontal"
schema={AdHocCheckSchema}
uiSchema={adHocCheckUiSchema}
CustomTemplate={CustomTemplate}
/>
);
};
export default AdHocCheckComponent;
i have no idea how to make the input field , select widget and also the button in same line. As of now its looking as in a default form one line after another.
You can customize the look and feel of each field via their templates. Given that the form submits as an object, you'd want to tweak the ObjectFieldTemplate:
https://react-jsonschema-form.readthedocs.io/en/latest/advanced-customization/#object-field-template
In fact, if you go to their playground (https://mozilla-services.github.io/react-jsonschema-form/, "Custom Object" tab link on top), you'll see all the fields in a single row (if your screen resolution is high enough, otherwise they will wrap over into subsequent rows). Their source code for that effect (via a custom ObjectFieldTemplate component( is located here: https://github.com/mozilla-services/react-jsonschema-form/blob/master/playground/samples/customObject.js
function ObjectFieldTemplate({ TitleField, properties, title, description }) {
return (
<div>
<TitleField title={title} />
<div className="row">
{properties.map(prop => (
<div
className="col-lg-2 col-md-4 col-sm-6 col-xs-12"
key={prop.content.key}>
{prop.content}
</div>
))}
</div>
{description}
</div>
);
}
i used customFieldTemplate and flex-box and could make it in a row
export const customFieldTemplate = (props) => {
const {id, label, classNames, required, errors, children} = props;
return (
<div className={classNames}>
<label className="field_label" htmlFor={id}>
<span className="required-field">
{required ? '*' : null}
</span>
{label}
</label>
{children}
{errors}
</div>
);
};
I am using typescript and in my case here is the answer
function ObjectFieldTemplate(props: ObjectFieldTemplateProps) {
return (
<div>
<h3>{props.title}</h3>
<p>{props.description}</p>
{props.properties.map((element) => (
<div className='property-wrapper'>{element.content}</div>
))}
</div>
);
}
<Form
formData={formState}
schema={schema as JSONSchema7}
transformErrors={transformErrors}
onChange={(e) => setFormState(e.formData)}
validator={validator}
onSubmit={() => onSubmit}
templates={{ ObjectFieldTemplate }}
>
Ref:
https://react-jsonschema-form.readthedocs.io/en/latest/advanced-customization/custom-templates/