Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onlyCountries cannot be passed a responsive data #659

Open
shiwofeiwo opened this issue Nov 20, 2023 · 0 comments
Open

onlyCountries cannot be passed a responsive data #659

shiwofeiwo opened this issue Nov 20, 2023 · 0 comments

Comments

@shiwofeiwo
Copy link

import PhoneInput from "react-phone-input-2";
import {PhoneInputProps} from "react-phone-input-2";
import React, {createElement, HTMLAttributes, useEffect, useState} from "react";

// @ts-ignore
interface MyPhoneInputProps extends PhoneInputProps, HTMLAttributes<HTMLElement> {
  myOnlyCountries?: string
}

const MyPhoneInput: React.FC<MyPhoneInputProps> = function MyPhoneInput(props) {
  const {
    value,
    onChange,
    myOnlyCountries,
    ...rest
  } = props;
  console.log(props)

  const [phone, setPhone] = useState('');
  const [only, setOnly] = useState(['af', 've']);

  useEffect(() => {
    if (typeof myOnlyCountries === 'string')
      setOnly(['cn', 'af'])
  }, [myOnlyCountries])

  const handlePhoneChange = (e) => {
    setPhone(e)
  }

  return <PhoneInput value={value || phone}
                     onChange={onChange || handlePhoneChange}

                     onlyCountries={only}

                     {...rest} />
}

MyPhoneInput.displayName = 'PhoneInput';
export default MyPhoneInput;

When I tried to pass a state to onlyCountries ( in useEffect ), it didn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant