Align Items one below the other in Semantic UI React - html

Hi people, I am using Semantic UI React for my project. Here I am rendering some content on a Modal. Here you search for the movies and click on add and it adds it below. The problem is that I want the movie list to be aligned with the start of the Search bar. I'm not able to do it. Is there any way using Grid that I can achieve it?
Here is my code:
import React, {useState, useEffect} from 'react';
import {Icon, Button, Modal, Input, Item} from 'semantic-ui-react'
const CreateMovieListModal = (props) => {
const [movieTitle, setMovieTitle] = useState('');
const [movieList, setMovieList] = useState([]);
function handleMovieChange (event, data) {
setMovieTitle(data.value);
}
function addMovie () {
const newMovies = [...movieList, movieTitle];
setMovieList(newMovies);
setMovieTitle('');
}
function showMovieList () {
return movieList.map((currentMovie)=> {
return (
<Item.Group>
<Item>
<Item.Image size='tiny' src='https://react.semantic-ui.com/images/wireframe/image.png' />
<Item.Content verticalAlign='middle'>
<Item.Header as='a'>{currentMovie}</Item.Header>
</Item.Content>
</Item>
</Item.Group>
)
})
}
return (
<Modal open={props.isOpen} onClose={props.onClose}>
<Modal.Header>Add a new list</Modal.Header>
<div style={{marginTop: '10px'}}>
<center>
<Input value={movieTitle} loading={false}
style={{width: '50%'}}
onChange={handleMovieChange}
placeholder='Search For Movies'
/>
<Button onClick={addMovie}>Add Movie</Button>
</center>
</div>
<Modal.Content scrolling>
<div>
{showMovieList()}
</div>
</Modal.Content>
<Modal.Actions>
<Button primary>
Save <Icon name='save' />
</Button>
</Modal.Actions>
</Modal>
);
};
export default CreateMovieListModal;

you need to wrap Input and Movie list into a div, and apply your <center> on that wrapper div:
<center>
<div> <!-- wrapper div -->
<Input value={movieTitle} loading={false}
style={{width: '50%'}}
onChange={handleMovieChange}
placeholder='Search For Movies'
/>
<Button onClick={addMovie}>Add Movie</Button>
<Modal.Content scrolling>
<div>
{showMovieList()}
</div>
</Modal.Content>
</div>
</center>

Related

HTML "hidden" attribute is making page load forever when using with React

I am trying to create a tab component with simple react state. But when using the
hidden
attribute the page start loading forever. Even if I restart the project the problem appears again.
import React, { useState } from "react";
const InventoryMain = () => {
const [tabIndex, setTabIndex] = useState(0);
return (
<div>
<div className="tabs">
<span onClick={()=> setTabIndex(0)} className="tab tab-lifted" >Tab 1</span>
<span onClick={()=> setTabIndex(1)} className="tab tab-lifted tab-active">Tab 2</span>
</div>
<div className="tab-contents">
<div className="tab-1" hidden={tabIndex !== 0}>Tab1 </div>
<div className="tab-2" hidden={tabIndex !== 1}>Tab2</div>
</div>
</div>
);
};
export default InventoryMain;
I was expecting a simple tab and was watching this video.
Simple React Tab video

How to make custom slide change button in Swiper? (Vue.js)

I need to delete default navigation (nextEl, prevEl) and make button with "NEXT SLIDE BUTTON" text scroll to next slide
<swiper class="news-slider" :sliderd-per-view="1" :modules="[Navigation, Pagination, A11y, Lazy]" navigation="navigation" :navigation="{ nextEl: '.nextArrow'}" :pagination="{ clickable: true, dynamicBullets: true, dynamicMainBullets: 5 }" preload-images="false" lazy="lazy">
<swiperSlide class="swiper-slide" v-for="n in 10" :key="n">
<div class="news-container">
<div class="news"><img class="news__image swiper-lazy" src="#/assets/img/lookism-transformed-transformed.png" alt=""/>
<div class="swiper-lazy-preloader"></div>
<div class="news__info">
<div class="news__title">title</div>
<div class="news__description">description</div>
<div class="news__buttons">
<button class="normal-button news-button">more</button>
<button class="next-button"><span class="next-button__text">NEXT SLIDE BUTTON</span><img class="next-button__icon" src="#/assets/img/arrow-right.svg" alt=""/></button>
</div>
</div>
</div>
</div>
</swiperSlide>
</swiper>
<script>
export default {
components: {
Swiper,
SwiperSlide,
},
setup() {
const onSwiper = (swiper) => {
console.log(swiper);
};
const onSlideChange = () => {
console.log('slide change');
};
return {
onSwiper,
onSlideChange,
modules: [Navigation, Pagination, Scrollbar, A11y],
};
},
};
</script>
How can i do that? I will be very grateful for your help
This should do it, all you need to do is import useSwiper and assign it to a const variable and use it in the div element as swiper.slideNext()
And also if you do not wish to use swiper's own elements then remove the navigation module.
Here's the doc source which is clearer
https://swiperjs.com/vue#use-swiper

