Skip to content

Commit

Permalink
setNumber now triggers input event
Browse files Browse the repository at this point in the history
  • Loading branch information
jackocnr committed May 23, 2024
1 parent 0e22110 commit 1444240
Show file tree
Hide file tree
Showing 18 changed files with 32,737 additions and 67 deletions.
1 change: 1 addition & 0 deletions build/js/intlTelInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -2734,6 +2734,7 @@ var factoryOutput = (() => {
if (countryChanged) {
this._triggerCountryChange();
}
this._trigger("input");
}
//* Set the placeholder number typ
setPlaceholderNumberType(type) {
Expand Down
2 changes: 1 addition & 1 deletion build/js/intlTelInput.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions build/js/intlTelInputWithUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2733,6 +2733,7 @@ var factoryOutput = (() => {
if (countryChanged) {
this._triggerCountryChange();
}
this._trigger("input");
}
//* Set the placeholder number typ
setPlaceholderNumberType(type) {
Expand Down
2 changes: 1 addition & 1 deletion build/js/intlTelInputWithUtils.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion grunt/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = function(grunt) {
tasks: "build:translations",
},
react: {
files: ["react/src/intl-tel-input/react.tsx", "react/demo/ValidationApp.tsx", "react/demo/SimpleApp.tsx"],
files: ["react/src/intl-tel-input/react.tsx", "react/demo/ValidationApp.tsx", "react/demo/SimpleApp.tsx", "react/demo/SetNumberApp.tsx"],
tasks: "react"
},
pluginCss: {
Expand Down
7 changes: 7 additions & 0 deletions react/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,10 @@ build({
entryPoints: ["react/demo/ValidationApp.tsx"],
outfile: "react/demo/validation-bundle.js",
});

//* Set Number demo app
build({
...demoShared,
entryPoints: ["react/demo/SetNumberApp.tsx"],
outfile: "react/demo/set-number-bundle.js",
});
10 changes: 2 additions & 8 deletions react/build/IntlTelInput.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2729,6 +2729,7 @@ var Iti = class {
if (countryChanged) {
this._triggerCountryChange();
}
this._trigger("input");
}
//* Set the placeholder number typ
setPlaceholderNumberType(type) {
Expand Down Expand Up @@ -2801,14 +2802,7 @@ var IntlTelInput = (0, import_react.forwardRef)(function IntlTelInput2({
const inputRef = (0, import_react.useRef)(null);
const itiRef = (0, import_react.useRef)(null);
(0, import_react.useImperativeHandle)(ref, () => ({
getInstance: () => ({
setCountry: (country) => itiRef.current?.setCountry(country),
// override setNumber to also call update, to trigger onChangeNumber etc
setNumber: (num) => {
itiRef.current?.setNumber(num);
update();
}
}),
getInstance: () => itiRef.current,
getInput: () => inputRef.current
}));
const update = () => {
Expand Down
10 changes: 2 additions & 8 deletions react/build/IntlTelInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -2693,6 +2693,7 @@ var Iti = class {
if (countryChanged) {
this._triggerCountryChange();
}
this._trigger("input");
}
//* Set the placeholder number typ
setPlaceholderNumberType(type) {
Expand Down Expand Up @@ -2765,14 +2766,7 @@ var IntlTelInput = forwardRef(function IntlTelInput2({
const inputRef = useRef(null);
const itiRef = useRef(null);
useImperativeHandle(ref, () => ({
getInstance: () => ({
setCountry: (country) => itiRef.current?.setCountry(country),
// override setNumber to also call update, to trigger onChangeNumber etc
setNumber: (num) => {
itiRef.current?.setNumber(num);
update();
}
}),
getInstance: () => itiRef.current,
getInput: () => inputRef.current
}));
const update = () => {
Expand Down
10 changes: 2 additions & 8 deletions react/build/IntlTelInputWithUtils.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2729,6 +2729,7 @@ var Iti = class {
if (countryChanged) {
this._triggerCountryChange();
}
this._trigger("input");
}
//* Set the placeholder number typ
setPlaceholderNumberType(type) {
Expand Down Expand Up @@ -8999,14 +9000,7 @@ var IntlTelInput = (0, import_react.forwardRef)(function IntlTelInput2({
const inputRef = (0, import_react.useRef)(null);
const itiRef = (0, import_react.useRef)(null);
(0, import_react.useImperativeHandle)(ref, () => ({
getInstance: () => ({
setCountry: (country) => itiRef.current?.setCountry(country),
// override setNumber to also call update, to trigger onChangeNumber etc
setNumber: (num) => {
itiRef.current?.setNumber(num);
update();
}
}),
getInstance: () => itiRef.current,
getInput: () => inputRef.current
}));
const update = () => {
Expand Down
10 changes: 2 additions & 8 deletions react/build/IntlTelInputWithUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2693,6 +2693,7 @@ var Iti = class {
if (countryChanged) {
this._triggerCountryChange();
}
this._trigger("input");
}
//* Set the placeholder number typ
setPlaceholderNumberType(type) {
Expand Down Expand Up @@ -8963,14 +8964,7 @@ var IntlTelInput = forwardRef(function IntlTelInput2({
const inputRef = useRef(null);
const itiRef = useRef(null);
useImperativeHandle(ref, () => ({
getInstance: () => ({
setCountry: (country) => itiRef.current?.setCountry(country),
// override setNumber to also call update, to trigger onChangeNumber etc
setNumber: (num) => {
itiRef.current?.setNumber(num);
update();
}
}),
getInstance: () => itiRef.current,
getInput: () => inputRef.current
}));
const update = () => {
Expand Down
55 changes: 55 additions & 0 deletions react/demo/SetNumberApp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React, { useState, ReactElement, useRef } from "react";
import { createRoot } from "react-dom/client";
import IntlTelInput from "../src/intl-tel-input/reactWithUtils";

const errorMap = [
"Invalid number",
"Invalid country code",
"Too short",
"Too long",
"Invalid number",
];

const App = (): ReactElement => {
const ref = useRef(null);
const [isValid, setIsValid] = useState<boolean | null>(null);
const [number, setNumber] = useState<string | null>(null);
const [errorCode, setErrorCode] = useState<number | null>(null);
const [notice, setNotice] = useState<string | null>(null);

const handleSetNumber = (): void => {
ref.current?.getInstance().setNumber("+14155552671");
};

const handleSubmit = (): void => {
if (isValid) {
setNotice(`Valid number: ${number}`);
} else {
const errorMessage = errorMap[errorCode || 0] || "Invalid number";
setNotice(`Error: ${errorMessage}`);
}
};

return (
<form>
<IntlTelInput
ref={ref}
onChangeNumber={setNumber}
onChangeValidity={setIsValid}
onChangeErrorCode={setErrorCode}
initOptions={{
initialCountry: "us",
}}
/>
<button className="button" type="button" onClick={handleSetNumber}>Set Number</button>
<button className="button" type="button" onClick={handleSubmit}>Validate</button>
{notice && <div className="notice">{notice}</div>}
</form>
);
};

const container = document.getElementById("app");
if (container) {
const root = createRoot(container);
root.render(<App />);
}
Loading

0 comments on commit 1444240

Please sign in to comment.