React default className ... Before ternary operator kicks in - javascript

import './App.css'
import {Button} from "antd"
<Button
shape="round"
className={
istrue === "itstrue" ? "trueclass" : "trueclass active"
}
>
TEXT
</Button>
Is it possible to set a default className ... just to start because const [istrue, setIsTrue] = useState(["itstrue"]); isn't working. It starts with itstrue state but ... the button always starts out with the non active state. I need it to start with the active state.
Can someone please help me? Thank you

You need to specify the conditional class names in react as:
className={"trueclass " + (!itstrue ? 'active' : '')}
By this the trueclass will be applied independent of the condition and the class active will only be applied when itstrue is false.

Related

Highlight current and up-one-level URL

I have a list of navlinks. When I'm on a certain page, that navlink should be highlighted. I also want the page up (only) one level to have its navlink highlighted as well, so:
All pages: /blogs, blogs/careers, blogs/authors
Page: /blogs/author
Highlight: /blogs/author, /blogs
Page: /blogs/author/Lauren-Stephenson
Highlight: /blogs/author/Lauren-Stephenson, blogs/authors
Here's how I'm doing it:
import React from 'react';
const navlinks = ["/blogs", "blogs/careers", "blogs/authors"]
const currentPath = "/blogs/authors/Lauren-Stephenson"
export function App(props) {
return (
<div className='App'>
{navlinks.map((links) =>
<div style={{color: currentPath.includes(links) ? 'green' : 'white'}}>{links}</div>
)}
</div>
);
}
But my code not only highlights /blogs/Authors/, it also highlights /blogs, which is incorrect, because I only want the page up one level to be highlighted.
How can I do this?
so maybe adding this condidtion might help you!
style={{color: currentPath.includes(links) && currentPath.length!==links.length ? 'green' : 'white'}}
it will be sure that it's not the same, if this wasn't the case you can change it from equals to < or > based on your need

Antd Segmented: Set tabindex=-1 to the internal Input component

I'm using the Segmented component by Ant Design. My application has some component like this:
...
import EmployeeCard from './EmployeeCard';
import ContractorCard from './ContractorCard';
function MyComponent() {
const [cardType, setCardType] = useState('Employee');
...
function changeCard(value) {
setCardType(value);
}
return (
...
<Segmented
options={['Employee', 'Contractor']}
onChange={changeCard)
value={cardType}
tabIndex={-1}
/>
{cardType === 'Employee'
? <EmployeeCard />
: <ContractorCard />
}
...
);
}
The EmployeeCard and ContractorCard have some input components in them that I want to be part of the overall tab flow on the page. However, I don't want the Segmented buttons themselves to be part of it. I set tabIndex={-1} on the Segmented component in order to take it out of the tab-flow, but apparently, internally, that component includes an Input component, which still has the default tabIndex={0}. I cannot figure out how to reach that component in order to set its tab index to -1.

REACT : change button color when active does not work

I want my side bar button to change color when I route to its linked page.
my code changes the color and the color remains the same even if I route to a different page.
here is my code:
lass Sidebar extends Component<{}, {}> {
style = this.getBtnClassName();
render() {
return (
<aside className="sidebar">
<nav className="nav">
<LinkButton to="/timer" label="Timer" cssPrefix={this.getBtnClassName()} styles={['block']} />
<LinkButton to="/reports" label="Report" cssPrefix={this.getBtnClassName()} styles={['block']} />
</nav>
</aside>
);
}
getBtnClassName() {
if (window.location.href === 'http://localhost:3000/timer') {
return 'nav_active';
} else {
return 'nav__btn';
}
}
}
export default Sidebar;
You can use className instead.
Start out by simply making your getBtnClassname simply use the window.location.pathname and not the window.location.href, then it can also work when deployed to any website.
getBtnClassName() {
if (window.location.pathname === '/timer') {
return 'nav_active';
} else {
return 'nav__btn';
}
}
From there, all you really need to do is call the function to assign the returned value of this.getBtnClassName to be a classname you wish to apply.
<button to="/timer" label="Timer" className={this.getBtnClassName()}>{this.getBtnClassName()}</button>
You could also assign the correct class to a variable inside the render, which when you only have 2 classes in your case is a little cleaner.
const btnClass = window.location.pathname === "/timer" ? "nav_active" : "nav__btn"
<button to="/reports" label="Report" className={btnClass}>{btnClass}</button>
Example:
https://stackblitz.com/edit/react-scltse
you can use NavLink from react-router-dom
and use the prop activeClassName
ref: https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/docs/api/NavLink.md

