How to make owl-carousel responsive in React? - reactjs

I am using react-owl-carousel package.
https://www.npmjs.com/package/react-owl-carousel
I have successfully implemented the code as instructed and the carousel is running smoothly.
Problem : Currently I am displaying 4 items simultaneously. And in every screen , these 4 items are coming . Instead of 4 , I want to show 3 items for devices between 768px to 1200px , 2 items between 500px to 767px and 1 item for the devices below 499px.
The option of including "responsive" is there in owl carousel doc. But I am wondering How to include it to achieve the same.
Here is what I have done so far.
import React, { Component } from 'react';
import {Grid, Row, Col , ProgressBar } from 'react-bootstrap';
import UserAvtar from '../common/UserAvtar.js';
import SectionHeaderOfCards from '../common/SectionHeaderOfCards.js';
import OwlCarousel from 'react-owl-carousel';
const options = {
items: 4,
};
class DashboardPage extends Component {
render() {
return (
<div>
<section className="has-small__padding has-grey__bg">
<UserAvtar />
</section>
<section className="has-small__padding">
<Grid>
<SectionHeaderOfCards title="Recommended Matches" />
<OwlCarousel margin={10} >
<div class="item"><h4>1</h4></div>
<div class="item"><h4>2</h4></div>
<div class="item"><h4>3</h4></div>
<div class="item"><h4>4</h4></div>
<div class="item"><h4>5</h4></div>
<div class="item"><h4>6</h4></div>
</OwlCarousel>
</Grid>
</section>
</div>
);
}
}
export default DashboardPage;

You have to use OwlCarousel Options responsive.
Please check official documentation of owlcarousel2 API options to here.
For example use following options for your items state.
options:{
loop: true,
margin:10,
nav:true,
responsive:{
0:{
items:1
},
600:{
items:3
},
1000:{
items:5
}
}
},
Please check demo example to here.
Hope this will help you.

You can follow -
import OwlCarousel from 'react-owl-carousel';
import 'owl.carousel/dist/assets/owl.carousel.css';
const options = {
margin: 30,
responsiveClass: true,
nav: true,
dots: false,
autoplay: false,
navText: ["Prev", "Next"],
smartSpeed: 1000,
responsive: {
0: {
items: 1,
},
400: {
items: 1,
},
600: {
items: 2,
},
700: {
items: 3,
},
1000: {
items: 5,
}
},
};
class Slider extends Component {
render() {
return (
<div>
<OwlCarousel className="slider-items owl-carousel" {...options}>
...
</OwlCarousel>
</div>
);
}
}
export default Slider;

