@react-awesome-query-builder/ui
Version:
User-friendly query builder for React. Core React UI
170 lines (165 loc) • 8.19 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { NumericFormat, numericFormatter as _numericFormatter, removeNumericFormat, useNumericFormat } from "react-number-format";
var getNumberFormatProps = function getNumberFormatProps(props) {
var excludePropsNames = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var propNames = [
// Numeric format
"prefix", "suffix", "thousandsGroupStyle", "thousandSeparator", "allowLeadingZeros", "allowNegative", "decimalScale", "decimalSeparator", "fixedDecimalScale",
// Pattern format
"allowEmptyFormatting", "format", "mask", "patternChar"];
var existingPropNames = propNames.filter(function (k) {
return !excludePropsNames.includes(k) && props[k] !== undefined;
});
var existingProps = Object.fromEntries(existingPropNames.map(function (k) {
return [k, props[k]];
}));
if (props.min >= 0) {
existingProps.allowNegative = false;
}
return existingProps;
};
var numericFormatter = function numericFormatter(val, numericFormatProps) {
if (val == undefined) {
return "";
}
return _numericFormatter("" + val, numericFormatProps);
};
// Copied from https://github.com/s-yadav/react-number-format/blob/master/src/utils.tsx#L309
function getDefaultChangeMeta(value) {
return {
from: {
start: 0,
end: 0
},
to: {
start: 0,
end: value.length
},
lastValue: ""
};
}
// Copied from https://github.com/s-yadav/react-number-format/blob/master/src/utils.tsx#L257
var findChangeRange = function findChangeRange(prevValue, newValue) {
var i = 0,
j = 0;
var prevLength = prevValue.length;
var newLength = newValue.length;
while (prevValue[i] === newValue[i] && i < prevLength) i++;
//check what has been changed from last
while (prevValue[prevLength - 1 - j] === newValue[newLength - 1 - j] && newLength - j > i && prevLength - j > i) {
j++;
}
return {
from: {
start: i,
end: prevLength - j
},
to: {
start: i,
end: newLength - j
}
};
};
// Helps with generating correct `changeMeta` for proper work of `removeNumericFormat()`
var getChangeMeta = function getChangeMeta(newValue) {
var prevValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
if (prevValue != undefined) {
return _objectSpread(_objectSpread({}, findChangeRange(prevValue, newValue)), {}, {
lastValue: newValue
});
} else {
return _objectSpread(_objectSpread({}, getDefaultChangeMeta(newValue)), {}, {
lastValue: newValue !== null && newValue !== void 0 ? newValue : ""
});
}
};
// Apply `decimalScale`
var limitToScale = function limitToScale(num, numericFormatProps) {
var _split$;
var decimalScale = numericFormatProps === null || numericFormatProps === void 0 ? void 0 : numericFormatProps.decimalScale;
var isNegative = num < 0;
// if num == -3.141
var positiveWholeNumberPart = Math.abs(isNegative ? Math.ceil(num) : Math.floor(num)); // 3
var decimalStr = (_split$ = ("" + num).split(".")[1]) !== null && _split$ !== void 0 ? _split$ : ""; // "141"
var decimalPart = decimalStr != undefined ? parseFloat("0." + decimalStr) : undefined;
if (decimalScale && decimalStr.length > decimalScale) {
decimalStr = decimalStr.substring(0, decimalScale); // "14"
decimalPart = parseFloat("0." + decimalStr); // 0.14
}
var res = (isNegative ? -1 : 1) * (positiveWholeNumberPart + decimalPart); // -3.14
return res;
};
var startsWithZero = function startsWithZero(num) {
var isNegative = num < 0;
var positiveWholeNumberPart = Math.abs(isNegative ? Math.ceil(num) : Math.floor(num));
return positiveWholeNumberPart === 0;
};
var getExtraLeadingZeros = function getExtraLeadingZeros(str, num) {
var _str$match;
var leadingZeros = (_str$match = str.match(/^-?(0+).+/)) === null || _str$match === void 0 || (_str$match = _str$match[1]) === null || _str$match === void 0 ? void 0 : _str$match.length;
var extraLeadingZeros = leadingZeros ? leadingZeros - (startsWithZero(num) ? 1 : 0) : undefined;
return extraLeadingZeros;
};
var getTrailingZeros = function getTrailingZeros(str) {
var _parts$;
var parts = str.split(".");
if ((_parts$ = parts[1]) !== null && _parts$ !== void 0 && _parts$.match(/^0+$/)) {
return parts[1].length;
}
return undefined;
};
var fixDecimal = function fixDecimal(origStr, numericFormatProps) {
var _numericFormatProps$d;
var lastStrValue = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
var decimalSeparator = (_numericFormatProps$d = numericFormatProps === null || numericFormatProps === void 0 ? void 0 : numericFormatProps.decimalSeparator) !== null && _numericFormatProps$d !== void 0 ? _numericFormatProps$d : ".";
var str = origStr;
if (lastStrValue !== null && lastStrValue !== void 0 && lastStrValue.endsWith(".") && origStr.indexOf(lastStrValue) === 0) {
// If decimal separator is "," but user types "." and numbers - treat this as decimal part
str = lastStrValue.substring(0, lastStrValue.length - 1) + decimalSeparator + origStr.substring(lastStrValue.length);
}
return str;
};
var numericParser = function numericParser(origStr, numericFormatProps) {
var _numericFormatProps$d2;
var lastStrValue = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
var lastNumValue = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : undefined;
var decimalSeparator = (_numericFormatProps$d2 = numericFormatProps === null || numericFormatProps === void 0 ? void 0 : numericFormatProps.decimalSeparator) !== null && _numericFormatProps$d2 !== void 0 ? _numericFormatProps$d2 : ".";
var str = fixDecimal(origStr, numericFormatProps, lastStrValue);
var changeMeta = getChangeMeta(str, lastStrValue);
var typedDecimalSep = changeMeta.to.end - changeMeta.to.start === 1 && [decimalSeparator, "."].includes(str[changeMeta.to.start]);
var cleanStr = removeNumericFormat(str, changeMeta, numericFormatProps);
var num = parseFloat(cleanStr);
num = !isNaN(num) && isFinite(num) ? num : undefined;
if (num != undefined) {
num = limitToScale(num, numericFormatProps);
}
var max = numericFormatProps.max,
min = numericFormatProps.min,
allowLeadingZeros = numericFormatProps.allowLeadingZeros;
var changedValueWithSep = typedDecimalSep && num != lastNumValue;
var isValid = num != undefined && (max == undefined || num <= max) && (min == undefined || num >= min);
var extraLeadingZeros = allowLeadingZeros ? getExtraLeadingZeros(cleanStr, num) : undefined;
var trailingZeros = changedValueWithSep ? undefined : getTrailingZeros(cleanStr);
var res = {
str: origStr,
num: num,
extraLeadingZeros: extraLeadingZeros,
trailingZeros: trailingZeros,
isValid: isValid
};
return res;
};
var addLeadingZeros = function addLeadingZeros(str, extraLeadingZeros) {
var isNegative = str.startsWith("-");
var positiveStr = isNegative ? str.replace(/^[-]+/, "") : str;
var res = (isNegative ? "-" : "") + "".padStart(extraLeadingZeros, "0") + positiveStr;
return res;
};
var addTrailingZeros = function addTrailingZeros(str, trailingZeros) {
var hasDot = str.endsWith(".");
var res = str + (hasDot ? "" : ".") + "".padStart(trailingZeros, "0");
return res;
};
export { NumericFormat, useNumericFormat, numericParser, numericFormatter, getNumberFormatProps, addLeadingZeros, addTrailingZeros };