Skip to content

Commit

Permalink
Fix Phone Number Input (#107)
Browse files Browse the repository at this point in the history
* Fix Phone Number Input

* synthetic fixes
  • Loading branch information
azizhk authored and ritz078 committed Oct 10, 2018
1 parent 7fa8914 commit 28f04d9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 37 deletions.
48 changes: 27 additions & 21 deletions src/components/PhoneNumberInput.tsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,56 @@
import * as React from "react";
import Select from "./Select";
import Input from "./Input";
import Option from "./Option";
import {
PhoneNumberInputProps,
PhoneNumberInputState
} from "./typings/PhoneNumberInput";
import { cx } from "emotion";
import { PhoneNumberInputProps } from "./typings/PhoneNumberInput";
import { wrapper, selectStyle } from "./styles/PhoneNumberInput.styles";

export default class PhoneNumberInput extends React.Component<
PhoneNumberInputProps,
PhoneNumberInputState
PhoneNumberInputProps
> {
onCountrySelect = country_code => {
onCountrySelect = countryCode => {
this.props.onChange({
country_code: country_code,
countryCode,
phone: this.props.phone
});
};

onNumberChange = (value: string) => {
if (!value.match(/^\d*$/)) {
return;
}
this.props.onChange({
country_code: this.props.country_code,
countryCode: this.props.countryCode,
phone: value
});
};

render() {
const { countries, phone, country_code } = this.props;
const {
phone,
countryCode,
className,
selectProps,
inputProps,
placeholder
} = this.props;
return (
<div className={wrapper}>
<div className={cx(wrapper, className)}>
<Select
placeholder="Phone no."
placeholder={placeholder || "Phone No."}
onChange={this.onCountrySelect}
value={country_code}
value={countryCode}
className={selectStyle}
{...selectProps}
>
{countries.map(country => (
<Option
value={country.country_code}
label={`${country.name} (${country.country_code})`}
/>
))}
{this.props.children}
</Select>
<Input onChange={this.onNumberChange} placeholder="" value={phone} />
<Input
onChange={this.onNumberChange}
placeholder=""
value={phone}
{...inputProps}
/>
</div>
);
}
Expand Down
20 changes: 9 additions & 11 deletions src/components/typings/PhoneNumberInput.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
export interface Country {
id: number;
name: string;
country_code: string;
}
import { SelectProps } from "./Select";
import { InputProps } from "./Input";

export interface PhoneNumberInputProps {
country_code: string;
countryCode: string;
phone: string;
onChange: (
{ country_code, phone }: { country_code: string; phone: string }
{ countryCode, phone }: { countryCode: string; phone: string }
) => void;
countries: Country[];
}

export interface PhoneNumberInputState {
country?: Country;
// Optional
className?: string;
selectProps?: Partial<SelectProps>;
inputProps?: Partial<InputProps>;
placeholder?: string;
}
1 change: 0 additions & 1 deletion src/components/typings/Select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export interface SelectProps {
placeholder: string;
required?: boolean;
errorMessage?: string;
children: React.ReactNodeArray;
value?: string;
selected?: Selected;
multiSelect?: boolean;
Expand Down
16 changes: 12 additions & 4 deletions stories/phonenumberinput.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PhoneNumberInput from "../src/components/PhoneNumberInput";
// import { boolean, select, text } from "@storybook/addon-knobs";
// import { css } from "emotion";
import { withState } from "@dump247/storybook-state";
import Option from "../src/components/Option";

// const className = css({
// width: 400
Expand Down Expand Up @@ -920,10 +921,17 @@ storiesOf("PhoneNumberInput", module).add(
"Material",
withState({})(({ store }) => (
<PhoneNumberInput
countries={countries}
country_code={store.state.country_code || "+91"}
countryCode={store.state.countryCode || "+91"}
phone={store.state.phone || ""}
onChange={({ country_code, phone }) => store.set({ country_code, phone })}
/>
onChange={({ countryCode, phone }) => store.set({ countryCode, phone })}
>
{countries.map(country => (
<Option
key={country.id}
value={country.country_code}
label={`${country.name} (${country.country_code})`}
/>
))}
</PhoneNumberInput>
))
);

1 comment on commit 28f04d9

@vercel
Copy link

@vercel vercel bot commented on 28f04d9 Oct 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully aliased the URL https://pebble-kncoyaaebx.now.sh to the following alias.

Please sign in to comment.