|
1 | | -import { FormStep } from "@egovernments/digit-ui-components"; |
2 | | -import React from "react"; |
| 1 | +import { ToggleSwitch, InputCard, TextBlock, FieldV1 } from "@egovernments/digit-ui-components"; |
| 2 | +import React, { useMemo, useState } from "react"; |
3 | 3 |
|
4 | 4 | const SelectMobileNumber = ({ t, onSelect, showRegisterLink, mobileNumber, onMobileChange, config, canSubmit }) => { |
| 5 | + const [isEmail, setIsEmail] = useState(false); |
| 6 | + const [email, setEmail] = useState(""); |
| 7 | + |
| 8 | + const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; |
| 9 | + |
| 10 | + const isEmailValid = useMemo(() => EMAIL_REGEX.test(email), [email]); |
| 11 | + const isDisabled = useMemo(() => { |
| 12 | + return isEmail ? !(isEmailValid && canSubmit) : !(isMobileValid && canSubmit); |
| 13 | + }, [isEmail, isEmailValid, isMobileValid, canSubmit]); |
| 14 | + |
| 15 | + const handleSubmit = () => { |
| 16 | + if (isEmail) { |
| 17 | + onSelect({ userName: email }); |
| 18 | + } else { |
| 19 | + onSelect({ mobileNumber }); |
| 20 | + } |
| 21 | + }; |
| 22 | + |
| 23 | + const core_mobile_config = window?.globalConfigs?.getConfig("CORE_MOBILE_CONFIGS") || {}; |
| 24 | + const isMobileValid = useMemo(() => mobileNumber && mobileNumber.length === core_mobile_config?.mobileNumberLength, [mobileNumber]); |
| 25 | + |
5 | 26 | return ( |
6 | | - <FormStep |
7 | | - isDisabled={!(mobileNumber.length === 10 && canSubmit)} |
8 | | - onSelect={onSelect} |
9 | | - config={config} |
| 27 | + <InputCard |
10 | 28 | t={t} |
11 | | - componentInFront="+91" |
12 | | - onChange={onMobileChange} |
13 | | - value={mobileNumber} |
14 | | - ></FormStep> |
| 29 | + texts={config?.texts} |
| 30 | + submit |
| 31 | + onNext={handleSubmit} |
| 32 | + isDisable={isDisabled} |
| 33 | + > |
| 34 | + <div> |
| 35 | + <FieldV1 |
| 36 | + withoutLabel={true} |
| 37 | + charCount={true} |
| 38 | + onChange={isEmail ? (e) => setEmail(e.target.value) : onMobileChange} |
| 39 | + placeholder={isEmail ? t("ENTER_EMAIL_PLACEHOLDER") : t("ENTER_MOBILE_PLACEHOLDER")} |
| 40 | + populators={{ |
| 41 | + name : isEmail ? "userName" : "mobileNumber", |
| 42 | + prefix: isEmail ? "" : core_mobile_config?.mobilePrefix |
| 43 | + }} |
| 44 | + props={{ |
| 45 | + fieldStyle: { width: "100%" } |
| 46 | + }} |
| 47 | + required |
| 48 | + type="text" |
| 49 | + value={isEmail ? email : mobileNumber} |
| 50 | + /> |
| 51 | + </div> |
| 52 | + |
| 53 | + <div style={{ display: "flex", alignItems: "center", gap: "1rem", marginBottom: "1.5rem", marginTop:"-24px" }}> |
| 54 | + <TextBlock body={t("CS_MOBILE_OR_EMAIL")}></TextBlock> |
| 55 | + <ToggleSwitch |
| 56 | + name="loginWithEmail" |
| 57 | + style={{ marginTop: "-1rem" }} |
| 58 | + value={isEmail} |
| 59 | + onChange={() => setIsEmail(!isEmail)} |
| 60 | + label={isEmail ? t("CS_USE_MOBILE_INSTEAD") : t("CS_LOGIN_REGISTER_WITH_EMAIL")} |
| 61 | + /> |
| 62 | + </div> |
| 63 | + </InputCard> |
15 | 64 | ); |
16 | 65 | }; |
17 | 66 |
|
|
0 commit comments