update TypeInput.tsx, TableList.tsx and date.ts

This commit is contained in:
faisolavolut 2025-02-04 16:09:15 +07:00
parent 3532415aba
commit 3cfeba878d
3 changed files with 63 additions and 24 deletions

View File

@ -25,6 +25,10 @@ export const TypeInput: React.FC<any> = ({
const textareaRef = useRef<HTMLTextAreaElement | null>(null); const textareaRef = useRef<HTMLTextAreaElement | null>(null);
let value: any = fm.data?.[name] || ""; let value: any = fm.data?.[name] || "";
if (type === "time") {
value = convertToTimeOnly(value) || "";
}
const [rating, setRating] = useState(value); // State untuk menyimpan nilai rating const [rating, setRating] = useState(value); // State untuk menyimpan nilai rating
const handleClick = (index: number) => { const handleClick = (index: number) => {
setRating(index); // Update nilai rating setRating(index); // Update nilai rating
@ -62,6 +66,12 @@ export const TypeInput: React.FC<any> = ({
const convertColor = tinycolor(meta.inputValue); const convertColor = tinycolor(meta.inputValue);
meta.rgbValue = convertColor.toRgbString(); meta.rgbValue = convertColor.toRgbString();
meta.render(); meta.render();
} else if (type === "time") {
if (fm.data?.[name]) fm.data[name] = convertToTimeOnly(fm.data[name]);
fm.render();
console.log({
data: fm.data,
});
} else { } else {
setRating(value ? value - 1 : value); setRating(value ? value - 1 : value);
} }
@ -442,3 +452,13 @@ const hasNonZeroDigitAfterDecimal = (input: string) => {
const regex = /[.,]\d*[1-9]\d*/; const regex = /[.,]\d*[1-9]\d*/;
return regex.test(input); return regex.test(input);
}; };
export const convertToTimeOnly = (isoString: any) => {
if (!isoString || isoString === "") return null;
const isoRegex = /^\d{4}-\d{2}-\d{2}T(\d{2}:\d{2}):\d{2}Z$/;
const match = isoString.match(isoRegex);
if (match) {
return match[1]; // Mengambil HH:mm dari format ISO
}
return isoString; // Jika format tidak sesuai, kembalikan string asli
};

View File

@ -27,6 +27,7 @@ import { getNumber } from "@/lib/utils/getNumber";
import { formatMoney } from "../form/field/TypeInput"; import { formatMoney } from "../form/field/TypeInput";
export const TableList: React.FC<any> = ({ export const TableList: React.FC<any> = ({
autoPagination = true,
name, name,
column, column,
style = "UI", style = "UI",
@ -115,35 +116,30 @@ export const TableList: React.FC<any> = ({
</> </>
); );
if (Array.isArray(onLoad)) { if (Array.isArray(onLoad)) {
local.data = onLoad; let res = onLoad;
if (!autoPagination) {
res = paginateArray(res, take, 1);
}
local.data = res;
local.render(); local.render();
setData(onLoad); setData(res);
} else { } else {
const res: any = onLoad({ let res: any = await onLoad({
search: local.search, search: local.search,
sort: local.sort, sort: local.sort,
take, take,
paging: 1, paging: 1,
}); });
if (res instanceof Promise) { if (!autoPagination) {
res.then((e) => { res = paginateArray(res, take, 1);
local.data = e;
cloneListFM(e);
local.render();
setData(e);
setTimeout(() => {
toast.dismiss();
}, 2000);
});
} else {
local.data = res;
cloneListFM(res);
local.render();
setData(res);
setTimeout(() => {
toast.dismiss();
}, 2000);
} }
local.data = res;
cloneListFM(res);
local.render();
setData(res);
setTimeout(() => {
toast.dismiss();
}, 1000);
} }
}, },
}); });
@ -203,19 +199,26 @@ export const TableList: React.FC<any> = ({
local.render(); local.render();
setData(onLoad); setData(onLoad);
} else if (typeof onLoad === "function") { } else if (typeof onLoad === "function") {
const res: any = await onLoad({ let res: any = await onLoad({
search: local.search, search: local.search,
sort: local.sort, sort: local.sort,
take, take,
paging: 1, paging: 1,
}); });
if (!autoPagination) {
res = paginateArray(res, take, 1);
}
local.data = res; local.data = res;
if (mode === "form") cloneListFM(res); if (mode === "form") cloneListFM(res);
local.render(); local.render();
setData(local.data); setData(local.data);
} else { } else {
local.data = onLoad; let res = onLoad;
if (mode === "form") cloneListFM(onLoad); if (!autoPagination) {
res = paginateArray(res, take, 1);
}
local.data = res;
if (mode === "form") cloneListFM(res);
local.render(); local.render();
setData(local.data); setData(local.data);
} }
@ -1013,3 +1016,12 @@ const getPagination = (currentPage: number, totalPages: number) => {
return pagination; return pagination;
}; };
function paginateArray(array: any[], take: number, paging: number) {
if (!Array.isArray(array) || !array?.length) {
return [];
}
const startIndex = (paging - 1) * take;
const endIndex = startIndex + take;
return array.slice(startIndex, endIndex);
}

View File

@ -46,3 +46,10 @@ export const formatTime = (date: string | Date) => {
} }
return "-"; return "-";
}; };
export const time = (date: string | Date) => {
if (date instanceof Date || typeof date === "string") {
return day(date).format("hh:mm");
}
return null;
};