@e-group/material-module
Version:
EGroup Team react component modules.
425 lines (350 loc) • 13.4 kB
JavaScript
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _uuid = require("uuid");
var _reactBeautifulDnd = require("react-beautiful-dnd");
var _Question = _interopRequireDefault(require("./Question"));
var _QuestionEditable = _interopRequireDefault(require("./QuestionEditable"));
var _SurveyContext = _interopRequireDefault(require("../Survey/SurveyContext"));
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
const SurveyQuestions = ({
onDragEnd,
droppableId,
optionTypes,
onNameChange,
onTypeChange,
onDescChange,
onRequiredChange,
onDelete,
onOptionChange,
onOptionDelete,
onOptionDragEnd,
onOptionCreate,
onOptionCreateOther,
onRatingStartChange,
onRatingEndChange,
onRatingStartInputChange,
onRatingEndInputChange
}) => {
const _useContext = (0, _react.useContext)(_SurveyContext.default),
questions = _useContext.questions,
setQuestions = _useContext.setQuestions,
selectedQuestionId = _useContext.selectedQuestionId,
setSelectedQuestionId = _useContext.setSelectedQuestionId;
const _useState = (0, _react.useState)(0),
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
autoFocusIndex = _useState2[0],
setAutoFocusIndex = _useState2[1];
const _useState3 = (0, _react.useState)(false),
_useState4 = (0, _slicedToArray2.default)(_useState3, 2),
autoFocusName = _useState4[0],
setAutoFocusName = _useState4[1];
const handleDragEnd = (result, provided) => {
const destination = result.destination,
source = result.source;
if (!destination) {
return;
}
if (destination.droppableId === source.droppableId && destination.index === source.index) {
return;
}
if (!questions) return;
const sourceQuestion = questions[source.index];
if (!sourceQuestion) return;
const nextQuestions = [...questions];
nextQuestions.splice(source.index, 1);
nextQuestions.splice(destination.index, 0, sourceQuestion);
setQuestions(nextQuestions);
if (onDragEnd) {
onDragEnd(result, provided, nextQuestions);
}
};
const handleNameChange = (event, question) => {
const nextQuestions = questions.map(el => {
if (el.questionId === question.questionId) {
return _objectSpread(_objectSpread({}, el), {}, {
questionName: event.target.value
});
}
return el;
});
setQuestions(nextQuestions);
if (onNameChange) {
onNameChange(event, question);
}
};
const handleTypeChange = (event, question) => {
const nextQuestionType = event.target.value;
let questionRatingEndValue;
let questionRatingStartValue;
const shouldCreateOneOption = (nextQuestionType === 'choicemulti' || nextQuestionType === 'choiceone' || nextQuestionType === 'select') && (!question.optionList || question.optionList.length === 0);
if (nextQuestionType === 'rating') {
questionRatingStartValue = 1;
questionRatingEndValue = 5;
}
let nextQuestion = _objectSpread({}, question);
let createdOption;
const nextQuestions = questions.map(el => {
if (el.questionId === question.questionId) {
createdOption = {
optionId: (0, _uuid.v4)(),
optionName: '選項 1',
isOther: 0
};
const result = _objectSpread(_objectSpread({}, el), {}, {
questionType: nextQuestionType,
optionList: shouldCreateOneOption ? [createdOption] : el.optionList,
questionRatingEndValue,
questionRatingStartValue
});
nextQuestion = _objectSpread({}, result);
return result;
}
return el;
});
setQuestions(nextQuestions);
if (onTypeChange) {
onTypeChange(event, question, nextQuestion, shouldCreateOneOption ? createdOption : undefined);
}
};
const handleDescChange = (event, question) => {
const nextQuestions = questions.map(el => {
if (el.questionId === question.questionId) {
return _objectSpread(_objectSpread({}, el), {}, {
questionDescription: event.target.value
});
}
return el;
});
setQuestions(nextQuestions);
if (onDescChange) {
onDescChange(event, question);
}
};
const handleIsRequiredChange = (event, question) => {
const nextQuestions = questions.map(el => {
if (el.questionId === question.questionId) {
return _objectSpread(_objectSpread({}, el), {}, {
isRequired: Number(event.target.checked)
});
}
return el;
});
setQuestions(nextQuestions);
if (onRequiredChange) {
onRequiredChange(event, question);
}
};
const handleDelete = (event, question) => {
const nextQuestions = questions.filter(el => el.questionId !== question.questionId);
setQuestions(nextQuestions);
if (onDelete) {
onDelete(event, question);
}
};
const handleOptionChange = (event, question, option) => {
let nextOption = _objectSpread({}, option);
const nextQuestions = questions.map(el => {
if (el.questionId === question.questionId) {
var _el$optionList;
return _objectSpread(_objectSpread({}, el), {}, {
optionList: (_el$optionList = el.optionList) === null || _el$optionList === void 0 ? void 0 : _el$optionList.map(opt => {
if (opt.optionId === option.optionId) {
const result = _objectSpread(_objectSpread({}, opt), {}, {
optionName: event.target.value
});
nextOption = _objectSpread({}, result);
return result;
}
return opt;
})
});
}
return el;
});
setQuestions(nextQuestions);
if (onOptionChange) {
onOptionChange(event, question, option, nextOption);
}
};
const handleOptionDelete = (event, question, deletedOption) => {
const nextQuestions = questions.map(el => {
if (el.questionId === question.questionId) {
var _el$optionList2;
return _objectSpread(_objectSpread({}, el), {}, {
optionList: (_el$optionList2 = el.optionList) === null || _el$optionList2 === void 0 ? void 0 : _el$optionList2.filter(opt => opt.optionId !== deletedOption.optionId)
});
}
return el;
});
setQuestions(nextQuestions);
if (onOptionDelete) {
onOptionDelete(event, question, deletedOption);
}
};
const handleOptionDragEnd = (result, question, options) => {
const nextQuestions = questions.map(el => {
if (el.questionId === question.questionId) {
return _objectSpread(_objectSpread({}, el), {}, {
optionList: options
});
}
return el;
});
setQuestions(nextQuestions);
if (onOptionDragEnd) {
onOptionDragEnd(result, question, options);
}
};
const handleOptionCreate = (e, question, index) => {
let createdOption;
const nextQuestions = questions.map(el => {
if (el.questionId === question.questionId && el.optionList) {
createdOption = {
optionId: (0, _uuid.v4)(),
optionName: "\u9078\u9805 ".concat(index),
isOther: 0
};
return _objectSpread(_objectSpread({}, el), {}, {
optionList: [...el.optionList, createdOption]
});
}
return el;
});
setQuestions(nextQuestions);
if (onOptionCreate) {
onOptionCreate(e, question, index, createdOption);
}
};
const handleOptionCreateOther = (e, question) => {
let createdOption;
const nextQuestions = questions.map(el => {
if (el.questionId === question.questionId && el.optionList) {
createdOption = {
optionId: (0, _uuid.v4)(),
optionName: '其他...',
isOther: 1
};
return _objectSpread(_objectSpread({}, el), {}, {
optionList: [...el.optionList, createdOption]
});
}
return el;
});
setQuestions(nextQuestions);
if (onOptionCreateOther) {
onOptionCreateOther(e, question, createdOption);
}
};
const handleRatingStartChange = (e, question) => {
const nextQuestions = questions.map(el => {
if (el.questionId === question.questionId) {
return _objectSpread(_objectSpread({}, el), {}, {
questionRatingStartValue: e.target.value
});
}
return el;
});
setQuestions(nextQuestions);
if (onRatingStartChange) {
onRatingStartChange(e, question);
}
};
const handleRatingEndChange = (e, question) => {
const nextQuestions = questions.map(el => {
if (el.questionId === question.questionId) {
return _objectSpread(_objectSpread({}, el), {}, {
questionRatingEndValue: e.target.value
});
}
return el;
});
setQuestions(nextQuestions);
if (onRatingEndChange) {
onRatingEndChange(e, question);
}
};
const handleRatingStartInputChange = (e, question) => {
const nextQuestions = questions.map(el => {
if (el.questionId === question.questionId) {
return _objectSpread(_objectSpread({}, el), {}, {
questionRatingStartName: e.target.value
});
}
return el;
});
setQuestions(nextQuestions);
if (onRatingStartInputChange) {
onRatingStartInputChange(e, question);
}
};
const handleRatingEndInputChange = (e, question) => {
const nextQuestions = questions.map(el => {
if (el.questionId === question.questionId) {
return _objectSpread(_objectSpread({}, el), {}, {
questionRatingEndName: e.target.value
});
}
return el;
});
setQuestions(nextQuestions);
if (onRatingEndInputChange) {
onRatingEndInputChange(e, question);
}
};
return /*#__PURE__*/_react.default.createElement(_reactBeautifulDnd.DragDropContext, {
onDragEnd: handleDragEnd
}, /*#__PURE__*/_react.default.createElement(_reactBeautifulDnd.Droppable, {
droppableId: droppableId
}, dropProvided => /*#__PURE__*/_react.default.createElement("div", dropProvided.droppableProps, /*#__PURE__*/_react.default.createElement("div", {
ref: dropProvided.innerRef
}, questions.map((question, index) => /*#__PURE__*/_react.default.createElement(_reactBeautifulDnd.Draggable, {
draggableId: question.questionId,
key: question.questionId,
index: index
}, dragProvided => /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
ref: dragProvided.innerRef
}, dragProvided.draggableProps), question.questionId === selectedQuestionId ? /*#__PURE__*/_react.default.createElement(_QuestionEditable.default, {
DragHandleProps: dragProvided.dragHandleProps,
question: question,
onNameChange: handleNameChange,
onTypeChange: handleTypeChange,
onDescChange: handleDescChange,
onRequiredChange: handleIsRequiredChange,
onDelete: handleDelete,
onOptionChange: handleOptionChange,
onOptionDelete: handleOptionDelete,
onOptionDragEnd: handleOptionDragEnd,
onOptionCreate: handleOptionCreate,
onOptionCreateOther: handleOptionCreateOther,
onRatingStartChange: handleRatingStartChange,
onRatingEndChange: handleRatingEndChange,
onRatingStartInputChange: handleRatingStartInputChange,
onRatingEndInputChange: handleRatingEndInputChange,
autoFocusOptionIndex: autoFocusIndex,
autoFocusName: autoFocusName,
optionTypes: optionTypes
}) : /*#__PURE__*/_react.default.createElement(_Question.default, {
DragHandleProps: dragProvided.dragHandleProps,
onClick: () => setSelectedQuestionId(question.questionId),
question: question,
onOptionClick: (el, opt, index) => {
setAutoFocusName(false);
setAutoFocusIndex(index);
},
onQuestionNameClick: () => {
setAutoFocusName(true);
}
})))), dropProvided.placeholder))));
};
var _default = SurveyQuestions;
exports.default = _default;