weight filed made manditory with decimlas

This commit is contained in:
naveen prabhu 2026-05-07 15:10:21 +05:30
parent 6dc3a0b2e8
commit 59a9208698

View File

@ -20,6 +20,7 @@ const createEmptyCodeForm = () => ({
code: '', code: '',
product: '', product: '',
unit: '', unit: '',
weight: '',
status: 'Active', status: 'Active',
is_active: true, is_active: true,
createdBy: '', createdBy: '',
@ -28,6 +29,8 @@ const createEmptyCodeForm = () => ({
description: '' description: ''
}); });
const WEIGHT_REGEX = /^\d{1,2}\.\d{1,10}$/;
// Toast notification component // Toast notification component
const Toast = ({ message, type = 'success', onClose, position = 'page' }) => { const Toast = ({ message, type = 'success', onClose, position = 'page' }) => {
React.useEffect(() => { React.useEffect(() => {
@ -896,19 +899,14 @@ const IsicHsCodes = () => {
const handleFormChange = (field) => (event) => { const handleFormChange = (field) => (event) => {
const value = event?.target?.value ?? event; const value = event?.target?.value ?? event;
if (field === "weight") { if (field === "weight") {
// Allow only numbers + optional decimal if (value === '') {
if (!/^\d*\.?\d*$/.test(value)) return; setForm((prev) => ({ ...prev, [field]: value }));
return;
}
// Split integer & decimal parts if (!/^\d{0,2}(\.\d{0,10})?$/.test(value)) return;
const [intPart = "", decPart = ""] = value.split("."); }
if (intPart.length > 2) return;
if (decPart.length > 10) return;
}
setForm((prev) => ({ ...prev, [field]: value })); setForm((prev) => ({ ...prev, [field]: value }));
}; };
@ -987,13 +985,20 @@ const handleExportCSV = () => {
const handleSaveForm = async () => { const handleSaveForm = async () => {
if (!form.code || !form.product) { if (!form.code || !form.product || !form.unit || !form.weight) {
const errorMsg = 'Please fill in all required fields'; const errorMsg = 'Please fill in all required fields';
console.error(errorMsg); console.error(errorMsg);
setToast({ show: true, message: errorMsg, type: 'error' }); setToast({ show: true, message: errorMsg, type: 'error' });
return; return;
} }
if (!WEIGHT_REGEX.test(form.weight)) {
const errorMsg = 'Weight must be a decimal value with up to 2 digits before the decimal and up to 10 digits after it';
console.error(errorMsg);
setToast({ show: true, message: errorMsg, type: 'error' });
return;
}
try { try {
setError(''); setError('');
@ -1625,19 +1630,21 @@ const handleExportCSV = () => {
)} )}
</div> </div>
{/* weight filed */} {/* weight filed */}
{/* <div> <div>
<label className="block text-sm font-medium text-[#374151] mb-1"> <label className="block text-sm font-medium text-[#374151] mb-1">
Weight Weight <span className="text-[#EF4444]">*</span>
</label> </label>
<input <input
type="text" type="text"
inputMode="decimal"
value={form.weight || ''} value={form.weight || ''}
onChange={handleFormChange("weight")} onChange={handleFormChange("weight")}
disabled={modalMode === "view"} disabled={modalMode === "view"}
className="w-full px-3 py-2 text-sm border border-[#CBA344] rounded-md focus:outline-none focus:ring-1 focus:ring-[#92722A] focus:border-[#92722A]" className="w-full px-3 py-2 text-sm border border-[#CBA344] rounded-md focus:outline-none focus:ring-1 focus:ring-[#92722A] focus:border-[#92722A]"
placeholder="Enter weight"
maxLength={13}
/> />
</div> */} </div>
</div> </div>
<div className="mt-4"> <div className="mt-4">
@ -1670,9 +1677,9 @@ const handleExportCSV = () => {
<button <button
type="button" type="button"
onClick={handleSaveForm} onClick={handleSaveForm}
disabled={!form.code || !form.product || !form.unit} disabled={!form.code || !form.product || !form.unit || !WEIGHT_REGEX.test(form.weight || '')}
className={`h-10 px-6 text-sm font-medium text-white rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-[#92722A] ${ className={`h-10 px-6 text-sm font-medium text-white rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-[#92722A] ${
!form.code || !form.product || !form.unit !form.code || !form.product || !form.unit || !WEIGHT_REGEX.test(form.weight || '')
? 'bg-gray-300 cursor-not-allowed' ? 'bg-gray-300 cursor-not-allowed'
: 'bg-[#92722A] hover:bg-[#7a5f22]' : 'bg-[#92722A] hover:bg-[#7a5f22]'
}`} }`}
@ -1737,4 +1744,4 @@ const handleExportCSV = () => {
); );
}; };
export default IsicHsCodes; export default IsicHsCodes;