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

Add ref support to PhoneInput component #169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface PhoneInputState {
countryCode: CountryCode;
disabled: boolean;
}
export default class PhoneInput extends Component<
class PhoneInput extends Component<
PhoneInputProps,
PhoneInputState
> {
Expand Down Expand Up @@ -304,4 +304,10 @@ export default class PhoneInput extends Component<
render(): JSX.Element;
}

// Define the ref-forwarding component type
export type PhoneInput = ForwardRefExoticComponent<PhoneInputProps & RefAttributes<TextInput>>;

const PhoneInput: PhoneInput = PhoneInputComponent as any;
export default PhoneInput;

export function isValidNumber(number: string, countryCode: CountryCode ): boolean;
18 changes: 17 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const dropDown =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAi0lEQVRYR+3WuQ6AIBRE0eHL1T83FBqU5S1szdiY2NyTKcCAzU/Y3AcBXIALcIF0gRPAsehgugDEXnYQrUC88RIgfpuJ+MRrgFmILN4CjEYU4xJgFKIa1wB6Ec24FuBFiHELwIpQxa0ALUId9wAkhCnuBdQQ5ngP4I9wxXsBDyJ9m+8y/g9wAS7ABW4giBshQZji3AAAAABJRU5ErkJggg==";
const phoneUtil = PhoneNumberUtil.getInstance();

export default class PhoneInput extends PureComponent {
class PhoneInput extends PureComponent {
constructor(props) {
super(props);
this.state = {
Expand All @@ -28,6 +28,7 @@ export default class PhoneInput extends PureComponent {
countryCode: props.defaultCode ? props.defaultCode : "IN",
disabled: props.disabled || false,
};
this.inputRef = React.createRef();
}

static getDerivedStateFromProps(nextProps, prevState) {
Expand Down Expand Up @@ -220,6 +221,7 @@ export default class PhoneInput extends PureComponent {
>{`+${code}`}</Text>
)}
<TextInput
ref={this.inputRef}
style={[styles.numberText, textInputStyle ? textInputStyle : {}]}
placeholder={placeholder ? placeholder : "Phone Number"}
onChangeText={this.onChangeText}
Expand All @@ -238,6 +240,20 @@ export default class PhoneInput extends PureComponent {
}
}

const PhoneInputWithRef = React.forwardRef((props, ref) => (
<PhoneInput
{...props}
ref={(phoneInputRef) => {
// Forward the ref to the TextInput
if (ref) {
ref.current = phoneInputRef ? phoneInputRef.inputRef.current : null;
}
}}
/>
));

export default PhoneInputWithRef;

export const isValidNumber = (number, countryCode) => {
try {
const parsedNumber = phoneUtil.parse(number, countryCode);
Expand Down