Skip to content

Commit 0c2ed2b

Browse files
Fix country flag disappearing issue (GH-109)
2 parents b9a34ec + 0f49fa1 commit 0c2ed2b

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.3.9",
2+
"version": "0.3.10",
33
"name": "antd-phone-input",
44
"description": "Advanced, highly customizable phone input component for Ant Design.",
55
"keywords": [
@@ -78,7 +78,7 @@
7878
"react": ">=16"
7979
},
8080
"dependencies": {
81-
"react-phone-hooks": "^0.1.6"
81+
"react-phone-hooks": "^0.1.11"
8282
},
8383
"devDependencies": {
8484
"@testing-library/react": "^14.0.0",

src/index.tsx

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ const PhoneInput = forwardRef(({
133133
const phoneMetadata = parsePhoneNumber(formattedNumber, countriesList);
134134
setCountryCode(phoneMetadata.isoCode as any);
135135
setValue(formattedNumber);
136+
setQuery("");
136137
handleChange({...phoneMetadata, valid: (strict: boolean) => checkValidity(phoneMetadata, strict)}, event);
137138
}, [countriesList, handleChange, pattern, setValue])
138139

@@ -183,6 +184,18 @@ const PhoneInput = forwardRef(({
183184
setValue(formattedNumber);
184185
}, [countriesList, metadata, onMount, pattern, setValue, value])
185186

187+
const suffixIcon = useMemo(() => {
188+
return enableArrow && (
189+
<span role="img" className="anticon" style={{paddingLeft: 8}}>
190+
<svg className="icon" viewBox="0 0 1024 1024"
191+
focusable="false" fill="currentColor" width="16" height="18">
192+
<path
193+
d="M848 368a48 48 0 0 0-81.312-34.544l-0.016-0.016-254.784 254.784-251.488-251.488a48 48 0 1 0-71.04 64.464l-0.128 0.128 288 288 0.016-0.016a47.84 47.84 0 0 0 34.544 14.688h0.224a47.84 47.84 0 0 0 34.544-14.688l0.016 0.016 288-288-0.016-0.016c8.32-8.624 13.44-20.368 13.44-33.312z"/>
194+
</svg>
195+
</span>
196+
);
197+
}, [enableArrow])
198+
186199
const countriesSelect = useMemo(() => (
187200
<Select
188201
suffixIcon={null}
@@ -206,7 +219,6 @@ const PhoneInput = forwardRef(({
206219
}}
207220
optionLabelProp="label"
208221
dropdownStyle={{minWidth}}
209-
notFoundContent={searchNotFound}
210222
onDropdownVisibleChange={onDropdownVisibleChange}
211223
dropdownRender={(menu) => (
212224
<div className={`${prefixCls}-phone-input-search-wrapper`}>
@@ -218,10 +230,22 @@ const PhoneInput = forwardRef(({
218230
onInput={({target}: any) => setQuery(target.value)}
219231
/>
220232
)}
221-
{menu}
233+
{countriesList.length ? menu : (
234+
<div className="ant-select-item-empty">{searchNotFound}</div>
235+
)}
222236
</div>
223237
)}
224238
>
239+
<Select.Option
240+
children={null}
241+
value={selectValue}
242+
style={{display: "none"}}
243+
key={`${countryCode}_default`}
244+
label={<>
245+
<div className={`flag ${countryCode}`}/>
246+
{suffixIcon}
247+
</>}
248+
/>
225249
{countriesList.map(([iso, name, dial, pattern]) => {
226250
const mask = disableParentheses ? pattern.replace(/[()]/g, "") : pattern;
227251
return (
@@ -230,15 +254,7 @@ const PhoneInput = forwardRef(({
230254
key={`${iso}_${mask}`}
231255
label={<>
232256
<div className={`flag ${iso}`}/>
233-
{enableArrow && (
234-
<span role="img" className="anticon" style={{paddingLeft: 8}}>
235-
<svg className="icon" viewBox="0 0 1024 1024"
236-
focusable="false" fill="currentColor" width="16" height="18">
237-
<path
238-
d="M848 368a48 48 0 0 0-81.312-34.544l-0.016-0.016-254.784 254.784-251.488-251.488a48 48 0 1 0-71.04 64.464l-0.128 0.128 288 288 0.016-0.016a47.84 47.84 0 0 0 34.544 14.688h0.224a47.84 47.84 0 0 0 34.544-14.688l0.016 0.016 288-288-0.016-0.016c8.32-8.624 13.44-20.368 13.44-33.312z"/>
239-
</svg>
240-
</span>
241-
)}
257+
{suffixIcon}
242258
</>}
243259
children={<div className={`${prefixCls}-phone-input-select-item`}>
244260
<div className={`flag ${iso}`}/>
@@ -248,7 +264,7 @@ const PhoneInput = forwardRef(({
248264
)
249265
})}
250266
</Select>
251-
), [selectValue, query, enableArrow, disabled, disableParentheses, disableDropdown, onDropdownVisibleChange, minWidth, searchNotFound, countries, countriesList, setFieldValue, setValue, prefixCls, enableSearch, searchPlaceholder])
267+
), [selectValue, suffixIcon, countryCode, query, disabled, disableParentheses, disableDropdown, onDropdownVisibleChange, minWidth, searchNotFound, countries, countriesList, setFieldValue, setValue, prefixCls, enableSearch, searchPlaceholder])
252268

253269
return (
254270
<div className={`${prefixCls}-phone-input-wrapper`}

tests/antd.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ describe("Checking the basic rendering and functionality", () => {
140140
form.setFieldValue("phone", {
141141
countryCode: 48,
142142
areaCode: "111",
143-
phoneNumber: "1111111",
143+
phoneNumber: "111111",
144144
isoCode: "pl"
145145
});
146146
}
@@ -182,7 +182,7 @@ describe("Checking the basic rendering and functionality", () => {
182182
await new Promise(r => setTimeout(r, 100));
183183
})
184184
assert(!inputHasError(form)); // valid
185-
assert(input.getAttribute("value") === "+48 (111) 111 1111");
185+
assert(input.getAttribute("value") === "+48 (111) 111 111");
186186
})
187187

188188
it("Checking validation with casual form actions", async () => {

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"noImplicitAny": true,
44
"noUnusedLocals": true,
55
"noUnusedParameters": true,
6-
"noImplicitUseStrict": true,
76
"strictNullChecks": true,
87
"module": "esnext",
98
"moduleResolution": "node",

0 commit comments

Comments
 (0)