kea-react
Version:
Componentes comunes de react
57 lines (49 loc) • 1.76 kB
text/typescript
/**Funciones para tratar el texto de las celdas de un grid */
import { CellData } from "./baseTypes";
export function getEffectiveString(x: CellData, string: string | undefined): string {
return string == undefined ? cellToText(x) : string;
}
/**Convierte una celda en el texto que se va a usar para la busqueda por texto*/
export function cellToText(x: CellData): string {
if (x == null) {
return "";
} else if (typeof x == "number") {
return "" + x;
} else if (typeof x == "boolean") {
return x ? "si" : "no";
} else if (typeof x == "string") {
return x;
} else if (x instanceof Date) {
return formatDate(x);
} else if (x instanceof Array) {
const y: any[] = x;
return y.map(z => cellToText(z)).join(", ");
}
else if (typeof x == "object") {
return Object.keys(x).join(" ");
};
throw new Error("No soportado");
}
function formatDate(x: Date) {
const locale = "es-MX";
let options: Intl.DateTimeFormatOptions;
options = {
year: "numeric",
month: "2-digit",
day: "2-digit",
};
const year = "" + x.getFullYear();
const month = "0" + (x.getMonth() + 1);
const day = "0" + x.getDate();
const hours = "0" + x.getHours();
const minutes = "0" + x.getMinutes();
//True si se debe de mostrar hora y fecha, si no, solo la fecha
const fullDateTime = hours != "00" || minutes != "00";
const dateStr = day.slice(-2) + "/" + month.slice(-2) + "/" + year;
if(fullDateTime) {
const hourStr = hours.slice(-2) + ":" + minutes.slice(-2);
return dateStr + " " + hourStr;
} else {
return dateStr;
}
}