You can make owl-carousel responsive in React like this explained bellow:
Step 1: you need to create state in same component where you want owl-carousel....
Like you have slider.js component so you have to create state in same file ..Like this;
Step 2: And the state you created initialize in responsive property in owl-carousel
import OwlCarousel from 'react-owl-carousel';
import $ from 'jquery';
import 'owl.carousel/dist/assets/owl.carousel.css';
import 'owl.carousel/dist/assets/owl.theme.default.css';
class Slider extends Component {
state= {
responsive:{
0: {
items: 1,
},
450: {
items: 2,
},
600: {
items: 3,
},
1000: {
items: 4,
},
},
}
render() {
return (<OwlCarousel className={'owl-theme'}
loop={true}
margin={10}
nav={true}
dots={false}
autoplay={true}
autoplayTimeout={2000}
items={4}
responsive={this.state.responsive} >
<div className={'item'}>
Item 1
</div>
<div className={'item'}>
Item 2
</div>
<div className={'item'}>
Item 3
</div>
<div className={'item'}>
Item 4
</div>
<div className={'item'}>
Item 5
</div>
</OwlCarousel>

I was getting a type error in typescript, here is the version without type error :
<OwlCarousel
mouseDrag= {false} touchDrag={true}
stagePadding={0} margin={0} autoplay ={true} merge={true} nav dots={true} slideBy={2} dotsEach={1} loop={true}
responsive= {
{
'1':{
items: 1
},
'1025': {
items: 2
}
}
}
>
{reviews}
</OwlCarousel>
hope it helps : )

Related

How to fill circle border based on progress in REACTJS

How to complete the border of circle based on selected option.
Here is four options.
1.Head
2.Body
3.Script
4.End Note
And I have a circle on side. What I am trying to do is, Head is by default active so circle border should be red color till 25% of its total area, then after selection of body it should be 50%. So on and at the end it should 100%.
Here is my code which I tried on click its changing text color till 4 clicks but I want above kind of thing.Being beginner in ReactJS I am unable to get this logic.
import React, { Component } from "react";
export default class Test extends Component {
constructor(props) {
super(props);
this.state = {
title: "Click here",
color:"red",
active:false,
clicks: 0,
}
}
getInitialState() {
return {
count: 0
};
}
changeTitle = () => {
this.setState((prevState) => ({
clicks: prevState.clicks + 1,
title: "New title",color:"green",active:true,
}));
};
render() {
return (
<div>
<div>count:{this.state.clicks}</div>
<h1 onClick={this.changeTitle.bind(this)} >Hello World </h1>
<h1 style={this.state.clicks===1 ? {color:"red"}:
(this.state.clicks===2)?{color:"yellow"}:
(this.state.clicks===2)?{color:"black"}:
{color:"green"}}>This is Magic: {this.state.title}</h1>;
</div>
)
}
}
Shoutout to #Ron for providing an answer in jquery (https://stackoverflow.com/a/50208291/7956790).
Use the css provided by #Ron.
Ok, here's the render function in React:
// your previous code
render() {
return (
<div id="circle-container">
<div className="quarter top-left">
<div className="quarter-fill top-left-fill" onClick={this.handleTopLeftClick}></div>
</div>
<div className="quarter top-right">
<div className="quarter-fill top-right-fill" onClick={this.handleTopRightClick}></div>
</div>
<div className="quarter bottom-left">
<div className="quarter-fill bottom-left-fill" onClick={this.handleBottomLeftClick}></div>
</div>
<div className="quarter bottom-right">
<div className="quarter-fill bottom-right-fill" onClick={this.handleBottomRightClick}></div>
</div>
</div>
);
}
Hook your onClick listeners to your arcs, e.g. onClick={this.handleTopRightClick} and bind them in the constructor function:
constructor(props) {
super(props);
this.handleTopLeftClick.bind(this);
this.handleTopRightClick.bind(this);
this.handleBottomLeftClick.bind(this);
this.handleBottomRightClick.bind(this);
}
And now define your handler functions in your class:
handleTopLeftClick() { }
handleTopRightClick() { }
handleBottomLeftClick() { }
handleBottomLeftClick() { }

How to use owl carousel options in React Owl Carousel?

I am new to React JS. I have tried to use owl carousel in React.
The package link I installed from npm is
https://www.npmjs.com/package/react-owl-carousel
As instructed I have imported Owl Carousel component on specific page. Here is the code:
import React, { Component } from 'react';
import {Grid, Row, Col , ProgressBar } from 'react-bootstrap';
import UserAvtar from '../common/UserAvtar.js';
import SectionHeaderOfCards from '../common/SectionHeaderOfCards.js';
import OwlCarousel from 'react-owl-carousel';
const options = {
items: 4,
};
class DashboardPage extends Component {
render() {
return (
<div>
<section className="has-small__padding has-grey__bg">
<UserAvtar />
</section>
<section className="has-small__padding">
<Grid>
<SectionHeaderOfCards title="Recommended Matches" />
<OwlCarousel margin={10} >
<div class="item"><h4>1</h4></div>
<div class="item"><h4>2</h4></div>
<div class="item"><h4>3</h4></div>
<div class="item"><h4>4</h4></div>
<div class="item"><h4>5</h4></div>
<div class="item"><h4>6</h4></div>
</OwlCarousel>
</Grid>
</section>
</div>
);
}
}
export default DashboardPage;
As default This code outputs 3 items at a time (As 3 is the default value in owl carousel) . I thought of I may initialised the value with 4 , hence tried ,
const options = {
items: 4,
};
But it's not working. There is nothing mentioned in its node package either. Anyone knows how to use owl carousel options in React Owl carousel ?
Apart I want to show 3 items for devices between 768px to 1200px , 2 items between 500px to 767px and 1 item for the devices below 499px.
Here is how normal owl carousel use the option to add responsiveness. https://owlcarousel2.github.io/OwlCarousel2/demos/responsive.html
How to achieve the same in React owl carousel ?
You need to add options object to OwlCarousel component.
Example:
<OwlCarousel margin={10} items={4} >
//...
</OwlCarouse>
OR
<OwlCarousel margin={10} {...options} >
//...
</OwlCarouse>
Best you can use:
render() {
const options = {
items: 1,
nav: true,
navText:["<div className='nav-btn prev-slide'></div>","<div className='nav-btn next-slide'></div>"],
rewind: true,
autoplay: true,
slideBy: 1,
dots: true,
dotsEach: true,
dotData: true
};
return (
<OwlCarousel ref="gallery" options={options}>
...
</OwlCarousel>
)

React-soundloud-embed error

I'm trying to embed a soundcloud track. I've imported the ReactSoundcloud to the page and now I'm getting these errors after using it inside the return. What am I doing wrong? (p.s. the url value is there as a placeholder for testing purposes)
My code:
import React from 'react';
import ReactDOM from 'react-dom';
import ReactSoundcloud from 'react-soundcloud-embed';
import ArtistList from '../../data/artistlist.jsx';
let artistlist2017 = ArtistList.data;
// Artist Section
class Artists extends React.Component{
render(){
var artistPrint = artistlist2017.map(function(artist, index){
return (
<div className="each_artist" key={index}>
<img
src={artist.src} alt=""
/>
<h1>{artist.name}</h1>
<h3>{artist.description}</h3>
<ReactSoundcloud url={"https://soundcloud.com/icebound/dusty-breaks-at-the-bottom-of-the-random-crates"}/>
</div>
);
});
return (
<div>
<div className="artist_block">
<div>
<div className="rotate_left_header">
<h2>
2017 Featured Artists
</h2>
</div>
<div className="artists">{artistPrint}</div>
</div>
</div>
</div>
);
}
};
export default Artists;
I changed my soundcloud component to
class ReactSoundcloud extends React.Component
{
constructor()
{
super();
this.props =
{
width: "100%",
height: "450px",
url: "https://soundcloud.com/icebound/dusty-breaks-at-the-bottom-of-the-random-crates",
autoPlay: false,
hideRelated: false,
showComments: true,
showUser: true,
showReposts: false,
visual: true,
color: "ff5500"
};
}
render() {
const { url, width, height, autoPlay, hideRelated,
showComments, showUser, showReposts, visual, color } = this.props;
var src = visual ?
`https://w.soundcloud.com/player/?url=${url}&auto_play=${autoPlay}&hide_related=${hideRelated}&show_comments=${showComments}&show_user=${showUser}&show_reposts=${showReposts}&visual=${visual}` :
`https://w.soundcloud.com/player/?url=${url}&color=${color}&auto_play=${autoPlay}&hide_related=${hideRelated}&show_comments=${showComments}&show_user=${showUser}&show_reposts=${showReposts}`;
return (
<iframe
width={ width }
height={ visual ? height : "auto" }
scrolling="no"
frameBorder="no"
src={ src } />
);
};
}
module.exports = ReactSoundcloud;
https://github.com/keske/react-soundcloud/blob/master/src/ReactSoundcloud.js
if ya look in source there is bit off diff
first usage of static defaultProps = { give me browsefy error,
second frameBorder="no"in source frameborder="no"
my not tested enought well but music now plays on my page !! :)
hmm i dont know why warning appears
'Warning: ReactSoundcloud(...): When calling super() in ReactSoundcloud, make sure to pass up the same props that your component's constructor was passed.'

material-ui card with openlayers map inside?

I'm trying to place an openlayers map inside a material-ui card component. I've tried placing a div containing the map inside the card text and the card media sections.
Can anyone help me figure out the correct way to put a map inside the card?
import React, {PropTypes} from 'react'
import {connect} from 'react-redux'
import 'openlayers/dist/ol.css';
import ol from 'openlayers';
import {Card, CardActions, CardHeader, CardMedia} from 'material-ui/Card';
import FlatButton from 'material-ui/FlatButton';
import Paper from 'material-ui/Paper'
import baseTheme from 'material-ui/styles/baseThemes/lightBaseTheme'
import getMuiTheme from 'material-ui/styles/getMuiTheme'
import '../components/tap_events'
import styles from '../styles/ExportInfo.css'
import {updateExportInfo} from '../actions/exportsActions.js';
class ExportInfo extends React.Component {
constructor(props) {
super(props)
getChildContext() {
return {muiTheme: getMuiTheme(baseTheme)};
}
componentDidMount() {
this._initializeOpenLayers()
}
_initializeOpenLayers() {
const scaleStyle = {
background: 'white',
};
this._map = new ol.Map({
controls: [
new ol.control.ScaleLine(),
new ol.control.Attribution({
collapsible: false,
collapsed: false,
}),
new ol.control.Zoom({
className: styles.olZoom
})
],
interactions: ol.interaction.defaults({
keyboard: false,
altShiftDragRotate: false,
pinchRotate: false
}),
layers: [
// Order matters here
new ol.layer.Tile({
source: new ol.source.OSM()
}),
],
target: 'infoMap',
view: new ol.View({
projection: "EPSG:3857",
center: [110, 0],
zoom: 2.5,
minZoom: 2.5,
maxZoom: 22,
})
});
}
render() {
const providers = this.props.providers;
console.log("this is it"+providers[0])
return (
<div className={styles.wholeDiv}>
<div className={styles.root}>
<Paper className={styles.paper} zDepth={2} rounded>
<div className={styles.mapCard}>
<Card >
<CardHeader
title="Selected Area of Interest"
actAsExpander={true}
showExpandableButton={true}
/>
<CardMedia expandable={true}>
<div id="infoMap" className={styles.map} ref="olmap">
</div>
</CardMedia>
</Card>
</div>
</Paper>
</div>
</div>
)
}
}
ExportInfo.childContextTypes = {
muiTheme: React.PropTypes.object.isRequired,
}
export default (ExportInfo)
The id of your div (summaryMap) doesn't match target field in olmap configuration ('infoMap') both of them should be the same.
Additionally instead of calling _initializeOpenLayers in componentDidMount. I would recommend using ref callback and initialize target with actual control instead of string.
<div id="summaryMap" className={styles.map} ref={olmapDiv => this. _initializeOpenLayers(olmapDiv)}>
</div>

ag-grid-react does not render properly

Following the sample from the docs:
https://www.ag-grid.com/best-react-data-grid/index.php
After creating new react app (have tried several times on different machines)
create-react-app whatever
If I apply the stylesheets (ag-grid.css & theme-fresh.css) all that is rendered is a gray line across the page. Any other combination renders a blank page. Removing ag-grid.css renders the grid but its jumbled all over the place.
Has anyone used this lately successfully with React? Does anyone recommend something different? (requirements: paging, sorting, filtering, selectable row(s))
thanks :-)
import React, { Component } from 'react';
import {AgGridReact} from 'ag-grid-react';
import '../node_modules/ag-grid/dist/styles/ag-grid.css';
import '../node_modules/ag-grid/dist/styles/theme-fresh.css';
class App extends Component {
constructor(props) {
super(props);
this.state = {
columnDefs: [
{headerName: 'First Name', field: 'name' },
{headerName: 'Last Name', field: 'lname' }
],
rowData: [
{ name: 'Dave', lname: 'Smith' },
{ name: 'Tommy', lname: 'Smith' },
{ name: 'Lea', lname: 'Jones' }
]
}
}
render() {
return (
<div className="ag-fresh">
<AgGridReact
columnDefs={this.state.columnDefs}
rowData={this.state.rowData}
rowSelection="multiple"
enableColResize="true"
enableSorting="true"
enableFilter="true"
groupHeaders="true"
rowHeight="22"
debug="true"
/>
</div>
);
}
}
export default App;
The outer grid required a height :-(
The documentation does not show this. Not sure why there is no min default height for the grid, but there is not.
So essentially you need something like this, where the grid is wrapped with an element which has a height:
<div className="ag-fresh">
<div className="grid_height_fix">
<AgGridReact columnDefs={this.state.columnDefs} rowData={this.state.data.gridDate} >
</AgGridReact>
</div>
</div>
.grid_height_fix {height:800px;}
The new and better way to do this :
const GridExample = () => {
// I use 100% because I set the height of the higher element manually, you can set something like 500px for the height here instead, or calculate it based on the screen size
const containerStyle = useMemo(() => ({ width: '100%', height: '100%' }), []);
const gridStyle = useMemo(() => ({ height: '100%', width: '100%' }), []);
return (
<div style={containerStyle}>
<div style={gridStyle} className="ag-theme-alpine">
<AgGridReact
...
></AgGridReact>
</div>
</div>
);
};
this way you have better controle over the dimensions.

Resources