@raona/components
Version:
React components used at Raona to work with SPFx
278 lines (277 loc) • 16.8 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var react_1 = require("react");
// import { Bind, calculateDropdownOptions, FileBuffer, TypedHash } from '@raona/sp';
// import { IDropdownOption, values } from 'office-ui-fabric-react';
var Yup = require("yup");
var Bind_1 = require("@raona/sp/dist/decorators/Bind");
var calculateDropdownOptions_1 = require("@raona/sp/dist/functions/calculateDropdownOptions");
var FormContainer_1 = require("./FormElements/FormContainer");
var FormFooterButtons_1 = require("./FormElements/FormFooterButtons");
var SteppedForm = /** @class */ (function (_super) {
__extends(SteppedForm, _super);
function SteppedForm(props, context) {
var _this = _super.call(this, props, context) || this;
_this.state = {
step: 0,
values: _this.props.initialValues || {},
isNextStepAvailable: false,
stepErrors: [],
showErrors: false,
isPublishing: false,
isWizardStepError: false,
};
return _this;
}
SteppedForm.prototype.componentDidMount = function () {
var initialValues = this.props.initialValues;
initialValues ? this.handleValidation(initialValues) : this.handleValidation({});
};
SteppedForm.prototype.changeStep = function (operation, stepForm) {
var _a = this.state, step = _a.step, values = _a.values;
var onChangeStep = this.props.onChangeStep;
this.setState({ isWizardStepError: false });
step += operation;
if (onChangeStep)
onChangeStep(step);
this.setState({ step: step, showErrors: false });
// To avoid doing it on the final step
if (stepForm !== undefined && step !== stepForm.length)
this.handleValidation(values, step);
};
SteppedForm.prototype.componentDidUpdate = function (prevProps, prevState) {
if (prevProps.forms !== this.props.forms) {
this.handleValidation(this.state.values, undefined, true);
}
};
SteppedForm.prototype.handleValidation = function (values, stepToCheck, avoidCallingOnChange) {
return __awaiter(this, void 0, void 0, function () {
var step, forms, fieldGroups, validationObject, validationSchema;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
step = this.state.step;
forms = this.props.forms;
step = stepToCheck !== undefined ? stepToCheck : step;
fieldGroups = forms.stepForm[step].groups;
validationObject = fieldGroups.reduce(function (obj, group) {
obj = group.fields.reduce(function (a, b) { return (a[b.id] = b.validation, a); }, obj);
return obj;
}, {});
validationSchema = Yup.object().shape(validationObject);
return [4 /*yield*/, validationSchema.validate(values, { abortEarly: false }).catch(function (err) {
_this.onChange(avoidCallingOnChange);
_this.setState({ stepErrors: err.inner, isNextStepAvailable: false /* , isWizardStepAvailable: false */ });
}).then(function (val) {
_this.onChange(avoidCallingOnChange);
if (val !== undefined) {
_this.setState({ stepErrors: [], isNextStepAvailable: true });
return true;
}
else {
return false;
}
})];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
SteppedForm.prototype.setValues = function (key, object, options) {
var state = this.state;
if (typeof options !== 'undefined') {
var calcDropdownoptions = calculateDropdownOptions_1.calculateDropdownOptions(options, object);
// console.log('setvalues_dropdown', calcDropdownoptions);
state.values[key] = calcDropdownoptions.dropdownOptions;
}
else {
state.values[key] = object;
}
this.setState(state);
this.setState({ isWizardStepError: false });
};
SteppedForm.prototype.headerStepChange = function (destStep, form, isPublishStep) {
return __awaiter(this, void 0, void 0, function () {
var _a, isNextStepAvailable, step, values, finalStep, isWizardStepAvailable, operation, i, bool;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = this.state, isNextStepAvailable = _a.isNextStepAvailable, step = _a.step, values = _a.values;
finalStep = this.props.finalStep;
isWizardStepAvailable = true;
operation = destStep - step;
if (operation < 0)
return [2 /*return*/, this.changeStep(operation, form.stepForm)];
if (isNextStepAvailable === false) {
return [2 /*return*/, this.setState({ showErrors: true })];
}
if (!isPublishStep) return [3 /*break*/, 2];
this.setState({ isPublishing: true });
return [4 /*yield*/, this.submitForm()];
case 1:
_b.sent();
_b.label = 2;
case 2:
;
if (!(operation > 1)) return [3 /*break*/, 7];
i = step;
_b.label = 3;
case 3:
if (!(i < destStep)) return [3 /*break*/, 6];
return [4 /*yield*/, this.handleValidation(values, i)];
case 4:
bool = _b.sent();
if (!bool) {
isWizardStepAvailable = isWizardStepAvailable && bool;
}
;
_b.label = 5;
case 5:
i++;
return [3 /*break*/, 3];
case 6:
if (isWizardStepAvailable) {
return [2 /*return*/, this.changeStep(operation, form.stepForm)];
}
else {
this.setState({ isWizardStepError: true });
this.setState({ isNextStepAvailable: true });
}
;
return [3 /*break*/, 8];
case 7:
if (finalStep || !isPublishStep) {
return [2 /*return*/, this.changeStep(operation, form.stepForm)];
}
_b.label = 8;
case 8: return [2 /*return*/];
}
});
});
};
SteppedForm.prototype.onChange = function (avoidCallingOnChange) {
if (avoidCallingOnChange)
return;
var onChange = this.props.onChange;
var values = this.state.values;
if (onChange)
onChange(values);
};
SteppedForm.prototype.submitForm = function () {
var onSubmit = this.props.onSubmit;
var values = this.state.values;
return onSubmit(values);
};
SteppedForm.prototype.deleteFile = function (id, index, setFieldValues) {
var state = this.state;
// TODO: check why the immutable version doesn't work
// let indexCallback: (_, index: number) => void = (_, i) => i !== index;
if (state.values[id])
state.values[id].splice(index, 1);
// if(state.values[id]) state.values[id] = state.values[id].filter(indexCallback);
(setFieldValues) && setFieldValues(id, state.values[id]);
// console.log(state.values, state.defaultValues);
this.setState(state);
};
SteppedForm.prototype.render = function () {
var _this = this;
var _a = this.props, forms = _a.forms, context = _a.context, finalStep = _a.finalStep, footerClassName = _a.footerClassName, nextBtnClassName = _a.nextBtnClassName, deleteBtnClassName = _a.deleteBtnClassName, prevBtnClassName = _a.prevBtnClassName, modalClassName = _a.className, bodyClassName = _a.bodyClassName, header = _a.header, prevBtnText = _a.prevBtnText, nextBtnText = _a.nextBtnText, publishBtnText = _a.publishBtnText, initialValues = _a.initialValues, leftSlot = _a.leftSlot, rightSlot = _a.rightSlot, hideNextButton = _a.hideNextButton, hidePrevButton = _a.hidePrevButton, hidePublishButton = _a.hidePublishButton, spinner = _a.spinner, formButtonsClassName = _a.formButtonsClassName, formWrapperClassName = _a.formWrapperClassName, formInnerWrapperClassName = _a.formInnerWrapperClassName, headerDiv = _a.headerDiv, closeDiv = _a.closeDiv, wizardStepErrorMsg = _a.wizardStepErrorMsg, wizardStepErrorClassName = _a.wizardStepErrorClassName;
var _b = this.state, step = _b.step, showErrors = _b.showErrors, stepErrors = _b.stepErrors, isNextStepAvailable = _b.isNextStepAvailable, values = _b.values, isPublishing = _b.isPublishing, isWizardStepError = _b.isWizardStepError;
var isMultiStepForm = forms.stepForm.length > 1;
var isFirst = step === 0 && isMultiStepForm;
var isLast = step === forms.stepForm.length - 1;
var isInBetween = step > 0 && step < forms.stepForm.length - 1 && isMultiStepForm;
var isStep = !(step === forms.stepForm.length);
var newHeader;
if (header)
newHeader = react_1.cloneElement(header, { headerStepChange: this.headerStepChange });
return (React.createElement("div", { className: modalClassName }, isStep
? (React.createElement(react_1.Fragment, null,
headerDiv && react_1.cloneElement(headerDiv, { headerStepChange: this.headerStepChange }, [newHeader, closeDiv]),
isWizardStepError && React.createElement("div", { className: wizardStepErrorClassName }, wizardStepErrorMsg),
React.createElement("div", { className: bodyClassName },
leftSlot,
React.createElement(FormContainer_1.FormContainer, { handleValidation: this.handleValidation, fieldGroups: forms.stepForm[step].groups, stepErrors: stepErrors, context: context, setValues: this.setValues, deleteFile: this.deleteFile, showErrors: showErrors, initialFormValues: initialValues, formattedValues: values, formWrapperClassName: formWrapperClassName, formInnerWrapperClassName: formInnerWrapperClassName }),
rightSlot),
React.createElement(FormFooterButtons_1.FormFootButtons, { isFirst: isFirst, isLast: isLast, isInBetween: isInBetween, isMultiStepForm: isMultiStepForm, isNextStepAvailable: isNextStepAvailable, handleShowErrors: function (value) { return _this.setState({ showErrors: value }); }, step: step, form: forms, finalStep: finalStep, deleteBtnClassName: deleteBtnClassName, nextBtnClassName: nextBtnClassName, footerClassName: footerClassName, prevBtnClassName: prevBtnClassName, changeStep: this.changeStep, submitForm: this.submitForm, customButtons: forms.stepForm[step].customButton, prevBtnText: prevBtnText, nextBtnText: nextBtnText, publishBtnText: publishBtnText, spinner: spinner, hideNextButton: hideNextButton, hidePrevButton: hidePrevButton, hidePublishButton: hidePublishButton, formButtonsClassName: formButtonsClassName, headerStepChange: this.headerStepChange, isPublishing: isPublishing }))) : (React.createElement("div", { className: bodyClassName }, finalStep))));
};
__decorate([
Bind_1.Bind()
], SteppedForm.prototype, "changeStep", null);
__decorate([
Bind_1.Bind()
], SteppedForm.prototype, "handleValidation", null);
__decorate([
Bind_1.Bind()
], SteppedForm.prototype, "setValues", null);
__decorate([
Bind_1.Bind()
], SteppedForm.prototype, "headerStepChange", null);
__decorate([
Bind_1.Bind()
], SteppedForm.prototype, "onChange", null);
__decorate([
Bind_1.Bind()
], SteppedForm.prototype, "submitForm", null);
__decorate([
Bind_1.Bind()
], SteppedForm.prototype, "deleteFile", null);
return SteppedForm;
}(react_1.Component));
exports.SteppedForm = SteppedForm;