@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
231 lines (229 loc) • 8.62 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = require("react");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _isEqual = _interopRequireDefault(require("lodash/isEqual"));
var _uiHeading = require("@instructure/ui-heading");
var _emotion = require("@instructure/emotion");
var _Edit = _interopRequireDefault(require("../Edit"));
var _Show = _interopRequireDefault(require("../Show"));
var _Take = _interopRequireDefault(require("../Take"));
var _Result = _interopRequireDefault(require("../Result"));
var _default = _interopRequireDefault(require("../../common/example/default"));
var _defaultTheme = _interopRequireDefault(require("../../common/example/defaultTheme"));
var _quizCommon = require("@instructure/quiz-common");
var _dec, _class, _MatchingExample;
/** @jsx jsx */
/* eslint-disable react/jsx-no-literals */
function _callSuper(_this, derived, args) {
function isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
return !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
} catch (e) {
return false;
}
}
derived = (0, _getPrototypeOf2["default"])(derived);
return (0, _possibleConstructorReturn2["default"])(_this, isNativeReflectConstruct() ? Reflect.construct(derived, args || [], (0, _getPrototypeOf2["default"])(_this).constructor) : derived.apply(_this, args));
}
/**
---
category: Matching
---
Matching Example.
```jsx_example
<SettingsSwitcher locales={LOCALES}>
<MatchingExample />
</SettingsSwitcher>
```
**/
var MatchingExample = exports["default"] = (_dec = (0, _quizCommon.withStyleOverrides)(_default["default"], _defaultTheme["default"]), _dec(_class = (_MatchingExample = /*#__PURE__*/function (_Component) {
function MatchingExample(props) {
var _this2;
(0, _classCallCheck2["default"])(this, MatchingExample);
_this2 = _callSuper(this, MatchingExample);
(0, _defineProperty2["default"])(_this2, "handleChangeItemState", function (itemState) {
var newState = Object.assign({}, itemState, itemState.scoringData ? {
scoredData: _this2.transformScoredData(itemState.scoringData)
} : null);
_this2.setState(newState);
});
(0, _defineProperty2["default"])(_this2, "handleResponseUpdate", function (userResponse) {
_this2.setState({
userResponse: {
value: userResponse
},
scoredData: _this2.transformScoredData(_this2.state.scoringData)
});
});
_this2.state = {};
if (props.useDefaultData) {
Object.assign(_this2.state, {
interactionData: {
questions: [{
id: 'uuid1',
itemBody: 'Condi Rice'
}, {
id: 'uuid2',
itemBody: 'Alexander Haig'
}, {
id: 'uuid3',
itemBody: 'John Kerry'
}],
answers: ['George W. Bush', 'Ronald Reagan', 'Barack Obama', 'George H.W. Bush', 'Bill Clinton']
},
itemBody: 'Match the Secretary of State with the President they served under.',
properties: {
shuffleRules: {
questions: {
shuffled: true
}
}
},
scoringData: {
value: {
uuid1: 'George W. Bush',
uuid2: 'Ronald Reagan',
uuid3: 'Barack Obama'
}
},
scoredData: {
value: [{
id: 'uuid1',
userResponded: 'Condi Rice',
value: 'Condi Rice'
}, {
id: 'uuid2',
userResponded: 'Alexander Haig',
value: 'Alexander Haig'
}, {
id: 'uuid3',
userResponded: 'John Kerry',
value: 'John Kerry'
}]
},
userResponse: {
value: {}
}
});
}
return _this2;
}
(0, _inherits2["default"])(MatchingExample, _Component);
return (0, _createClass2["default"])(MatchingExample, [{
key: "componentDidMount",
value: function componentDidMount() {
this.setState({
scoredData: this.transformScoredData(this.state.scoringData)
});
}
}, {
key: "transformScoredData",
value: function transformScoredData(scoringData) {
var _this3 = this;
return {
correct: (0, _isEqual["default"])(this.state.userResponse.value, scoringData.value),
value: Object.keys(scoringData.value).map(function (currKey) {
return {
id: currKey,
userResponded: _this3.state.userResponse.value[currKey],
resultScore: Object.keys(scoringData.value).includes(currKey) ? 1 : 0,
value: scoringData.value[currKey]
};
})
};
}
}, {
key: "renderEdit",
value: function renderEdit() {
return (0, _emotion.jsx)("div", {
title: "edit"
}, (0, _emotion.jsx)(_uiHeading.Heading, {
level: "h2"
}, "Edit"), (0, _emotion.jsx)(_Edit["default"], {
changeItemState: this.handleChangeItemState,
interactionData: this.state.interactionData,
itemBody: this.state.itemBody,
scoringData: this.state.scoringData,
properties: this.state.properties,
errors: {}
}));
}
}, {
key: "renderShow",
value: function renderShow() {
return (0, _emotion.jsx)("div", {
title: "show"
}, (0, _emotion.jsx)(_uiHeading.Heading, {
level: "h2"
}, "Show"), (0, _emotion.jsx)(_Show["default"], {
interactionData: this.state.interactionData,
itemBody: this.state.itemBody,
scoringData: this.state.scoringData
}));
}
}, {
key: "renderTake",
value: function renderTake() {
return (0, _emotion.jsx)("div", {
title: "take"
}, (0, _emotion.jsx)(_uiHeading.Heading, {
level: "h2"
}, "Take"), (0, _emotion.jsx)(_Take["default"], {
interactionData: this.state.interactionData,
itemBody: this.state.itemBody,
userResponse: this.state.userResponse,
handleResponseUpdate: this.handleResponseUpdate
}));
}
}, {
key: "renderResult",
value: function renderResult() {
return (0, _emotion.jsx)("div", {
title: "result"
}, (0, _emotion.jsx)(_uiHeading.Heading, {
level: "h2"
}, "Result"), (0, _emotion.jsx)(_Result["default"], {
interactionData: this.state.interactionData,
itemBody: this.state.itemBody,
userResponse: this.state.userResponse,
scoredData: this.state.scoredData
}));
}
}, {
key: "render",
value: function render() {
return (0, _emotion.jsx)("div", null, (0, _emotion.jsx)("div", {
css: this.props.styles.row
}, (0, _emotion.jsx)("div", {
css: this.props.styles.panel
}, this.renderEdit()), (0, _emotion.jsx)("div", {
css: this.props.styles.panel
}, this.renderShow())), (0, _emotion.jsx)("div", {
css: this.props.styles.row
}, (0, _emotion.jsx)("div", {
css: this.props.styles.panel
}, this.renderTake()), (0, _emotion.jsx)("div", {
css: this.props.styles.panel
}, this.renderResult())));
}
}]);
}(_react.Component), (0, _defineProperty2["default"])(_MatchingExample, "displayName", 'MatchingExample'), (0, _defineProperty2["default"])(_MatchingExample, "componentId", "Quizzes".concat(_MatchingExample.displayName)), (0, _defineProperty2["default"])(_MatchingExample, "propTypes", {
useDefaultData: _propTypes["default"].bool,
styles: _propTypes["default"].object
}), (0, _defineProperty2["default"])(_MatchingExample, "defaultProps", {
useDefaultData: true
}), _MatchingExample)) || _class);