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);
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<any> = ({
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
};

View File

@ -27,6 +27,7 @@ import { getNumber } from "@/lib/utils/getNumber";
import { formatMoney } from "../form/field/TypeInput";
export const TableList: React.FC<any> = ({
autoPagination = true,
name,
column,
style = "UI",
@ -115,35 +116,30 @@ export const TableList: React.FC<any> = ({
</>
);
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 {
if (!autoPagination) {
res = paginateArray(res, take, 1);
}
local.data = res;
cloneListFM(res);
local.render();
setData(res);
setTimeout(() => {
toast.dismiss();
}, 2000);
}
}, 1000);
}
},
});
@ -203,19 +199,26 @@ export const TableList: React.FC<any> = ({
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);
}

View File

@ -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;
};