Boilerplate for custom surveys in React and Typescript with either a simple table based survey or complex any-question surveys.
npm i
npm start
# open: localhost:4044
npm run build
npm serve
# open: localhost:4046
To use in own libraries install dependencies and copy the src folder into your project:
npm i --save clsx immutable react-uid
# only used in SurveyGen:
npm i --save react-animate-height
Get started by checking out SurveyGen and SurveyTable, also easy with starting the repo.
- Survey Base, used in the implementations
- Question Handler, basic question definition
- SurveyGen, complex any-question survey
- SurveyTable, survey in a table
The files in src/Survey are the base files for rendering and storing the questions and answers. Used in the actual survey implementations.
createSurvey()
creates the immutable recordSurveyStore
SurveyStore
holds the state and state updater/answersSurveyProvider
with props:store: SurveyStore
the returned value ofcreateSurvey()
store.values
the immutable OrderedMap which stores the answersstore.values.toJS()
to convert back to JS-object- Note: if your app requires high performance, you may need to avoid toJS()
store.onChange(id, value)
to update with a hard coded valuestore.onChange(id, oldvalue => { return newvalue })
to get the old value from the onchange handler (recommended)store.getValue(id)
to get the value of a single answer
types: {}
, the types available for questionsquestions: []
, all questions for this survey
useSurvey()
hook to get the store, types and questions
QuestionRenderer
, the universal renderer for one questiontype
determines the handler for the questionshowIf
basic "show when that" logic handlerWrapper
component that should wrap any component, like theshowIf
handler inSurveyGen
- and any other prop that the actual question handler should receive
Two ready-to-use survey implementations are included in the boilerplate: SurveyGen and SurveyTable. They handle/show the survey in different ways and support different question types through multiple question handler.
With the base components you can create any own handler, take a look in src/SurveyGen/SurveyGen and src/SurveyTable/SurveyTable for how this can be done.
Each implementation needs it's question type handlers, the basic data/properties are defined in the QuestionProps interface.
An minimal question in JSON:
{
"id": "fav_num_select2",
"label": "How many children do you have?",
"type": "number"
}
See src/DemoTable.tsx for a basic question schema and basic setup using portals.
HandlerRadio
HandlerYesNo
See src/DemoGen.tsx for a basic question schema and basic setup using portals.
HandlerCheck
showing multiple checkboxes, saving selected as an array- additional properties
values?: string[]
defines the checkboxes/values
- additional properties
HandlerNumber
- additional properties
min?: number
max?: number
showMoreThen?: boolean
- additional properties
HandlerRadio
- additional properties
values?: string[]
defines the checkboxes/values
- additional properties
HandlerRating
basic handler number rating, can be used to display anything else as rating, like icons- additional properties
min: number
the start number for the ratingcount: number
how many numbers can be ratedCompActive
react componentCompStandard
react component
- additional properties
HandlerRatingEmoji
- additional properties
min: number
the start number for the ratingcount: number
how many numbers can be rated
- additional properties
HandlerRatingHeart
- additional properties
min: number
the start number for the ratingcount: number
how many numbers can be rated
- additional properties
HandlerRatingStar
- additional properties
min: number
the start number for the ratingcount: number
how many numbers can be rated
- additional properties
HandlerString
- additional properties
multiline
will render atextarea
- additional properties
This project is free software distributed under the MIT License.
See: LICENSE.
© 2020 Michael Becker
The icons used in src/icons are Material UI icons under the Apache-2.0 License.
By committing your code/creating a pull request to this repository you agree to release the code under the MIT License attached to the repository.