Last active
January 19, 2022 12:06
-
-
Save paraparata/022e91c80bccba6b6dfc76a17cb28565 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const Input = React.forwardRef((props, ref) => { | |
| const { | |
| type, | |
| currency, | |
| telOptions, | |
| label, | |
| className, | |
| disabled = false, | |
| prefix, | |
| suffix, | |
| style, | |
| value, | |
| onChange, | |
| ...rest | |
| } = props; | |
| const classNames = cn( | |
| styles.root, | |
| "px-3 py-2 rounded border border-neutral-500", | |
| { | |
| "py-0 text-sm": type === "currency", | |
| "px-0 py-0 border-0": type === "file", | |
| [styles.disabled]: disabled, | |
| [styles["with-label"]]: label, | |
| }, | |
| className | |
| ); | |
| const [activeTel, setActiveTel] = React.useState({ | |
| cc: type === "tel" ? telOptions[0] : "", | |
| ac: "+62", | |
| }); | |
| const [telNumber, setTelNumber] = React.useState(""); | |
| const handleOnChange = (e) => { | |
| if (type === "tel") { | |
| setTelNumber(e.target.value); | |
| onChange(activeTel.ac + telNumber); | |
| } else { | |
| onChange(e); | |
| } | |
| }; | |
| React.useEffect(() => { | |
| if (type === "tel") { | |
| if (value) { | |
| const cc = telOptions.filter( | |
| (item) => item.value === value.substring(0, 3) | |
| )[0]; | |
| const ac = value.substring(3); | |
| setActiveTel({ cc, ac }); | |
| } | |
| } | |
| }, []); | |
| return ( | |
| <div className={classNames} style={style}> | |
| {label && <label>{label}</label>} | |
| <div> | |
| {type === "currency" ? ( | |
| <span className="mr-3 pr-3 py-2 border-r border-neutral-500 font-bold"> | |
| {currency} | |
| </span> | |
| ) : type === "tel" ? ( | |
| <Dropdown | |
| cClassName="rounded-br-none rounded-tr-none" | |
| options={telOptions.map((item) => { | |
| if (typeof item.name !== "object") { | |
| item.name = ( | |
| <span className="flex justify-center items-center gap-2"> | |
| <img className="w-4 h-4" src={item.img} alt={item.name} /> | |
| {item.name} | |
| </span> | |
| ); | |
| } | |
| return item; | |
| })} | |
| placeholder={activeTel.cc.name} | |
| onChange={({ value }) => setActiveTel({ ...activeTel, ac: value })} | |
| /> | |
| ) : ( | |
| prefix | |
| )} | |
| {type === "currency" ? ( | |
| <CurrencyInput | |
| ref={ref} | |
| decimalsLimit={5} | |
| disabled={disabled} | |
| value={value} | |
| onValueChange={onChange} | |
| {...rest} | |
| /> | |
| ) : ( | |
| <input | |
| ref={ref} | |
| type={type} | |
| disabled={disabled} | |
| data-istel={type === "tel" ? true : false} | |
| pattern={type === "tel" ? "[0-9]*" : undefined} | |
| value={value} | |
| onChange={handleOnChange} | |
| {...rest} | |
| /> | |
| )} | |
| {suffix} | |
| </div> | |
| </div> | |
| ); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment