From b8141064bcce99574b94506f9e3028425759158d Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 30 Aug 2024 10:56:20 +0700 Subject: input npwp by template --- .../modules/register/components/FormBisnis.tsx | 117 +++++++++++++++++---- 1 file changed, 99 insertions(+), 18 deletions(-) (limited to 'src-migrate/modules/register/components') diff --git a/src-migrate/modules/register/components/FormBisnis.tsx b/src-migrate/modules/register/components/FormBisnis.tsx index a278097b..6ab12776 100644 --- a/src-migrate/modules/register/components/FormBisnis.tsx +++ b/src-migrate/modules/register/components/FormBisnis.tsx @@ -14,6 +14,8 @@ import { toast } from 'react-hot-toast'; import { EyeIcon } from '@heroicons/react/24/outline'; +import BottomPopup from '@/core/components/elements/Popup/BottomPopup'; +import Image from 'next/image' interface FormProps { type: string; @@ -42,6 +44,11 @@ const form: React.FC = ({ type, required, isPKP }) => { const { control, watch, setValue } = useForm(); const [selectedCategory, setSelectedCategory] = useState(''); const [isChekBox, setIsChekBox] = useState(false); + const [isExample, setIsExample] = useState(false); + // Inside your component + const [formattedNpwp, setFormattedNpwp] = useState(""); // State for formatted NPWP + const [unformattedNpwp, setUnformattedNpwp] = useState(""); // State for unformatted NPWP + const [industries, setIndustries] = useState([]); const [companyTypes, setCompanyTypes] = useState([]); @@ -90,9 +97,58 @@ const form: React.FC = ({ type, required, isPKP }) => { validate(); }; + const handleInputChangeNpwp = (event: ChangeEvent) => { + const { name, value } = event.target; + updateForm('type_acc',`business`) + updateForm('is_pkp',`${isPKP}`) + updateForm('npwp', value); + validate(); + }; + const handleChange = (event: ChangeEvent) => { setIsChekBox(!isChekBox) }; + + const formatNpwp = (value: string) => { + try { + const cleaned = ("" + value).replace(/\D/g, ""); + // const match = cleaned.match(/(\d{0,2})?(\d{0,3})?(\d{0,3})?(\d{0,1})?(\d{0,3})?(\d{0,3})$/); + console.log("panjang",form?.npwp?.length) + console.log("panjang cleaned",cleaned.length) + let match + if(cleaned.length <= 15){ + match = cleaned.match(/(\d{0,2})?(\d{0,3})?(\d{0,3})?(\d{0,1})?(\d{0,3})?(\d{0,3})$/); + }else{ + match = cleaned.match(/(\d{0,3})?(\d{0,3})?(\d{0,3})?(\d{0,1})?(\d{0,3})?(\d{0,3})$/); + } + + + if (match) { + return [ + match[1], + match[2] ? "." : "", + match[2], + match[3] ? "." : "", + match[3], + match[4] ? "." : "", + match[4], + match[5] ? "-" : "", + match[5], + match[6] ? "." : "", + match[6], + ].join(""); + } + + // If match is null, return the original cleaned string or handle as needed + return cleaned; + + } catch (error) { + // Handle error or return a default value + console.error("Error formatting NPWP:", error); + return value; + } + }; + useEffect(() => { if (isChekBox) { @@ -172,7 +228,26 @@ const form: React.FC = ({ type, required, isPKP }) => { break; } }; + console.log("form",form) return ( + <> + setIsExample(false)} + > +
+ Contoh SPPKP +
+
@@ -319,25 +394,28 @@ const form: React.FC = ({ type, required, isPKP }) => {
- + { if (!required) { - const value = e.target.value.replace(/\D/g, ''); // Remove non-digit characters - if (value.length <= 16) { - handleInputChange(e); - } + const unformatted = e.target.value.replace(/\D/g, ""); // Remove all non-digit characters + const formattedValue = formatNpwp(unformatted); // Format the value + setUnformattedNpwp(unformatted); // Store unformatted value + setFormattedNpwp(formattedValue); // Store formatted value + handleInputChangeNpwp({ ...e, target: { ...e.target, value: unformatted } }); // Update form state with unformatted value } }} aria-invalid={!required && !!errors.npwp} @@ -347,12 +425,14 @@ const form: React.FC = ({ type, required, isPKP }) => {
-
} @@ -362,9 +442,9 @@ const form: React.FC = ({ type, required, isPKP }) => { name='sppkp' className={`form-input mt-3 ${required ? 'cursor-no-drop' : ''}`} disabled={required} - contentEditable={required}S + contentEditable={required} readOnly={required} - placeholder='000.000.000.0-000.000' + placeholder='X-XXXPKP/WJPXXX/XX.XXXX/XXXX' onChange={handleInputChange} value={!required ? form.sppkp : ''} aria-invalid={!required && !!errors.sppkp} @@ -409,6 +489,7 @@ const form: React.FC = ({ type, required, isPKP }) => { {isPKP && !required && !!errors.sppkp_document && {errors.sppkp_document}} + ) } -- cgit v1.2.3