-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add international phone number examples
- Loading branch information
1 parent
0beec1b
commit 2f8c1c9
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
...s/core/stories/patterns/international-phone-number/international-phone-number.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { | ||
Dropdown, | ||
FormField, | ||
FormFieldLabel, | ||
Input, | ||
Option, | ||
StackLayout, | ||
Text, | ||
} from "@salt-ds/core"; | ||
import "@salt-ds/countries/saltCountries.css"; | ||
import type { Meta, StoryFn } from "@storybook/react"; | ||
import { useState } from "react"; | ||
|
||
export default { | ||
title: "Patterns/International Phone Number", | ||
} as Meta; | ||
|
||
type Country = { name: string; code: string; countryCode: string }; | ||
|
||
const countryPhonePrefixes: Country[] = [ | ||
{ name: "United States", code: "+1", countryCode: "US" }, | ||
{ name: "Canada", code: "+1", countryCode: "CA" }, | ||
{ name: "United Kingdom", code: "+44", countryCode: "GB" }, | ||
{ name: "Germany", code: "+49", countryCode: "DE" }, | ||
{ name: "France", code: "+33", countryCode: "FR" }, | ||
{ name: "Australia", code: "+61", countryCode: "AU" }, | ||
{ name: "India", code: "+91", countryCode: "IN" }, | ||
{ name: "China", code: "+86", countryCode: "CN" }, | ||
{ name: "Japan", code: "+81", countryCode: "JP" }, | ||
{ name: "Brazil", code: "+55", countryCode: "BR" }, | ||
{ name: "Mexico", code: "+52", countryCode: "MX" }, | ||
{ name: "South Africa", code: "+27", countryCode: "ZA" }, | ||
{ name: "Russia", code: "+7", countryCode: "RU" }, | ||
{ name: "Italy", code: "+39", countryCode: "IT" }, | ||
{ name: "Spain", code: "+34", countryCode: "ES" }, | ||
{ name: "Netherlands", code: "+31", countryCode: "NL" }, | ||
{ name: "Sweden", code: "+46", countryCode: "SE" }, | ||
{ name: "Switzerland", code: "+41", countryCode: "CH" }, | ||
{ name: "Argentina", code: "+54", countryCode: "AR" }, | ||
{ name: "South Korea", code: "+82", countryCode: "KR" }, | ||
]; | ||
|
||
const CountryIcon = ({ code }: { code: string }) => ( | ||
<div | ||
className={`saltCountry-${code}`} | ||
style={{ | ||
height: "calc(var(--salt-size-base) - var(--salt-spacing-150))", | ||
width: "calc(var(--salt-size-base) - var(--salt-spacing-150))", | ||
}} | ||
/> | ||
); | ||
|
||
const InternationalPhoneNumberTemplate: StoryFn<{ | ||
direction?: "row" | "column"; | ||
}> = ({ direction }) => { | ||
const [selectedCountry, setSelectedCountry] = useState<Country>( | ||
countryPhonePrefixes[0], | ||
); | ||
|
||
return ( | ||
<StackLayout direction={direction}> | ||
<FormField> | ||
<FormFieldLabel>Country</FormFieldLabel> | ||
<Dropdown<Country> | ||
valueToString={(country) => `${country.name} (${country.code})`} | ||
startAdornment={<CountryIcon code={selectedCountry.countryCode} />} | ||
selected={[selectedCountry]} | ||
onSelectionChange={(_, value) => setSelectedCountry(value[0])} | ||
> | ||
{countryPhonePrefixes | ||
.sort((a, b) => a.name.localeCompare(b.name)) | ||
.map((country) => ( | ||
<Option key={country.name} value={country}> | ||
<CountryIcon code={country.countryCode} /> {country.name} ( | ||
{country.code}) | ||
</Option> | ||
))} | ||
</Dropdown> | ||
</FormField> | ||
<FormField> | ||
<FormFieldLabel>Phone number</FormFieldLabel> | ||
<Input startAdornment={<Text>{selectedCountry.code}</Text>} /> | ||
</FormField> | ||
</StackLayout> | ||
); | ||
}; | ||
|
||
export const Column = InternationalPhoneNumberTemplate.bind({}); | ||
Column.args = { | ||
direction: "column", | ||
}; | ||
|
||
export const Row = InternationalPhoneNumberTemplate.bind({}); | ||
Row.args = { | ||
direction: "row", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters