Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-mcqview

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-mcqview

A customizable multiple choice question react component that is based on the superflows design language.

  • 1.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

react-mcqview

A customizable multiple choice question react component that is based on the superflows design language.

NPM JavaScript Style Guide

Install

npm install --save react-mcqview

Install the dependencies.

Dependencies

npm install --save bootstrap
npm install --save react-bootstrap
npm install --save react-bootstrap-icons
npm install --save react-ui-components-superflows

Functionality

This package allows you to easily create customizable multiple choice question forms in React. You can either choose a single question component, which is called McqView, or a complete form McqForm. This package also allows conditional rendering, wherein certain questions are displayed based on the answer of previous questions.

Components

McqView - a multiple choice question component

Props

  • question: text of the question
  • answers: array of answers, every answer needs to have text, id
  • multiSelect: allow selecting multiple answers
  • selectedValue: array of answer ids, used to prepopulate answers
  • setValue: function to be called when value is set
  • onChange: function to be called when value is changed

Demo

Demo

Usage


import React from 'react';
import { useState, useRef } from 'react';
import { McqView } from 'react-mcqview';
import 'bootstrap/dist/css/bootstrap.min.css';

const App = () => {
  // Stores the stringified representation
  const [value, setValue] = useState('');

  function onChange(value) {
    console.log('value changed', value);
  }

  return (
    <McqView
      question="Do you love fried noodles?"
      answers={[
        { answer: 'Yes', id: 1 },
        { answer: 'No', id: 2 },
        { answer: "Can't Say", id: 3 },
      ]}
      multiSelect={true}
      selectedValue={[1, 2]}
      setValue={setValue}
      onChange={onChange}
    />
  );
};

export default App;


McqForm - a multiple choice question form component

Props

  • formData: data of questions in json array format
  • onComplete: callback after the form is completely filled
  • showSubmit: show submit button
  • onSubmit: callback after submit button clicked (applicable on when showSubmit is set as true)

Formdata

A sample formdata is show below in the usage section. Form data is an array of question data. Each question data contains the following

  • id of the question (you will have to specify that)
  • text of the questions
  • possible list of answers to the question, each answer needs to have its own id
  • a flag to allow selecting multiple answers for that question
  • display condition, if the question depends on some answer(s) of any of the previous questions

Demo

Demo

Usage


import React from 'react'
import { useState, useRef } from "react";
import { Col, Row, Container } from 'react-bootstrap';
import { McqForm } from 'react-mcqview'
import 'bootstrap/dist/css/bootstrap.min.css';

const App = () => {

  // Stores the stringified representation
  const [value, setValue] = useState('');

  function onSubmit(values) {
    console.log('form submitted changed', values);
  }

  const data = [
    {
      id: 1,
      question: "Do you love fried noodles?",
      answers: [
        {answer: "Yes", id: 1},
        {answer: "No", id: 2},
        {answer: "Can't Say", id: 3}
      ],
      multiSelect: true
    },
    {
      id: 2,
      condition: {
        question: 1, answer: [2]
      },
      question: "Okay, then do you love fried rice?",
      answers: [
        {answer: "Yes", id: 1},
        {answer: "No", id: 2},
        {answer: "Can't Say", id: 3}
      ],
      multiSelect: true
    },
    {
      id: 3,
      condition: {
        question: 2, answer: [2]
      },
      question: "What about chicken soup?",
      answers: [
        {answer: "I love it!", id: 1},
        {answer: "Next, please", id: 2},
      ],
      multiSelect: true
    }
  
  ]

  return  ( 
  
    <Container className='mt-5'>
      <Row className='justify-content-center'>
        <Col sm={12} xs={12} md={6} xxl={6}>
          <McqForm 
            formData={data}
            onComplete={(result) => {console.log('result', result);}}
            showSubmit={true}
            onSubmit={(result) => {console.log('onsubmit result', result);}} />
        </Col>
      </Row>
    </Container>

  )
  
}

export default App

Tests

PASS src/index.test.js (15.952s)

  • ✓ McqView: Render of McqView without preselected values (39ms)
  • ✓ McqView: Render of McqView with preselected values (7ms)
  • ✓ McqView: Select answer (multiselect) (1019ms)
  • ✓ McqView: Select answer (singleselect) (2017ms)
  • ✓ McqView: Unselect answer 1 (multiselect) (2017ms)
  • ✓ McqView: Unselect answer 2 (multiselect) (2013ms)
  • ✓ McqView: Unselect answers 1 (singleselect) (2018ms)
  • ✓ McqView: Unselect answers 1 (singleselect) (2019ms)
  • ✓ McqForm: Basic Render (11ms)
  • ✓ McqForm: Complete without conditionals (1021ms)
  • ✓ McqForm: Render conditionals (2037ms)

----------------|----------|----------|----------|----------|-------------------|

File% Stmts% Branch% Funcs% LinesUncovered Line #s
All files93.8686.499293.2
src0000
index.js0000
src/components93.8686.499293.2
McqForm.js91.5586.3687.590.77... 46,147,148,206
McqView.js97.6786.6710097.3782
---------------------------------------------------------------------------
Test Suites: 1 passed, 1 total
Tests: 11 passed, 11 total
Snapshots: 0 total
Time: 16.743s
Ran all test suites.

License

MIT © superflows-dev

Keywords

FAQs

Package last updated on 06 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc