How to render a React component with survey.js? - reactjs

Here is a working survey.js with react example: http://plnkr.co/edit/qXdeQa6x2FHRg0YrOlPL?p=preview
My html also does have in head the <script src="https://unpkg.com/survey-react"></script> included.
In my main.jsx I'm calling the rendering of a div that will be filled up based on a json file and by multiple react components, in my json array I have a questions json object too for survey.js. What I would like to achieve is to render a survey when the questions object is being reached in parsing the array.
In my top level component I have this calling for it:
if (section.hasOwnProperty('questions')){
return <Questions key={i+"questions"} id={i+"questions"} questions={section.questions} />;
The Questions.jsx:
var React = require('react');
var ReactDOM = require('react-dom');
var Survey = require('survey-react');
var Questions = React.createClass({
render: function() {
Survey.Survey.cssType = "bootstrap";
Survey.defaultBootstrapCss.navigationButton = "btn btn-green";
window[this.props.id] = new Survey.Model( this.props.questions );
var questionid = this.props.id;
var idi = this.props.id.replace("questions","");
return (
<dt>
<div id={this.props.id}></div>
</div>
</dt>
);
}
});
module.exports = Questions;
ReactDOM.render(<Survey.Survey model={window[questionid]} />, document.getElementById({questionid}));
It does compile without errors but then in the browser I get the console errors:
TypeError: WEBPACK_IMPORTED_MODULE_1_react is undefined[Learn
More]
ReferenceError: questionid is not defined
Seems like I try to do it the wrong way, but how to do it right? I'm new to react and not familiar with using reactDOM inside of a component and is my first project on survey.js as well.
Thank you for helping out.

Please check this code:
import React, { Component } from 'react';
import { render } from 'react-dom';
import * as Survey from 'survey-react';
import 'survey-react/survey.css';
import 'bootstrap/dist/css/bootstrap.css'
import './style.css';
class App extends Component {
componentWillMount() {
Survey.Survey.cssType = "bootstrap";
Survey.defaultBootstrapCss.navigationButton = "btn btn-green";
}
render() {
var json = { title: "Product Feedback Survey Example", showProgressBar: "top", pages: [
{questions: [
{ type: "matrix", name: "Quality", title: "Please indicate if you agree or disagree with the following statements",
columns: [{ value: 1, text: "Strongly Disagree" },
{ value: 2, text: "Disagree" },
{ value: 3, text: "Neutral" },
{ value: 4, text: "Agree" },
{ value: 5, text: "Strongly Agree" }],
rows: [{ value: "affordable", text: "Product is affordable" },
{ value: "does what it claims", text: "Product does what it claims" },
{ value: "better then others", text: "Product is better than other products on the market" },
{ value: "easy to use", text: "Product is easy to use" }]},
{ type: "rating", name: "satisfaction", title: "How satisfied are you with the Product?",
mininumRateDescription: "Not Satisfied", maximumRateDescription: "Completely satisfied" },
{ type: "rating", name: "recommend friends", visibleIf: "{satisfaction} > 3",
title: "How likely are you to recommend the Product to a friend or co-worker?",
mininumRateDescription: "Will not recommend", maximumRateDescription: "I will recommend" },
{ type: "comment", name: "suggestions", title:"What would make you more satisfied with the Product?", }
]},
{questions: [
{ type: "radiogroup", name: "price to competitors",
title: "Compared to our competitors, do you feel the Product is",
choices: ["Less expensive", "Priced about the same", "More expensive", "Not sure"]},
{ type: "radiogroup", name: "price", title: "Do you feel our current price is merited by our product?",
choices: ["correct|Yes, the price is about right",
"low|No, the price is too low for your product",
"high|No, the price is too high for your product"]},
{ type: "multipletext", name: "pricelimit", title: "What is the... ",
items: [{ name: "mostamount", title: "Most amount you would every pay for a product like ours" },
{ name: "leastamount", title: "The least amount you would feel comfortable paying" }]}
]},
{ questions: [
{ type: "text", name: "email",
title: "Thank you for taking our survey. Your survey is almost complete, please enter your email address in the box below if you wish to participate in our drawing, then press the 'Submit' button."}
]}
]};
var model = new Survey.Model(json);
return (
<Survey.Survey model={model}/>
);
}
}
render(<App />, document.getElementById('root'));
and here a live example: https://stackblitz.com/edit/surveyjs-react-stackoverflow45544026

Related

React ant design table cell collapse issue

Im using my react type script project for ant design table
i want to know how to do that following image like as table, im search any tutorial but not seen anything, any one know how to do that correctly.
code sand box here
Thanks
image here
code here
class App extends React.Component {
columns: any = [
{
title: "Name",
dataIndex: "name",
key: "name"
},
{
title: "Age",
dataIndex: "age",
key: "age"
},
{
title: "Address",
dataIndex: "address",
key: "address"
},
{
title: "Tags",
key: "tags",
dataIndex: "tags"
},
{
title: "Action",
key: "action"
}
];
data: any = [
{
key: "1",
name: "John Brown",
age: 32,
address: "New York No. 1 Lake Park",
tags: ["nice", "developer"]
},
{
key: "2",
name: "Jim Green",
age: 42,
address: "London No. 1 Lake Park",
tags: ["loser"]
},
{
key: "3",
name: "Joe Black",
age: 32,
address: "Sidney No. 1 Lake Park",
tags: ["cool", "teacher"]
}
];
render() {
return <Table columns={this.columns} dataSource={this.data} />;
}
}
You want to create 2 sub rows in each row, but only for some columns. you can use rowspan for this.
you can duplicate your rows (row1-row1-row2-row2-row3-row3-...), and put the subrow values in them (row1_subrow1-row1_subrow2-row2_subrow1-row2_subrow2-...), then use rowspan for the columns you want to expand (like Section and Name in your image), and expand the odd rows and collapse the even rows for this columns.
the full code : (Codesandbox Demo)
import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Table } from "antd";
let multiRowRender = (value, row, index) => {
const obj = {
children: value,
props: {}
};
if (index % 2 === 0) {
obj.props.rowSpan = 2;
}
if (index % 2 === 1) {
obj.props.rowSpan = 0;
}
return obj;
};
const columns = [
{
title: "Number",
dataIndex: "number"
},
{
title: "Issue",
dataIndex: "issue"
},
{
title: "Name",
dataIndex: "name",
render: multiRowRender
},
{
title: "Section",
dataIndex: "section",
render: multiRowRender
}
];
let data = [
{
key: "1",
name: "John Brown",
issues: [32, 100],
numbers: [18889898989, 545054],
section: "sec 1"
},
{
key: "2",
name: "Jim Green",
issues: [42, 50],
numbers: [18889898888, 1420054],
section: "sec 2"
}
];
let data2 = [];
data.forEach(d => {
data2.push({ ...d, issue: d.issues[0], number: d.numbers[0] });
data2.push({
...d,
issue: d.issues[1],
number: d.numbers[1],
key: d.key + "-row2"
});
});
data = data2;
ReactDOM.render(
<Table columns={columns} dataSource={data} bordered />,
document.getElementById("container")
);
Codesandbox Demo

Delete an object inside an object reactjs redux?

I am building a simple application with reactjs, redux; The database like this
const initialState = [
{
title: "List 1",
id: 0,
cards: [
{
id: 0,
text: "Task 1",
},
{
id: 1,
text: "Task 2",
},
],
},
{
title: "List 2",
id: 1,
cards: [
{
id: 0,
text: "Task 3",
},
{
id: 1,
text: "Task 4",
},
],
},
];
The app has many lists. In many list has many cards, I want delete a card in a list
So in my listReducer.js. I created a delete reducer to delete a task like this.
case CONSTANTS.DELETE_CARD: {
const { listId, id } = action.payload;
return state.map((list) => {
if (list.id === listId) {
return {
...list,
cards: list.cards.filter((card) => card.id !== id),
};
}
return list;
});
}
But It not working correctly. What I am doing wrong?
the code: https://codesandbox.io/s/github/htactive-nhaptt/crud-trello?file=/src/reducers/listReducers.js:1466-1773
The issue is not with your reducer. I looked at your codesandbox example and added a few console.log and looks like everything gets updated correctly.
The problem is in the render function of your List component in components/List.js. Because you're not passing a unique key to Card component, React doesn't know what items have changed and it only sees a change in cards list lenght. So on re-render, it renders the old list up to the new length (hope it makes sense!) See here for more info on keys: https://reactjs.org/docs/lists-and-keys.html#keys.
You can fix the issue by passing a unique key to Card like this on line 72:
return <Card key={card.id} id={card.id} listId={listId} text={card.text} />;

TypeError: Failed to construct 'URL': Please use the 'new' operator, this DOM object constructor cannot be called as a function

I have a data.js file where I have this:
export default [
{
id: 1,
title: "Black shirt Friday outfit",
price: 30.99,
image:URL("../images/black-shirt.jpg")
},
{
id:2,
title:"Look at me in brown boots",
price:25.30,
// image: (url("../images/brown-outfit.jpg"))
},
{
id:3,
title:"Cute winter",
price: 21.90,
// image: url("../images/cozy-outfit.jpg"),
},
My question is: What syntax can I use to render those images in my react-app?
For anyone desperately searching for the answer its in error message
you need to type "new" in front of URL so new URL
example below
export default [
{
id: 1,
title: "Black shirt Friday outfit",
price: 30.99,
image: new URL("../images/black-shirt.jpg")
},
You can import them and then reference them:
import Image1 from "../images/black-shirt.jpg"
import Image2 from "../images/brown-outfit.jpg"
import Image3 from "../images/cozy-outfit.jpg"
**export default [
{
id: 1,
title: "Black shirt Friday outfit",
price: 30.99,
image: Image1
},
{
id:2,
title:"Look at me in brown boots",
price:25.30,
image: Image2
},
{
id:3,
title:"Cute winter",
price: 21.90,
image: Image3
},**
Then when you want to use them in a component, you'd refer directly to that imported object as the source:
data.map( item =>
<img src={item.image} />
)
const data = [
{
id: 1,
title: "Black shirt Friday outfit",
price: 30.99,
image:"../images/black-shirt.jpg"
},
{
id:2,
title:"Look at me in brown boots",
price:25.30,
image: "../images/black-shirt.jpg"
},
{
id:3,
title:"Cute winter",
price: 21.90,
image: "../images/black-shirt.jpg"
}
];
export default data;
In your main file:
return {
<div>
<ul>
{data.map(item =><img key={Math.random()} src={require(`../images/${item.image}.jpg`)} />
</ul>
</div>
;
}

React Data Grid shows bad

I have a problem with ReactDataGrid component. I have already installed react-data-grid. The code is the same as in the reac grid's web:
const columns = [
{ key: 'id', name: 'ID' },
{ key: 'title', name: 'Title' },
{ key: 'count', name: 'Count' }];
const rows = [{ id: 0, title: 'row1', count: 20 }, { id: 1, title: 'row1', count: 40 }, { id: 2, title: 'row1', count: 60 }];
class App extends React.Component {
render() {
return (
<ReactDataGrid
columns={columns}
rowGetter={i => rows[i]}
rowsCount={3}
minHeight={150} />
)
}
}
export default App;
and i get:
Result
Thank you!
Import the CSS like so :
import 'react-data-grid/dist/react-data-grid.css';
It should be fine.
import React from "react";
import ReactDOM from "react-dom";
import ReactDataGrid from "react-data-grid";
const columns = [
{ key: "id", name: "ID", editable: true },
{ key: "title", name: "Title", editable: true },
{ key: "count", name: "Count", editable: true }
];
const rows = [
{ id: 0, title: "row1", count: 20 },
{ id: 1, title: "row1", count: 40 },
{ id: 2, title: "row1", count: 60 }
];
class App extends React.Component {
render() {
return (
<ReactDataGrid
columns={columns}
rowGetter={i => rows[i]}
rowsCount={3}
minHeight={150}
enableCellSelect={true}
/>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
https://codesandbox.io/s/rdg-cell-editing-h8bnr
the answer is in the above code this above code is working
You don't really need to downgrade. The issue is that the css is not being imported.
If you can import css from node-modules, it'll work.
Workaround for me was I took the whole css and we are now self-maintaining the css, making changes when needed.
I couldn't load the css either, I got around this by including
import ReactDataGrid from 'react-data-grid/dist/react-data-grid.min.js';
instead of
import ReactDataGrid from 'react-data-grid';

TypeError: _SchoolProduct__WEBPACK_IMPORTED_MODULE_2___default.a.map is not a function

I am new at ReactJs and try to complete a task from the youtube channel.
I created array "products" in "SchoolProduct.js" then using props I passed the value in Product.js.
Now in App.js, I used map function to get data
(Maybe I understand something wrong about props or map function)
Here is SchoolProduct.js:
const products = [
{
id: "1",
name: "pencil",
price: 1,
description: "this is pencil"
},
{
id: "2",
name: "rubber",
price: 10,
description: "this is rubber"
}
]
this is my Product.js
import React from "react"
function Product(props)
{
return
(
<div>
<h2>{props.product.name}</h2>
<p>{props.product.price.toLocaleString("en-US", {style: "currency",
currency: "USD"})}
- {props.product.description}</p>
</div>
)
}
export default Product
and this my App.js
import React, { Component } from 'react';
import Product from "./Product"
import productsData from "./SchoolProduct"
function App(){
const productsComponents = productsData.map(item => <Product product=
{item}/>)
return (
<div>
{productsComponents}
</div>
)
}
export default App;
The Error is:
TypeError: _SchoolProduct__WEBPACK_IMPORTED_MODULE_2___default.a.map is not a function
its shows error in App.js line no 8, which is "const productsComponents"
I know I create a silly mistake, but I am trying to improve it
I have to export my error in default way,
const products = [
{
id: "1",
name: "pencil",
price: 1,
description: "this is pencil"
},
{
id: "2",
name: "rubber",
price: 10,
description: "this is rubber"
}
]
export default products
export default [
{
id: "1",
name: "Pencil",
price: 1,
description: "Perfect for those who can't remember things! 5/5 Highly recommend."
},
{
id: "2",
name: "Housing",
price: 0,
description: "Housing provided for out-of-state students or those who can't commute"
},
{
id: "3",
name: "Computer Rental",
price: 300,
description: "Don't have a computer? No problem!"
},
{
id: "4",
name: "Coffee",
price: 2,
description: "Wake up!"
},
{
id: "5",
name: "Snacks",
price: 0,
description: "Free snacks!"
},
{
id: "6",
name: "Rubber Duckies",
price: 3.50,
description: "To help you solve your hardest coding problems."
},
{
id: "7",
name: "Fidget Spinner",
price: 21.99,
description: "Because we like to pretend we're in high school."
},
{
id: "8",
name: "Sticker Set",
price: 14.99,
description: "To prove to other devs you know a lot."
}
]

Resources