diff --git a/components/form/field/TypeInput.tsx b/components/form/field/TypeInput.tsx index 638965e..ebc4e97 100644 --- a/components/form/field/TypeInput.tsx +++ b/components/form/field/TypeInput.tsx @@ -25,6 +25,10 @@ export const TypeInput: React.FC = ({ const textareaRef = useRef(null); let value: any = fm.data?.[name] || ""; + + if (type === "time") { + value = convertToTimeOnly(value) || ""; + } const [rating, setRating] = useState(value); // State untuk menyimpan nilai rating const handleClick = (index: number) => { setRating(index); // Update nilai rating @@ -62,6 +66,12 @@ export const TypeInput: React.FC = ({ const convertColor = tinycolor(meta.inputValue); meta.rgbValue = convertColor.toRgbString(); 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 { setRating(value ? value - 1 : value); } @@ -442,3 +452,13 @@ const hasNonZeroDigitAfterDecimal = (input: string) => { const regex = /[.,]\d*[1-9]\d*/; 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 +}; diff --git a/components/tablelist/TableList.tsx b/components/tablelist/TableList.tsx index 0edeba7..1e833c9 100644 --- a/components/tablelist/TableList.tsx +++ b/components/tablelist/TableList.tsx @@ -27,6 +27,7 @@ import { getNumber } from "@/lib/utils/getNumber"; import { formatMoney } from "../form/field/TypeInput"; export const TableList: React.FC = ({ + autoPagination = true, name, column, style = "UI", @@ -115,35 +116,30 @@ export const TableList: React.FC = ({ ); if (Array.isArray(onLoad)) { - local.data = onLoad; + let res = onLoad; + if (!autoPagination) { + res = paginateArray(res, take, 1); + } + local.data = res; local.render(); - setData(onLoad); + setData(res); } else { - const res: any = onLoad({ + let res: any = await onLoad({ search: local.search, sort: local.sort, take, paging: 1, }); - if (res instanceof Promise) { - res.then((e) => { - 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); + if (!autoPagination) { + res = paginateArray(res, take, 1); } + local.data = res; + cloneListFM(res); + local.render(); + setData(res); + setTimeout(() => { + toast.dismiss(); + }, 1000); } }, }); @@ -203,19 +199,26 @@ export const TableList: React.FC = ({ local.render(); setData(onLoad); } else if (typeof onLoad === "function") { - const res: any = await onLoad({ + let res: any = await onLoad({ search: local.search, sort: local.sort, take, paging: 1, }); + if (!autoPagination) { + res = paginateArray(res, take, 1); + } local.data = res; if (mode === "form") cloneListFM(res); local.render(); setData(local.data); } else { - local.data = onLoad; - if (mode === "form") cloneListFM(onLoad); + let res = onLoad; + if (!autoPagination) { + res = paginateArray(res, take, 1); + } + local.data = res; + if (mode === "form") cloneListFM(res); local.render(); setData(local.data); } @@ -1013,3 +1016,12 @@ const getPagination = (currentPage: number, totalPages: number) => { 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); +} diff --git a/utils/date.ts b/utils/date.ts index da49f8b..0538258 100644 --- a/utils/date.ts +++ b/utils/date.ts @@ -46,3 +46,10 @@ export const formatTime = (date: string | Date) => { } return "-"; }; + +export const time = (date: string | Date) => { + if (date instanceof Date || typeof date === "string") { + return day(date).format("hh:mm"); + } + return null; +};