mirror of
https://github.com/inventree/InvenTree.git
synced 2026-11-27 01:58:44 +00:00
Refactor more UI components out into lib directory (#9994)
* Refactor table column types * Offloading more component type definitions * Remove unused funcs * Move conversion functions * ActionButton * Refactor YesNoButton * ProgressBar * make row actions available * search input * ButtonMenu * Bump UI version * Tweak function defs
This commit is contained in:
1 parent
c6166d7c4a
commit
d137728e60
119 files changed
+664
-524
No files matched your search
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Determine if the provided value is "true":
|
||||
*
|
||||
* Many settings stored on the server are true/false,
|
||||
* but stored as string values, "true" / "false".
|
||||
*
|
||||
* This function provides a wrapper to ensure that the return type is boolean
|
||||
*/
|
||||
export function isTrue(value: any): boolean {
|
||||
if (value === true) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (value === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const s = String(value).trim().toLowerCase();
|
||||
|
||||
return ['true', 'yes', '1', 'on', 't', 'y'].includes(s);
|
||||
}
|
||||
|
||||
/*
|
||||
* Resolve a nested item in an object.
|
||||
* Returns the resolved item, if it exists.
|
||||
*
|
||||
* e.g. resolveItem(data, "sub.key.accessor")
|
||||
*
|
||||
* Allows for retrieval of nested items in an object.
|
||||
*/
|
||||
export function resolveItem(obj: any, path: string): any {
|
||||
const properties = path.split('.');
|
||||
return properties.reduce((prev, curr) => prev?.[curr], obj);
|
||||
}
|
||||
|
||||
export function identifierString(value: string): string {
|
||||
// Convert an input string e.g. "Hello World" into a string that can be used as an identifier, e.g. "hello-world"
|
||||
|
||||
value = value || '-';
|
||||
|
||||
return value.toLowerCase().replace(/[^a-z0-9]/g, '-');
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
export interface FormatDecmimalOptionsInterface {
|
||||
digits?: number;
|
||||
minDigits?: number;
|
||||
locale?: string;
|
||||
}
|
||||
|
||||
export interface FormatCurrencyOptionsInterface {
|
||||
digits?: number;
|
||||
minDigits?: number;
|
||||
currency?: string;
|
||||
locale?: string;
|
||||
multiplier?: number;
|
||||
}
|
||||
|
||||
export function formatDecimal(
|
||||
value: number | null | undefined,
|
||||
options: FormatDecmimalOptionsInterface = {}
|
||||
) {
|
||||
const locale = options.locale || navigator.language || 'en-US';
|
||||
|
||||
if (value === null || value === undefined) {
|
||||
return value;
|
||||
}
|
||||
|
||||
const formatter = new Intl.NumberFormat(locale, {
|
||||
style: 'decimal',
|
||||
maximumFractionDigits: options.digits ?? 6,
|
||||
minimumFractionDigits: options.minDigits ?? 0
|
||||
});
|
||||
|
||||
return formatter.format(value);
|
||||
}
|
||||
|
||||
/*
|
||||
* format currency (money) value based on current settings
|
||||
*
|
||||
* Options:
|
||||
* - currency: Currency code (uses default value if none provided)
|
||||
* - locale: Locale specified (uses default value if none provided)
|
||||
* - digits: Maximum number of significant digits (default = 10)
|
||||
*/
|
||||
export function formatCurrencyValue(
|
||||
value: number | string | null | undefined,
|
||||
options: FormatCurrencyOptionsInterface = {}
|
||||
) {
|
||||
if (value == null || value == undefined) {
|
||||
return null;
|
||||
}
|
||||
|
||||
value = Number.parseFloat(value.toString());
|
||||
|
||||
if (Number.isNaN(value) || !Number.isFinite(value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
value *= options.multiplier ?? 1;
|
||||
|
||||
// Extract locale information
|
||||
const locale = options.locale || navigator.language || 'en-US';
|
||||
|
||||
const minDigits = options.minDigits ?? 0;
|
||||
const maxDigits = options.digits ?? 6;
|
||||
|
||||
const formatter = new Intl.NumberFormat(locale, {
|
||||
style: 'currency',
|
||||
currency: options.currency,
|
||||
maximumFractionDigits: Math.max(minDigits, maxDigits),
|
||||
minimumFractionDigits: Math.min(minDigits, maxDigits)
|
||||
});
|
||||
|
||||
return formatter.format(value);
|
||||
}
|
||||
|
||||
/*
|
||||
* Format a file size (in bytes) into a human-readable format
|
||||
*/
|
||||
export function formatFileSize(size: number) {
|
||||
const suffixes: string[] = ['B', 'KB', 'MB', 'GB'];
|
||||
|
||||
let idx = 0;
|
||||
|
||||
while (size > 1024 && idx < suffixes.length) {
|
||||
size /= 1024;
|
||||
idx++;
|
||||
}
|
||||
|
||||
return `${size.toFixed(2)} ${suffixes[idx]}`;
|
||||
}
|
||||
Reference in new issue
Block a user