React-Elastic-Carousel Custom Arrow - reactjs

I wanted to get help for adding custom arrows to my react carousel, but I am getting the error for 'type' is not defined no-undef. Can someone please tell me what I am doing wrong? and preferably a solution for the current situation.
Thank you
import Carousel, { consts } from 'react-elastic-carousel';
import LeftArrow from './Assets/Group 1316.svg'
import RightArrow from './Assets/Group 1317.svg'
export default function BootcampNew (props) {
const breakPoints = [
{ width: 1, itemsToShow: 1 },
{ width: 550, itemsToShow: 2 },
{ width: 768, itemsToShow: 3 },
{ width: 900, itemsToShow: 4 },
];
type === consts.PREV;
const myArrow = ({type,onClick, isEdge}) => {
const pointer = type === consts.PREV ? {LeftArrow} : {RightArrow}
return(
<Button onClick={onClick} disabled={isEdge}>{pointer}</Button>
)
}
<Carousel
renderArrow={myArrow}
breakPoints={breakPoints}
pagination={false}>
<CourseCard/>
<Carousel/>

you can't type type === consts.PREV;
without any definition and the only use of it, in this case, is to be inside a condition, the other thing you don't return what's being rendered by the functional component, the below code should solve the problems.
import Carousel, { consts } from 'react-elastic-carousel';
import LeftArrow from './Assets/Group 1316.svg'
import RightArrow from './Assets/Group 1317.svg'
function myArrow({ type, onClick, isEdge }) {
const pointer = type === consts.PREV ? {LeftArrow} : {RightArrow}
return (
<button onClick={onClick} disabled={isEdge}>
{pointer}
</button>
)
}
export default function BootcampNew (props) {
const breakPoints = [
{ width: 1, itemsToShow: 1 },
{ width: 550, itemsToShow: 2 },
{ width: 768, itemsToShow: 3 },
{ width: 900, itemsToShow: 4 },
];
return (
<Carousel renderArrow={myArrow} breakPoints={breakPoints} pagination={false}>
<CourseCard/>
</Carousel>
);
}

const pointer = type === consts.PREV ? : <img src={leftArrow} />:<img src={rightArrow} />

Related

fontSize in Treemap react apexchart not working

I'm using react apexcharts to render a treemap
I'm using fontSize inside dataLabels to set fontSize but it isn't working.
dataLabels: {
//enabled: true,
style: {
fontSize: "20px",
},
Here is the screenshot
Also I have used -
return `${textList[0]}
${textList[1]}
${textList[2]}`
-to return string in multiple lines but that also doesn't seem to be working.
Below is the full code
import React from 'react';
//import ReactApexChart from 'react-apexcharts';
import { ApexOptions } from "apexcharts";
import dynamic from 'next/dynamic';
import { getTenantDetails } from "utils/assetOverview";
const ReactApexChart = dynamic(() => import('react-apexcharts'), { ssr: false });
type datapointType={
value: number
seriesIndex: number
dataPointIndex: number
}
type tenantType={
x:string,
y:number
}
const TreeMap : React.FC<{areaChecked:boolean,assetCode:string}> = ({areaChecked,assetCode}) => {
let {tenantList} = getTenantDetails(assetCode);
let tenants = [] as tenantType[]
tenants = tenantList && tenantList.map((tenant)=>{
if(areaChecked){
return {
"x":`${tenant.name}\n${tenant.area}ft (${tenant.percentage}%)\n${tenant.years}`,
"y":tenant.area
}
}else{
return {
"x":`${tenant.name}\n$${tenant.revenue} (${tenant.percentage}%)\n${tenant.years}`,
"y":tenant.revenue
}
}
})
const series =[
{
data:tenants
}
]
const options:ApexOptions = {
legend: {
show: false
},
colors:['#002776'],
chart: {
height: 260,
toolbar:{
show:false
}
},
plotOptions: {
treemap: {
enableShades: false
}
},
dataLabels: {
//enabled: true,
style: {
fontSize: "20px",
},
formatter: function(text:string,o:datapointType){
let textList = text.split("\n");
return `${textList[0]}
${textList[1]}
${textList[2]}`
}
}
}
return (
<React.Fragment>
<ReactApexChart options={options} series={series} type="treemap" height={260} />
</React.Fragment>
);
}
export default TreeMap;
Also tried to set fontSize in global.scss file but the text in each rectangle overflows- Here is the screenshot
.apexcharts-data-labels > text {
font-size: 10px !important;
}
SO tried to add
.apexcharts-data-labels > text {
overflow-wrap: break-word !important;
}
but the above code doesn't seem to work

How to disable a specific row to be draggable in AG table?

I am using the Managed Dragging of AG Grid React table and want to disable a specific row, if it matches the condition.
In Docs I couldn't find enough information how to do that. As it describes here, it is possible to add the draggable feature conditionally, like this
rowDrag: params => !params.node.group
In params object, I couldn't find the row data to implement my condition.
In the code example described below, I want to disable the row to be draggable if the name==='John.
Also, how to that if you have row draggable for entire row: rowDragEntireRow={true}?
Sandbox demo and code
import React from "react";
import { AgGridReact } from "ag-grid-react";
import "ag-grid-community/dist/styles/ag-grid.css";
import "ag-grid-community/dist/styles/ag-theme-alpine.css";
function App() {
const [gridApi, setGridApi] = React.useState(null);
const [gridColumnApi, setGridColumnApi] = React.useState(null);
const onGridReady = (params) => {
setGridApi(params.api);
setGridColumnApi(params.columnApi);
};
const defaultColDef = {
flex: 1,
editable: true
};
const columnDefs = [
{
headerName: "Name",
field: "name",
rowDrag: (params) => {
console.log("params", params);
return !params.node.group;
}
},
{ headerName: "stop", field: "stop" },
{
headerName: "duration",
field: "duration"
}
];
const rowData = React.useMemo(
() => [
{
name: "John",
stop: 10,
duration: 5
},
{
name: "David",
stop: 15,
duration: 8
},
{
name: "Dan",
stop: 20,
duration: 6
}
],
[]
);
return (
<div>
<h1 align="center">React-App</h1>
<div>
<div className="ag-theme-alpine" style={{ height: "700px" }}>
<AgGridReact
columnDefs={columnDefs}
rowData={rowData}
defaultColDef={defaultColDef}
onGridReady={onGridReady}
rowDragManaged={true}
//rowDragEntireRow={true}
></AgGridReact>
</div>
</div>
</div>
);
}
export default App;
Any help will be appreciated
Update your rowDrag definition in the name column definition to the following:
rowDrag: (params) => {
if (params.data.name == "John") {
return false;
}
return true;
}
Demo.

How to test a component that receives a ref as props?

I want to snapshot a component in React using react-test-renderer. The component I want to test receives a ref from another component. The component I'm testing relies on a function implemented by the component which is passing the ref as props:
import React from "react";
import { makeStyles, Paper, Typography } from "#material-ui/core";
import { INodeInfoProps } from "./interfaces";
const useStyles = makeStyles({
container: {
position: "absolute",
padding: 10,
maxHeight: 600,
width: 400,
overflowWrap: "break-word",
"& p": {
fontSize: 12,
},
},
channels: {
display: "flex",
},
channelsComponent: {
marginLeft: 5,
},
});
export const NodeInfo: React.FC<INodeInfoProps> = ({ graphRef, info }) => {
const classes = useStyles();
const getDivCoords = () => {
if (graphRef.current) {
const nodeCoordinates = graphRef.current.graph2ScreenCoords(
info?.x || 0,
info?.y || 0,
info?.z || 0
);
return {
top: nodeCoordinates.y + 20,
left: nodeCoordinates.x,
};
}
return { top: 0, left: 0 };
};
if (info && graphRef.current) {
return (
<Paper
className={classes.container}
style={{
top: getDivCoords().top,
left: getDivCoords().left,
}}
>
<Typography>Pubkey: {info.publicKey}</Typography>
<Typography>Alias: {info.alias}</Typography>
</Paper>
);
}
return null;
};
So the function graph2ScreenCoords is implemented in the component which the ref is received by props by my component.
My test component would look like this:
import React from "react";
import renderer from "react-test-renderer"
import {NodeInfo} from "../index";
it('should render each node info', () => {
const info = {
publicKey: "test123",
alias: "test",
color: "#fff",
visible: true,
links: [
{
channelId: "123",
node1: "test123",
node2: "test345",
capacity: "10000",
color: "#fff"
}
]
}
const tree = renderer.create(<NodeInfo graphRef={} info={info}/>).toJSON()
expect(tree).toMatchSnapshot();
})
But I need to pass the ref to the test component, so it can access the function graph2ScreenCoords.
How should I make it the right way? Should I render the component in my test, create a ref and pass it as props? Should I mock the ref? How?
Thanks in advance

ReactJS: Change html/jsx element dynamically

I wanna change my JSX element tag dynamically but the remaining attributes stay the same.
Let's say I have something like this:-
import React, { useState } from 'react'
import classNames from 'classnames'
import { makeStyles } from '#material-ui/core/styles'
import DesktopWindowsIcon from '#material-ui/icons/DesktopWindows'
import DnsIcon from '#material-ui/icons/Dns'
import StorageIcon from '#material-ui/icons/Storage'
import CloudIcon from '#material-ui/icons/Cloud'
export const try = () => {
const classes = useStyles()
const [changeIconColor, setChangeIconColor] = useState('')
const icons = [
{ id: 0, icon: <DesktopWindowIcon /> },
{ id: 1, icon: <DnsIcon /> },
{ id: 2, icon: <StorageIcon /> },
{ id: 3, icon: <CloudIcon /> },
]
return (
<>
{icons.maps(icon => (
<>
{/* this will work */}
{icon.icon}
</>
))}
</>
)
}
const useStyles = makeStyles((theme) => ({
icon: {
width: 100,
height: 100,
marginBottom: 12,
},
iconMouseHover: {
color: theme.palette.secondary.main
}
}))
But what I wanna do is something like this:-
import React, { useState } from 'react'
import classNames from 'classnames'
import { makeStyles } from '#material-ui/core/styles'
import DesktopWindowsIcon from '#material-ui/icons/DesktopWindows'
import DnsIcon from '#material-ui/icons/Dns'
import StorageIcon from '#material-ui/icons/Storage'
import CloudIcon from '#material-ui/icons/Cloud'
export const try = () => {
const classes = useStyles()
const [changeIconColor, setChangeIconColor] = useState('')
const icons = [
{ id: 0, icon: <DesktopWindowsIcon key={icon.id} className={changeIconColor === icon.id ? classNames(classes.icon, classes.iconMouseHover) : classes.icon} /> },
{ id: 1, icon: <DnsIcon key={icon.id} className={changeIconColor === icon.id ? classNames(classes.icon, classes.iconMouseHover) : classes.icon} /> },
{ id: 2, icon: <StorageIcon key={icon.id} className={changeIconColor === icon.id ? classNames(classes.icon, classes.iconMouseHover) : classes.icon} /> },
{ id: 3, icon: <CloudIcon key={icon.id} className={changeIconColor === icon.id ? classNames(classes.icon, classes.iconMouseHover) : classes.icon} /> },
]
return (
<>
{icons.maps(icon => (
<>
{/* this will not work since it gave me an error saying icon is not defined in array above */}
{icon.icon}
</>
))}
</>
)
}
const useStyles = makeStyles((theme) => ({
icon: {
width: 100,
height: 100,
marginBottom: 12,
},
iconMouseHover: {
color: theme.palette.secondary.main
}
}))
Is there any ways for me to do this dynamically with React?
Something that can change the icon tag but the remaining attributes stay the same:-
// only tag name changes
<OnlyThisChange className={changeIconColor === skill._id ? classNames(classes.icon, classes.iconMouseHover) : classes.icon} />
Is this possible with react?
yes, this possible.
First of all, you don't need to write a key in the icon component. Prop key must be defined in the Fragment element(<> -> <React.Fragment key={icon.id}>).
Your example is almost correct, one mistake that you're doing is:
changeIconColor === icon.id
instead of the above example, you can hardcode your ids:
changeIconColor === 1

I have a card component that I need to update to add new items

I built a card component that shows a list of user data and images with antd on nextJs. I want to build a functionality that creates a modal to input new data and image and adds it to the user interface as a new card, but I am confused on how to get my hands around it. I need assistance. Here's a link to my code!
import React from 'react';
import { Avatar, Card, Icon, List } from 'antd';
import { ICON_LIST, LIST_TEXTS, STYLES, USER_UPLOAD } from './constants';
const { AVATAR, CARD_CONTAINER, ICON, USER_LIST } = STYLES;
const { INNER, MORE, UPLOAD, VERTICAL } = LIST_TEXTS
class Home extends React.Component {
state = {
clicks: 0,
};
IncrementIconText = () => {
this.setState({ clicks: this.state.clicks + 1 });
}
render() {
const actions = ( ICON_LIST.map(({ type }) => (
<span>
<Icon key={type} type={type} onClick={this.IncrementIconText} style={ICON} />
{this.state.clicks}
</span>
)));
return (
<List
itemLayout={VERTICAL}
dataSource={USER_UPLOAD}
renderItem={item => (
<List.Item style={USER_LIST}>
<Card
actions={actions}
cover={<img alt={UPLOAD} src={item.image} />}
extra={<Icon type={MORE} />}
hoverable
title={<a><Avatar src={item.image} style={AVATAR} />{item.user}</a>}
type={INNER}
style={CARD_CONTAINER}
>
{item.story}
</Card>
</List.Item>
)}
/>
);
}
}
export default Home;
constants.js
export const ICON_LIST = [
{
key: "heart",
type: "heart",
},
{
key: "dislike",
type: "dislike",
},
{
key: "meh",
type: "meh",
},
]
export const LIST_TEXTS = {
INNER: "inner",
MORE: "more",
UPLOAD: "upload",
VERTICAL: "vertical",
};
export const STYLES = {
AVATAR: {
marginRight: 8
},
CARD_CONTAINER: {
width: "650px",
marginBottom: 50
},
ICON: {
marginRight: 8
},
USER_LIST: {
width: "100%",
display: "flex",
justifyContent: "center",
alignItems: "center"
},
};
export const USER_UPLOAD = [
{
image: "http://sugarweddings.com/files/styles/width-640/public/1.%20The%20Full%20Ankara%20Ball%20Wedding%20Gown%20#therealrhonkefella.PNG",
story: "Today's my birthday next week! What do you think?",
user: "Chioma",
},
{
image: "https://dailymedia.com.ng/wp-content/uploads/2018/10/7915550_img20181007141132_jpeg01c125e1588ffeee95a6f121c35cd378-1.jpg",
story: "Going for an event. Do you like my outfit",
user: "Simpcy",
},
{
image: "https://i0.wp.com/www.od9jastyles.com/wp-content/uploads/2018/01/ankara-styles-ankara-styles-gown-ankara-tops-ankara-gowns-ankara-styles-pictures-latest-ankara-style-2018-latest-ankara-styles-ankara-ankara-styles.png?fit=437%2C544&ssl=1",
story: "Saturdays are for weddings. Yay or nay!",
user: "Angela",
},
]
So this could get you started:
https://codesandbox.io/s/1r7j6lom34?fontsize=14
I moved your static USER_UPLOAD into the state of Home and wrote a method to add a new upload to that state.
You would now need to come up with a component that shows your modal and calls AddUpload with the right values.
Also your card-actions don't seem to function properly. To fix that i suggest creating a wrapper component for Card that has a state with the appropriate click counters. That way every card has its own clickcounters.

Resources