Tabbed Pages in React Does not allow me switch between tabs

I am trying to make tabs in a react application in which there should be tab A and tab B.
I am doing this in React JS. How do i go about this?
My code is looking like this:
import React from 'react';
import './Dashboard.css';
import 'font-awesome/css/font-awesome.min.css';
import logo2 from './logo2.svg';
import clock from './clock.svg';
const Estimates = () => {
function openPage(pageName,elmnt){
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablink");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].style.backgroundColor = "";
}
document.getElementById(pageName).style.display = "block";
}
return (
<div className="dashboardHome">
<div className="SideBar">
<div className="logo">
<img src={logo2} className="dashlogo" alt="logo" />
</div>
</div>
<div className="headerArea">
<div>
<p><img src={clock} className="clock" alt="clock" /><font className="headerAreaDir"><h4>Estimating</h4></font></p>
</div>
</div>
<div className="dataArea">
<div>
<input type="text" placeholder="  Search customers by name" className="searchBox" /> <p className="SearchFilterClass">Filter by: <select className="MySelector">
<option>7 Days</option>
<option>14 Days</option>
<option>21 Days</option>
</select></p>
</div>
<button className="estimates_area" onClick={openPage('upcoming_estimates',this)}>Upcoming Estimates</button>
<button onClick={openPage('estimates_sent',this)}>Estimates Sent</button>
<div className="estimates_area">
<div>
<div id="upcoming_estimates" className="">
<h2>upcoming Estimates</h2>
<p>This area is for upcoming estimates</p>
</div>
<div id="estimates_sent" className="">
<h2>Sent Estimates</h2>
<p>This area is for Sent Estimates</p>
</div>
</div>
</div>
</div>
</div>
)
}
export default Estimates;
How do i make this into tabbed pages? Please I need some form of assistance here. I want something like this for instance, I click upcoming_estimates, it shows upcoming_estimates div, and the later. just need some clarification here
Edits
The main code is Looking like this now :
import React from 'react';
import './Dashboard.css';
import 'font-awesome/css/font-awesome.min.css';
import logo2 from './logo2.svg';
import clock from './clock.svg';
const Estimates = () => {
function UpcomingEstimates(){
return(
<div id="upcoming_estimates" className="">
<h2>upcoming Estimates</h2>
<p>This area is for upcoming estimates</p>
</div>
)
}
function EstimatesSent(){
return(
<div id="estimates_sent" className="">
<h2>Sent Estimates</h2>
<p>This area is for Sent Estimates</p>
</div>
)
}
return (
<div className="dashboardHome">
<div className="SideBar">
<div className="logo">
<img src={logo2} className="dashlogo" alt="logo" />
</div>
</div>
<div className="headerArea">
<div>
<p><img src={clock} className="clock" alt="clock" /><font className="headerAreaDir"><h4>Estimating</h4></font></p>
</div>
</div>
<div className="dataArea">
<div>
<input type="text" placeholder="  Search customers by name" className="searchBox" /> <p className="SearchFilterClass">Filter by: <select className="MySelector">
<option>7 Days</option>
<option>14 Days</option>
<option>21 Days</option>
</select></p>
</div>
<div className="estimates_area">
<button href="/UpcomingEstimates" >Upcoming Estimates</button>
<button href="/EstimatesSent">Estimates Sent</button>
</div>
</div>
</div>
)
}
export default Estimates;
Then App.Js is looking like this
import HomePage from './HomePage';
import Register from './Register';
import Dashboard from './DashBoard';
import { BrowserRouter, Route,Switch } from 'react-router-dom';
import Estimates from './Estimates';
function App() {
return (
<BrowserRouter>
<Switch>
<Route path = '/' component={HomePage} exact/>
<Route path = '/register' component={Register} />
<Route path = '/estimates-home' component={Estimates} />
<Route path = '/dashboard' component={Dashboard} />
<Route path="/UpcomingEstimates" component={(props) => ( <UpcomingEstimates /> )} />
<Route path="/estimatesSent" component={(props) => ( <EstimatesSent /> )} />
</Switch>
</BrowserRouter>
);
}
export default App;
Try something like this:
import { useState } from "react";
import "./styles.css";
export default function App() {
const [selectedTab, set] = useState(1);
return (
<>
<div>
Click to select tab!
<div
className={`tab ${selectedTab === 1 && "selected"}`}
onClick={() => set(1)}
>
Tab1
</div>
<div
className={`tab ${selectedTab === 2 && "selected"}`}
onClick={() => set(2)}
>
Tab2
</div>
</div>
<div className="content">Content for Tab {selectedTab}</div>
</>
);
}
See result here: https://75zpi.csb.app/
Codesandbox: https://codesandbox.io/s/75zpi
This way it will work, modify button with href to hit urls, which will call Route, separate out the div code into components, and render them
<button className="estimates_area" href="/UpcomingEstimates" >Upcoming Estimates</button>
<button onClick={} href="/estimatesSent">Estimates Sent</button>
You might want to change href url to,
<button className="estimates_area" href="/estimates-home/UpcomingEstimates" >Upcoming Estimates</button>
<button onClick={} href="/estimates-home/estimatesSent">Estimates Sent</button>
This is separate file, UpcomingEstimates.js
function UpcomingEstimates(){
return(
<div id="upcoming_estimates" className="">
<h2>upcoming Estimates</h2>
<p>This area is for upcoming estimates</p>
</div>
)
}
This is separate file, EstimatesSent.js
function EstimatesSent(){
return(
<div id="estimates_sent" className="">
<h2>Sent Estimates</h2>
<p>This area is for Sent Estimates</p>
</div>
)
}
this shows few router changes, in App.js, import both EstimatesSent and UpcomingEstimates,
<Route path = '/estimates-home' component={Estimates} >
<Route path="/UpcomingEstimates" component={(props) => ( <UpcomingEstimates /> )} />
<Route path="/estimatesSent" component={(props) => ( <EstimatesSent /> )} />
</Route>

React map is duplicating DOM Elements

I have the following issue:
I am building a web application, BackEnd in spring and FrontEnd in React.
Now I am adding a feature that shows how many products are in the cart while the client is clicking on "buy". The problem is that when I do a map to get the API in the dom tree, it seems like is duplicating the element.
Images:
Bug: "Carrinho ( )" is being duplicated
Note:
I am consuming two APIs
Code:
import React, { useEffect, useState } from 'react';
import {
Row,
CardBody,
Container,
} from 'reactstrap';
import api from '../../resources/api_produtos';
import apiCart from '../../resources/api_cart';
import axios from 'axios';
const Main = () =>{
const[product, setProduct] = useState([]);
const[cart, setCart] = useState([]);
const fetchData = () =>{
const productApi = api.get('');
const cartApi = apiCart.get('');
axios.all([productApi, cartApi]).then(
axios.spread((...allData) =>{
const allProductData = allData[0].data;
const allDataCart = allData[1].data;
setProduct(allProductData);
setCart(allDataCart);
console.log(allDataCart);
})
)
}
useEffect(() =>{
fetchData()
}, [])
return (
<div>
<div className="p-3 mb-2 bg-dark text-white d-flex justify-content-between">
<div>
<strong>Game Store</strong>
</div>
<div>
{cart.map(carrinho =>(
<div key={carrinho.id}>
<div>
Carrinho ( {carrinho.amount} ) HERE IS BEING DUPLICATED
</div>
</div>
))}
</div>
</div>
<Container>
<div className="jumbotron mt-3"><h1>Produtos</h1></div>
{product.map(produto => (
<div className="card mb-3">
<div key={produto.id}className="card-header d-flex justify-content-between">
<span>
Id: {produto.id}
</span>
<div>
<nav>
<form method="POST" action={"http://localhost:8080/comprar/" + produto.id}>
<input type="submit" className="btn btn-secondary" value="Comprar" ></input>
</form>
</nav>
</div>
</div>
<CardBody>
<Row>
<div className="col-12 col-sm-8 mb-3">
<div className="row">
<div key={produto.id}>
<div >
Nome: {produto.name}
</div>
<div >
Preço: R$ {produto.price}
</div>
</div>
</div>
</div>
<div className="col-12 col-md-4">
<figure key={produto.id}>
<img className="img-thumbnail"src={produto.image} />
</figure>
</div>
</Row>
</CardBody>
</div>
))}
</Container>
</div>
);
}
export default Main;
And this is how the API "Cart" looks like:
[{"amount":"7"},[{"id":12,"name":"Mortal Kombat XL","price":69.99,"score":150,"image":"https://images-americanas.b2w.io/produtos/01/00/offers/01/00/item/126077/6/126077695_1GG.png"},{"id":12,"name":"Mortal Kombat XL","price":69.99,"score":150,"image":"https://images-americanas.b2w.io/produtos/01/00/offers/01/00/item/126077/6/126077695_1GG.png"},{"id":12,"name":"Mortal Kombat XL","price":69.99,"score":150,"image":"https://images-americanas.b2w.io/produtos/01/00/offers/01/00/item/126077/6/126077695_1GG.png"},{"id":12,"name":"Mortal Kombat XL","price":69.99,"score":150,"image":"https://images-americanas.b2w.io/produtos/01/00/offers/01/00/item/126077/6/126077695_1GG.png"},{"id":12,"name":"Mortal Kombat XL","price":69.99,"score":150,"image":"https://images-americanas.b2w.io/produtos/01/00/offers/01/00/item/126077/6/126077695_1GG.png"},{"id":12,"name":"Mortal Kombat XL","price":69.99,"score":150,"image":"https://images-americanas.b2w.io/produtos/01/00/offers/01/00/item/126077/6/126077695_1GG.png"},{"id":12,"name":"Mortal Kombat XL","price":69.99,"score":150,"image":"https://images-americanas.b2w.io/produtos/01/00/offers/01/00/item/126077/6/126077695_1GG.png"}]]
How do I fix this duplication?
The problem is the response object from your cart API.
It's bad-formed, since your BE is returning an array which gives no sense since the response includes one single cart.
So, the map function iterate over an array, modifying its content by the callback, place them in a new array, and then returns it.
So essentially you are trying to modify the obj on the index 0, which is and obj with the "amount" field, and then, you are trying to map an array at index 1.
So you have to update your response from the BE, something like that:
{
"id": "cart_id",
"items": []
}
Which has more sense compared with yours. Note that since items is an array you don't need the "amount" field, you can access it with carrinho.items.length for instance. Then render it with
<div key={carrinho.id}>
<div>Carrinho ({carrinho.items.length})</div>
</div>

