@e-group/utils
Version:
eGroup team utils that share across projects.
140 lines (119 loc) • 4.23 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
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) { _defineProperty(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; }
import { fromJS, List, isImmutable, Map } from '@e-group/immutable';
/**
* If formatter value is string or boolean get the correct option format for react select.
* Option format example, { value: 'foo', label: 'bar' }.
*/
function getFormattedSelectOption({
value,
labelPath,
valuePath,
selectOptions
}) {
if (typeof value === 'string' || typeof value === 'number') {
if (selectOptions) {
const selectedOption = selectOptions.filter(el => el[valuePath] === value)[0];
if (selectedOption) {
return fromJS(selectedOption);
}
}
return fromJS({
label: value,
value
});
}
if (Map.isMap(value)) {
return fromJS(_objectSpread(_objectSpread({}, value.toJS()), {}, {
label: value.getIn(labelPath),
value: value.getIn(valuePath)
}));
}
return value;
}
/**
* Make react select redux form formatter.
* @param {*} options
* @param {array} options.labelPath
* @param {array} options.valuePath
*/
export function makeReactSelectFormatter(options = {}) {
const _options$labelPath = options.labelPath,
labelPath = _options$labelPath === void 0 ? ['label'] : _options$labelPath,
_options$valuePath = options.valuePath,
valuePath = _options$valuePath === void 0 ? ['value'] : _options$valuePath,
selectOptions = options.options;
return function formatter(value, name) {
if (List.isList(value)) {
return value.map(el => getFormattedSelectOption({
value: el,
labelPath,
valuePath,
selectOptions
}));
}
return getFormattedSelectOption({
value,
labelPath,
valuePath,
selectOptions
});
};
}
/**
* If react select options value is immutable Map get the correct value format to store in redux form.
*/
function getNormalizedReduxFormValue({
value,
disableReturnStringValue
}) {
if (Map.isMap(value)) {
if (disableReturnStringValue) {
return value.deleteAll(['label', 'value']);
}
return value.get('value');
}
return value;
}
/**
* Make react select redux form normalizer.
* @param {*} options
* @param {boolean} options.disableReturnStringValue set `true` to disable return String value
*/
export function makeReactSelectNormalizer(options = {}) {
const disableReturnStringValue = options.disableReturnStringValue;
return function normalizer(value, previousValue, allValues, previousAllValues, name) {
if (isImmutable(value)) {
if (List.isList(value)) {
return value.map(el => getNormalizedReduxFormValue({
value: el,
disableReturnStringValue
}));
}
return getNormalizedReduxFormValue({
value,
disableReturnStringValue
});
}
return value;
};
}
/**
* normalize form field of number
* @param {object} options
*/
export function positive(options = {}) {
const _options$min = options.min,
min = _options$min === void 0 ? 0 : _options$min,
max = options.max,
fixed = options.fixed;
return value => {
let result = fixed ? Number(value).toFixed() : Number(value);
if (result < min) result = min;
if (typeof max === 'number') {
if (result > max) result = max;
}
return result;
};
}