Question T11861
Visible to All Users

Issue with nouislider

created 2 years ago

when I am creating new slider and comes back its gone,

JavaScript
import React, { useEffect, useContext, Fragment, useState } from 'react'; import $ from 'jquery'; import { SurveyCreatorComponent, SurveyCreator } from 'survey-creator-react'; import { Serializer } from 'survey-core'; import * as Survey from 'survey-core'; import * as widgets from 'surveyjs-widgets'; import 'survey-core/defaultV2.css'; import 'survey-creator-core/survey-creator-core.min.css'; import MediaAssetManager, { getUploadBucket, composeImagesPaths, getFolderPrefix, } from 'components/MediaAssetManager'; import { joinUrlParts } from 'utilities/urls'; import { ServiceProviderUserDataContext } from 'components/ServiceProviderUserData'; import { allowedActions } from 'constants/roles'; import { SurveyQuestionEditorDefinition } from "survey-creator-core"; import { toolboxItems } from './define-custom-questions'; widgets.nouislider(Survey,$); Serializer.addProperty('question', { name: 'csvLabel', displayName: 'Label (for reports)', category: 'general', visibleIndex: 20, isRequired: false, }); Serializer.addProperty('question', { name: 'dataInput', category: 'general', }); Serializer.addProperty('question', { name: 'dataType', category: 'general', }); Serializer.addProperty('dropdown', { name: 'inputType', category: 'general', }); Serializer.addProperty('radiogroup', { name: 'inputType', category: 'general', }); Serializer.addProperty('boolean', { name: 'inputType', category: 'general', }); Serializer.addProperty('question', { name: 'dataUpdate', category: 'general', choices: [ { value: 'Living Area', text: 'Living Area' } ], }); Serializer.addProperty('question', { name: 'rangeMin:number', category: 'general', }); Serializer.addProperty('question', { name: 'rangeMax:number', category: 'general', }); Serializer.addProperty('question', { name: 'step:number', category: 'general', }); Serializer.addProperty('checkbox', { name: 'minCount:number', displayName: 'Minimum Answer Count', category: 'validation', }); Serializer.addProperty('checkbox', { name: 'maxCount:number', displayName: 'Maximum Answer Count', category: 'validation', }); Serializer.findProperty('text', 'inputType').setChoices([ 'text', 'number', 'date', 'email', 'boolean', ]); SurveyQuestionEditorDefinition.definition.question.properties = [ {name: 'csvLabel:text', category: 'general'}] Serializer.findProperty('text', 'maxLength').visible = false; Serializer.findProperty('question', 'validators').visible = false; Serializer.findProperty('dropdown', 'inputType').visible = false; Serializer.findProperty('radiogroup', 'inputType').visible = false; Serializer.findProperty('boolean', 'inputType').visible = false; const NewSurveyCreator = ({ setSurveyQuestions, surveyQuestions, surveyType, baseURL, editMode, }) => { const [creator, setSurveyCreator] = useState(null); const [isPickingImage, setIsPickingImage] = useState(false); const { currentUserCan, serviceProviderId, displayName } = useContext( ServiceProviderUserDataContext ); useEffect(() => { const creatorOptions = { showJSONEditorTab: false, showLogicTab: false, isAutoSave: true, haveCommercialLicense: true, readOnly: editMode !== 'full' && editMode !== 'partial', showDesignerTab: editMode === 'full' || editMode === 'partial', showSurveyTitle: false, questionTypes: [ 'checkbox', 'text', 'radiogroup', 'dropdown', 'comment', 'rating', 'boolean', 'imagepicker', // 'multipletext', // 'expression', ], }; const creator = new SurveyCreator(creatorOptions); creator.toolbox.addItem({ isCopied: false, iconName: 'icon-nouislider', title: 'Data Update - Slider', category: 'Data Update', json: { type: 'nouislider', dataType: 'data', isRequired: true, dataInput: true, ...toolboxItems.nouislider.dataUpdateFields[0], }, }); /* creator.toolbox.addItem({ isCopied: false, iconName: 'icon-text', title: 'Data Update - Text Input', category: 'Data Update', json: { type: 'text', dataType: 'data', isRequired: true, dataInput: true, ...toolboxItems.text.dataUpdateFields[0], }, }); */ creator.toolbox.removeItem('nouislider'); creator.onQuestionAdded.add(function (sender, options) { const q = options.question; if (q.dataInput) { return; } q.dataType = 'survey'; q.dataInput = false; q.title = 'Change the question title in the "Settings" section.'; q.csvLabel = q.name; if (q.getType() === 'comment') { q.maxLength = 256; } }); creator.onSurveyInstanceCreated.add(function (sender, options) { if (options.reason === 'designer') { options.survey.onShowingChoiceItem.add((sender, options) => { const q = options.question; if (q.dataInput) { if ( options.item === options.question.otherItem || options.item === options.question.noneItem || options.item === options.question.newItem ) { options.visible = false; } } }); } }); creator.onSetPropertyEditorOptions.add(function (editor, options) { if ( options.obj && options.obj.getType() === 'imagepicker' && options.propertyName === 'choices' ) { options.editorOptions.showTextView = false; } }); creator.onElementAllowOperations.add((editor, options) => { if (!options.obj || !options.obj.page) return; options.allowChangeType = true; if (options.obj.dataInput) { options.allowEdit = true; options.allowDelete = true; options.allowCopy = false; options.allowAddToToolbox = false; options.allowDragging = true; options.allowChangeRequired = true; options.allowShowHideTitle = false; } }); Serializer.findProperty('question', 'description').displayName = 'Instructions'; creator.onShowingProperty.add(function (sender, options) { const question = options.obj; const allowedProperties = question.dataInput ? [ 'title', 'description', 'csvLabel', 'inputType', 'dataUpdate', 'rangeMin', 'rangeMax', 'step', 'isRequired', ] : [ 'title', 'choices', 'description', 'csvLabel', 'isRequired', 'inputType', 'min', 'max', 'expression', 'validators', 'minCount', 'maxCount', 'rateStep', 'labelTrue', 'labelFalse', 'maxLength', 'showLabel', 'multiSelect', 'rateValues', 'rateMin', 'rateMax', 'minRateDescription', 'maxRateDescription', 'correctAnswer', 'defaultValue', ]; if ( options.obj.getType() === 'survey' || options.obj.getType() === 'page' ) { options.canShow = [].indexOf(options.property.name) > -1; } else { options.canShow = allowedProperties.indexOf(options.property.name) > -1; } }); creator.onSurveyPropertyValueChanged.add((editor, options) => { if ( options.obj && toolboxItems[options.obj.getType()] && options.propertyName === 'dataUpdate' ) { const newObj = toolboxItems[ options.obj.getType() ].dataUpdateFields.find( ({ dataUpdate }) => dataUpdate === options.value ); if (newObj) { Object.keys(newObj).forEach((key) => { options.obj[key] = newObj[key]; }); } } }); creator.onModified.add((editor, options) => { if (options.type === 'QUESTION_CONVERTED') { const newQuestion = options.newValue; if ( newQuestion.getType().indexOf('data-update') === -1 && newQuestion.dataInput ) { newQuestion.dataInput = false; } if (newQuestion.getType().indexOf('data-update') > -1) { const newObj = toolboxItems[newQuestion.getType()].dataUpdateFields[0]; if (newObj) { Object.keys(newObj).forEach((key) => { newQuestion[key] = newObj[key]; if (key === 'choices') { newQuestion.questionWrapper.choices = newObj[key]; } }); } } } }); const saveMySurvey = () => { const jsonOutput = JSON.parse(creator.text); jsonOutput.deliveryType = surveyType; jsonOutput.dismiss = true; const toPersist = { ...jsonOutput, pages: jsonOutput.pages.map((page) => ({ ...page, elements: page.elements.map((element) => { return { ...element, type: element.type.replace('data-update-', ''), }; }), })), }; setSurveyQuestions(toPersist); }; creator.saveSurveyFunc = saveMySurvey; creator.onOpenFileChooser.add((sender, options) => { setIsPickingImage(options); }); creator.onUploadFile.add((sender, options) => { options.callback('success', options.files); }); creator.text = JSON.stringify({ ...surveyQuestions, pages: surveyQuestions.pages.map((page) => ({ ...page, elements: page.elements.map((element) => { return { ...element, type: element.dataInput && element.type.indexOf('data-update') === -1 ? 'data-update-' + element.type : element.type, }; }), })), }); setSurveyCreator(creator); }, []); return ( <Fragment> {creator && <SurveyCreatorComponent creator={creator} />} <MediaAssetManager isOpen={isPickingImage} onClose={() => setIsPickingImage(false)} onItemSelect={(itemKey) => { if (!baseURL) { return; } if (!isPickingImage.callback) { return; } isPickingImage.callback(joinUrlParts(baseURL, itemKey)); }} uploadBucket={getUploadBucket()} upload={currentUserCan(allowedActions.UPLOAD_ASSETS)} select images={composeImagesPaths([ { category: 'survey', thumbnail: getFolderPrefix({ path: 'survey' }), fullsize: getFolderPrefix({ path: 'survey' }), displayName: 'Default', }, { category: 'survey', thumbnail: getFolderPrefix({ provider: serviceProviderId, path: 'survey', }), fullsize: getFolderPrefix({ provider: serviceProviderId, path: 'survey', }), displayName, upload: true, active: true, }, ])} /> </Fragment> ); }; export default NewSurveyCreator;

Answers approved by surveyjs Support

created 2 years ago

Hello Jang,
Please refer to the following example which demonstrates how to integrate a NoUISlider widget to Survey Creator: example.

Let me know if it helps.