how to use/implement input tags and custom jsx tag for syntax highlighting using Highlight.JS?

I was just searching around how to highlight code just like Text editors on website and I found some libraries and I liked highlight.js and started to play around with it.
But to my sense it worked well except for the <input/>tags and custom react component tags doesn't get rendered well.
I followed the documentation and implement the code in following manner:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.1/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.1/styles/night-owl.min.css">
<div class="code-container" style="margin: auto; width: 50%;">
<pre><code data-language="html">
import React, { Component } from 'react'
import './reg-style/Credentail.css'
import 'react-phone-number-input/style.css'
import PhoneInput from 'react-phone-number-input'
export class Credential extends Component {
continue = e => {
e.preventDefault();
this.props.nextStep();
};
back = e => {
e.preventDefault();
this.props.prevStep();
};
render() {
const { values, handleChange } = this.props;
return (
<pre>
<code data-language="html">
<div>
<Helmet>
<body className="form-bg-col"></body>
</Helmet>
<div className="login">
<div className="reg-container">
<button onClick={this.back} className="col-form-prev"><i class="fa fa-chevron-left"></i></button>
<div className="user-detail email">
Email:
<input onChange={handleChange('Email')} defaultValue={values.Email} type="email" placeholder="Enter your Email"/>
</div>
<div className="user-detail ">
Phone:
<PhoneInput
onChange={handleChange('Phone')} defaultValue={values.Phone}
displayInitialValueAsLocalNumber
placeholder="Enter phone number"
defaultCountry="IN"
/>
</div>
<div className="user-detail username">
Username:
<code><input onChange={handleChange('Username')} defaultValue={values.Username} type="text" placeholder="Select your unique Username" /></code>
</div>
<div className="user-detail password">
Password:
<input onChange={handleChange('Password')} defaultValue={values.Password} type="password" placeholder="Choose your Password" />
</div>
<button onClick={this.continue} className="col-form-next"><i class="fa fa-chevron-right"></i></button>
</div>
</div>
</div>
</code>
</pre>
)
}
}
export default Credential
</code></pre>
</div>
and result was as following:
What should i do to rectify these issues?
Thanks...
All the HTML inside your pre/code block HTML needs to be properly escaped to avoid code injection.... This is just how HTML works.
Bad (this is actual HTML and will be rendered by the browser):
<pre><code>
<strong>Hey I'm HTML</strong>
</code></pre>
Good (now it's just text [when rendered]):
<pre><code>
<strong>Hey I'm HTML</strong>
</code></pre>
[Disclaim: I'm the current Highlight.js maintainer.]