Skip to content

Commit

Permalink
Remove unused phone indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
jducro committed Dec 4, 2024
1 parent e6d3889 commit 5feca12
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 105 deletions.
1 change: 0 additions & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"react-textarea-autosize": "8.3.3",
"style": "^0.0.3",
"url-polyfill": "^1.0.13",
"world-countries": "5.0.0",
"yup": "^1.2.0"
},
"scripts": {
Expand Down
100 changes: 1 addition & 99 deletions packages/components/src/Components/Inputs/Tel.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react';
import ReactSelect from 'react-select';
import worldCountries from 'world-countries/dist/countries.json';
import classNames from 'classnames';
import { AsYouType, CountryCode } from 'libphonenumber-js';
import Field, { FieldProps, FieldState } from '../Field';
Expand All @@ -21,8 +20,6 @@ interface TelState extends FieldState {
}

class Tel extends Field<TelProps, TelState> {
private countries;

static defaultProps = {
...Field.defaultProps,
excludeCountries: [],
Expand All @@ -35,105 +32,10 @@ class Tel extends Field<TelProps, TelState> {
constructor(props) {
super(props);
this.type = 'tel';
if (props.withIndicator) {
this.className = 'dp-pc_field--tel';
this.state = {
country: null,
indicator: '',
nationalNumber: '',
};
}
}

handleIndicator = (value) => {
this.setState({
country: value.country,
indicator: value.value
});
};

handleNationalNumber = (e) => {
const formatted = new AsYouType(this.state.country).input(`+${this.state.indicator}${e.target.value}`);
const regex = new RegExp(`^\\+${this.state.indicator}\\s*`);
const nationalNumber = formatted.replace(regex, '');
this.setState({
nationalNumber
});
};

static filterIndicator(value, filter) {
const nameRegex = new RegExp(filter, 'i');
return value.data.countryName.match(nameRegex) || value.data.value.match(filter);
}

loadCountries = () => {
const { onlyCountries, excludeCountries } = this.props;
if (!this.countries) {
this.countries = [];
Array.from(worldCountries)
.filter((country) => {
if (onlyCountries.length) {
return onlyCountries.includes(country.cca2);
}
if (excludeCountries.length) {
return !excludeCountries.includes(country.cca2);
}
return true;
})
.forEach((country) => {
country.idd.suffixes.forEach((code) => {
this.countries.push({
value: code,
country: country.cca2,
label: (
<span>(+{code})
<span className="react-select__description">
{country.flag} {country.name.common}
</span>
</span>),
countryName: country.name.common
});
});
});
}
return this.countries;
};

renderField = ({ form }) => {
const {
name,
className,
placeholder,
} = this.props;
if (!this.props.withIndicator) {
return super.renderField(form);
}
return <>
<ReactSelect
key="indicator"
value={
this.loadCountries().find(indicator =>
indicator.country === this.state.country
&& indicator.value === this.state.indicator)}
name={name}
isClearable={false}
filterOption={Tel.filterIndicator}
components={{ SelectContainer }}
onChange={this.handleIndicator}
onBlur={() => form.setFieldTouched(name, true)}
options={this.loadCountries()}
classNamePrefix="react-select"
placeholder="(1234)"
/>,
<input
key="national_number"
type="tel"
value={this.state.nationalNumber}
onChange={this.handleNationalNumber}
placeholder={placeholder}
className={classNames('dp-pc_input', className)}
/>
</>;
return super.renderField(form);
}
}

Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17249,11 +17249,6 @@ wordwrap@^1.0.0:
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
integrity sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==

[email protected]:
version "5.0.0"
resolved "https://registry.yarnpkg.com/world-countries/-/world-countries-5.0.0.tgz#6f75ebcce3d5224d84e9117eaf0d75a7726b6501"
integrity sha512-wAfOT9Y5i/xnxNOdKJKXdOCw9Q3yQLahBUeuRol+s+o20F6h2a4tLEbJ1lBCYwEQ30Sf9Meqeipk1gib3YwF5w==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
Expand Down

0 comments on commit 5feca12

Please sign in to comment.