if else statement in map function reactjs

You don't have to read the whole code, just read the comment in the editQuantity function and in showOrderItem function, specially in the showOrderItem function and my problem is i think just silly, as both of my function are working as they supposed to work,
*editQuantity function supposed to change the state, it changing it, i checked by adding the console line.
*showOrderItem function supposed display the item, he is doing that job as well.
My problem is, i try to add conditional rendering in the showOrderItem function that not working, even though i am able to change the state.
Please read the comment in showOrderItem function, to see where is the problem:
import React from 'react';
export default class ShowOrder extends React.Component{
constructor(props){
super(props);
this.state={
quantityEditing:this.props.orderItems,
}
}
editQuantity(item){
const order=this.state.quantityEditing;
for(var i =0; i<order.length; i++){
if(order[i].item==item){
console.log(order[i].orderQuantityEditing)
order[i].orderQuantityEditing=true;
this.setState({order:this.state.quantityEditing})
console.log(order[i].orderQuantityEditing)
}
}
}
showOrderItem(){
const style = {cursor:'pointer'}
const orderItems=this.state.quantityEditing;
console.log(orderItems)
const orderItem=orderItems.map((item,index)=>
<p>
{orderItems.orderQuantityEditing ? 'some':
<span style={style} onClick={this.editQuantity.bind(this,item.item)}>
//as you can see in here i added conditional rendering, that if orderItems.orderQuantityEditing is true display me some, but that's not working --orderItems.orderQuantityEditing ? 'some'(this part) does not matter how many times i click on property it never display me my string 'some'
{item.quantity}</span>}
<span style={style}> {item.item}</span>
<span style={style}> Q</span>
<span style={style}> N</span>
<span style={style}> X</span>
</p>
);
return orderItem;
}
render(){
return(
<div>
{this.showOrderItem()}
</div>
);
}
}
Instead of
{orderItems.orderQuantityEditing ?
'some'
:
<span style={style} onClick{this.editQuantity.bind(this,item.item)}>
I think you need to write this:
{item.orderQuantityEditing ?
'some'
:
<span style={style} onClick={this.editQuantity.bind(this,item.item)}>
Because you are using map, and item will be each object of array, so you need to use item to access that property. During the for loop, when changing the state you wrote:
order[i].orderQuantityEditing=true;
That it proper, order will be an array and you need to provide the index to access particular object of that.

React: Dropdown component shown once

I have a drop down component that looks like this:
{...}
this.state = {
isVisible: false
}
}
toggleDisplay() {
this.setState({isVisible: !this.state.isVisible});
}
render() {
return (
<div>
<button onClick={this.toggleDisplay()}>click</button>
{this.state.isVisible ? <MenuElements toggleDisplay={this.toggleDisplay} /> : '' }
</div>
)
}
}
"MenuElements" is just a ul that has a li. On another page i am using this component multiple times, so whenever i click on the button, "MenuElements" is shown for each click. The problem is that i want only one component to be displayed. So if a MenuElements component is already displayed, if i click on another button, it closes the previous component, and opens the second one.
How could this be implemented in my code?
Thanks.
You will somehow need to have a single state that defines which MenuItem is displayed. You could go with a global state with something like Redux, but if you are trying to build a reusable component, I guess it'd be best to wrap all of the MenuItem components in a parent component and keep a state there. That, I think, is the React way of doing it. Read this for an idea of how to design components: https://facebook.github.io/react/docs/thinking-in-react.html.
BTW, I think there is an error in the Button onClick handler. It should be:
<button onClick={this.toggleDisplay.bind(this)}> // or bind it somewhere else
Also, the correct way to change state based on previous state is this:
// Correct
this.setState((prevState, props) => ({
counter: prevState.counter + props.increment
}));
// Wrong
this.setState({
counter: this.state.counter + this.props.increment,
});
I'd say this is du to the context of your callbacks. Have you tried forcing the context ?
<div>
<button onClick={this.toggleDisplay.bind(this)}>
click
</button>
{this.state.isVisible ?
<MenuElements toggleDisplay={this.toggleDisplay.bind(this)} />
: '' }
</div>

Categories