@pantheon-systems/design-toolkit-react
Version:
Pantheon's React Design Toolkit
308 lines (297 loc) • 11 kB
JavaScript
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
import _createClass from '@babel/runtime/helpers/createClass';
import { useEffect } from 'react';
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var makeDefaultGate = function makeDefaultGate(id, label) {
return {
id: id,
isComplete: false,
label: label
};
};
var makeDefaultStep = function makeDefaultStep(id) {
return {
id: id,
isComplete: false
};
};
var StepGateController = /*#__PURE__*/function () {
function StepGateController(setState) {
_classCallCheck(this, StepGateController);
this.setState = setState;
/* Gate API Functionss */
this.getStepApi = this.getStepApi.bind(this);
this.registerGate = this.registerGate.bind(this);
/* Step API Functions */
this.goBackward = this.goBackward.bind(this);
this.goForward = this.goForward.bind(this);
this.goToGate = this.goToGate.bind(this);
this.registerStep = this.registerStep.bind(this);
this.setError = this.setError.bind(this);
}
_createClass(StepGateController, [{
key: "getGateApi",
value: function getGateApi() {
return {
getStepApi: this.getStepApi,
goToGate: this.goToGate,
registerGate: this.registerGate,
unregisterGate: this.unregisterGate
};
}
/*
Gate API Functions
*/
}, {
key: "getStepApi",
value: function getStepApi() {
return {
goBackward: this.goBackward,
goForward: this.goForward,
goToGate: this.goToGate,
registerStep: this.registerStep,
setError: this.setError
};
}
}, {
key: "addGate",
value: function addGate(gateId, label) {
this.setState(function (state) {
return _objectSpread(_objectSpread({}, state), {}, {
gates: new Map(state.gates.set(gateId, makeDefaultGate(gateId, label)))
});
});
}
}, {
key: "registerGate",
value: function registerGate(gateId, label) {
var _this = this;
// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(function () {
_this.addGate(gateId, label);
return function () {
return _this.removeGate(gateId);
};
}, [gateId, label]);
}
}, {
key: "removeGate",
value: function removeGate(gateId) {
this.setState(function (state) {
var gates = state.gates;
gates["delete"](gateId);
return _objectSpread(_objectSpread({}, state), {}, {
gates: new Map(state.gates["delete"](gateId))
});
});
}
/*
Step API Functions
*/
}, {
key: "addStep",
value: function addStep(stepId) {
this.setState(function (state) {
return _objectSpread(_objectSpread({}, state), {}, {
steps: new Map(state.steps.set(stepId, makeDefaultStep(stepId)))
});
});
}
}, {
key: "goBackward",
value: function goBackward() {
var _this2 = this;
setTimeout(function () {
_this2.setState(function (state) {
var currentGate = state.currentGate,
currentStep = state.currentStep,
gates = state.gates,
steps = state.steps;
var previousGate = currentGate;
var previousStep = currentStep - 1;
/* If there isn't a previous step, we'll go to the previous gate */
if (previousStep < 0) {
previousStep = 0;
previousGate = currentGate - 1;
/* If there isn't a previous gate, don't progress */
if (previousGate < 0) {
previousGate = currentGate;
}
}
var _currentGate = _slicedToArray(_toConsumableArray(gates)[currentGate], 2),
currentGateId = _currentGate[0],
currentGateState = _currentGate[1];
var _previousGate = _slicedToArray(_toConsumableArray(gates)[previousGate], 2),
previousGateId = _previousGate[0],
previousGateState = _previousGate[1];
var _previousStep = _slicedToArray(_toConsumableArray(steps)[previousStep], 2),
previousStepId = _previousStep[0],
previousStepState = _previousStep[1];
var _currentStep = _slicedToArray(_toConsumableArray(steps)[currentStep], 2),
currentStepId = _currentStep[0],
currentStepState = _currentStep[1];
gates.set(currentGateId, _objectSpread(_objectSpread({}, currentGateState), {}, {
isComplete: false
}));
gates.set(previousGateId, _objectSpread(_objectSpread({}, previousGateState), {}, {
isComplete: false
}));
steps.set(currentStepId, _objectSpread(_objectSpread({}, currentStepState), {}, {
isComplete: false,
isError: false
}));
steps.set(previousStepId, _objectSpread(_objectSpread({}, previousStepState), {}, {
isComplete: false,
isError: false
}));
return _objectSpread(_objectSpread({}, state), {}, {
currentStep: previousStep,
currentGate: previousGate,
gates: new Map(gates),
isComplete: false,
steps: new Map(steps)
});
});
}, 1);
}
}, {
key: "goForward",
value: function goForward() {
var _this3 = this;
setTimeout(function () {
_this3.setState(function (state) {
var currentGate = state.currentGate,
currentStep = state.currentStep,
gates = state.gates,
steps = state.steps;
var _currentGate2 = _slicedToArray(_toConsumableArray(gates)[currentGate], 2),
currentGateId = _currentGate2[0],
currentGateState = _currentGate2[1];
var _currentStep2 = _slicedToArray(_toConsumableArray(steps)[currentStep], 2),
currentStepId = _currentStep2[0],
currentStepState = _currentStep2[1];
var gatesLength = _toConsumableArray(gates).length;
var stepsLength = _toConsumableArray(steps).length;
var nextGate = currentGate;
var nextStep = currentStep + 1;
var isComplete = false;
var isGateComplete = false;
/* If there isn't a next step, we'll go to the next gate */
if (nextStep >= stepsLength) {
nextStep = 0;
nextGate = currentGate + 1;
isGateComplete = true;
/* If there isn't a next gate, don't progress */
if (nextGate >= gatesLength) {
nextStep = currentStep;
nextGate = currentGate;
isComplete = true;
}
}
return _objectSpread(_objectSpread({}, state), {}, {
currentStep: nextStep,
currentGate: nextGate,
gates: new Map(gates.set(currentGateId, _objectSpread(_objectSpread({}, currentGateState), {}, {
isComplete: isGateComplete
}))),
isComplete: isComplete,
steps: new Map(steps.set(currentStepId, _objectSpread(_objectSpread({}, currentStepState), {}, {
isComplete: true
})))
});
});
}, 1);
}
}, {
key: "goToGate",
value: function goToGate(gateId) {
var _this4 = this;
setTimeout(function () {
_this4.setState(function (state) {
var currentGate = state.currentGate,
gates = state.gates;
var gateKeys = _toConsumableArray(gates.keys());
var targetGate = gateKeys.indexOf(gateId);
if (targetGate > currentGate) {
throw new Error('Can only go back to a previous gate');
}
if (targetGate === -1) {
throw new Error('Did not find target gate');
}
for (var i = targetGate; i <= currentGate; i += 1) {
var key = gateKeys[i];
gates.set(key, _objectSpread(_objectSpread({}, gates.get(key)), {}, {
isComplete: false
}));
}
return _objectSpread(_objectSpread({}, state), {}, {
currentGate: targetGate,
currentStep: 0,
gates: new Map(gates),
isComplete: false
});
});
}, 1);
}
}, {
key: "registerStep",
value: function registerStep(stepId) {
var _this5 = this;
// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(function () {
_this5.addStep(stepId);
return function () {
return _this5.removeStep(stepId);
};
}, [stepId]);
}
}, {
key: "removeStep",
value: function removeStep(stepId) {
this.setState(function (state) {
var steps = state.steps;
steps["delete"](stepId);
return _objectSpread(_objectSpread({}, state), {}, {
steps: new Map(steps)
});
});
}
}, {
key: "setError",
value: function setError(isError) {
this.setState(function (state) {
var currentStep = state.currentStep,
steps = state.steps;
var _currentStep3 = _slicedToArray(_toConsumableArray(steps)[currentStep], 2),
currentStepId = _currentStep3[0],
currentStepState = _currentStep3[1];
steps.set(currentStepId, _objectSpread(_objectSpread({}, currentStepState), {}, {
isError: isError
}));
return _objectSpread(_objectSpread({}, state), {}, {
steps: new Map(steps)
});
});
}
}], [{
key: "getStateDefaults",
value: function getStateDefaults() {
return {
currentGate: 0,
currentStep: 0,
gates: new Map(),
isComplete: false,
isError: false,
steps: new Map()
};
}
}]);
return StepGateController;
}();
var StepperController = StepGateController;
export { StepperController as default };
//# sourceMappingURL=StepperController.js.map