Repository URL to install this package:
|
Version:
4.0.23 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const tslib_1 = require("tslib");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const exotic_1 = require("exotic");
const InputState_1 = require("../input/InputState");
const fixture_1 = require("./fixture");
/**
* === added these 4 fns to split out serializing ===
*/
function toSerialized(serializable) {
return serializable.toJSON ? serializable.toJSON() : JSON.stringify(serializable);
}
exports.toSerialized = toSerialized;
function fromInputToSpread(inputData) {
const keyArray = Object.keys(inputData);
const key = keyArray[0];
const value = inputData[key];
return {
value,
key
};
}
exports.fromInputToSpread = fromInputToSpread;
function fromInputToSerializable(input, getInputValue) {
if (input.type === 'groupElements') {
return input.elementList.slice(0).map(getInputValue);
} else {
return input;
}
}
exports.fromInputToSerializable = fromInputToSerializable;
/**
* we pass getInputValue here because it's used as a recursive tail call
* @see http://2ality.com/2015/06/tail-call-optimization.html
*/
function fromInputToSerializedKeyValue(input, getInputValue) {
const serializable = fromInputToSerializable(input, getInputValue);
const inputData = toSerialized(serializable);
return fromInputToSpread(inputData);
}
exports.fromInputToSerializedKeyValue = fromInputToSerializedKeyValue;
/**
* @todo - this should be InputState.from
* @description ensure the value is coerced to inputstate
*/
function toInputState(input, index) {
if (input.type === 'groupElements') {
input.elementList = input.elementList.map(toInputState);
} // already something that extends input state
if (exotic_1.isFunction(input.setValue)) {
return input;
} else {
return InputState_1.InputState.init(input);
}
}
exports.toInputState = toInputState;
/**
* @todo split
*/
function toClassList(instance) {
const {
formClassName,
// @deprecated
SubmitCustomClass,
CancelCustomClass,
ButtonGroupCustomClass,
// cache stringified
cachedClassList
} = instance;
const customClasslist = instance.classList || exotic_1.EMPTY_OBJ;
const custom = {
form: formClassName,
buttonGroup: customClasslist.buttonClass || ButtonGroupCustomClass,
submitButton: customClasslist.submitButton || SubmitCustomClass,
cancelButton: customClasslist.cancelButton || CancelCustomClass
};
if (JSON.stringify(custom) === JSON.stringify(cachedClassList)) {
return cachedClassList;
}
const classList = {
/**
* classNames
* @todo - variable names are camelCase...
*/
// form: [classes.form, custom.form],
form: classnames_1.default(fixture_1.classes.form, custom.form),
/**
* is usually Blue
* there is no need for custom classname
* use scoped styles
*/
// submitButton: [classes.submitButton, custom.submitButton],
submitButton: classnames_1.default(fixture_1.classes.submitButton, custom.submitButton),
/**
* is usually GhostButton or BlueButton.grey
*/
// cancelButton: [classes.cancelButton, custom.cancelButton],
cancelButton: classnames_1.default(fixture_1.classes.cancelButton, custom.cancelButton),
/**
* meh
*/
// buttonGroup: [classes.buttonGroup, custom.buttonGroup],
buttonGroup: classnames_1.default(fixture_1.classes.buttonGroup, custom.buttonGroup)
};
instance.cachedClassList = classList;
return classList;
}
exports.toClassList = toClassList;
/**
* @example
* <label for='eh'>
* <?img /> to show an image instead of the input for custom styles
* <input id='eh'/>
* <span|div> children|text
* </label>
*/
exports.isInputState = input => exotic_1.isObj(input) && exotic_1.isFunction(input.setValue);
function handleRecursive(state, index) {
const input = state.inputsList[index];
const {
elementList
} = input;
if (exotic_1.isArray(elementList) === false) {
return;
}
const toChild = (nested, nestedIndex) => {
const nestedState = toInputStateAt(input, nestedIndex); // just metadata, can remove
// nestedState.parent = input
// nestedState.form = state
return nestedState;
}; // for toInputStateAt...
input.inputsList = input.elementList;
input.elementList = input.elementList.map(toChild);
input.elementList = input.inputsList;
delete input.inputsList;
}
exports.handleRecursive = handleRecursive;
function toInputStateAt(state, index) {
// annoyingly named
const inputsList = state.inputsList;
const initialState = inputsList[index]; // !!!! THIS IS THE ISSUE
// initialState && initialState instanceof InputState
const isAlreadyState = exports.isInputState(initialState); // for sanity
if (isAlreadyState === true) {
return initialState;
} // should call action...
if (isAlreadyState === false) {
// could pass in form as a parent (, state)
state.inputsList[index] = new InputState_1.InputState(initialState);
handleRecursive(state, index);
}
const instantiated = inputsList[index];
return instantiated;
}
exports.toInputStateAt = toInputStateAt; //# sourceMappingURL=deps.js.map