-
-
Notifications
You must be signed in to change notification settings - Fork 460
feat/phoneInput: added phoneInput Form Feature #1334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
34b3732
709b3be
6e2e16f
3dd1832
d9752f7
45be937
cff0a50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
title: React Phone Input - Flowbite | ||
description: Use the phone number input component from Flowbite to set a phone number inside a form field and use sizes | ||
--- | ||
|
||
The phone number input component from Flowbite React can be used to set a phone number inside a form field by using the native type="tel" attribute. | ||
|
||
The examples are built with the utility classes from Tailwind CSS and they are fully responsive, dark mode compatible and support RTL layouts and can be used for any type of web project. | ||
dhavalveera marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
To start using the component make sure that you have imported it from Flowbite React: | ||
dhavalveera marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```jsx | ||
import { PhoneInput } from "flowbite-react"; | ||
``` | ||
|
||
## Default phone input | ||
|
||
Get started with the following phone input example with default type as `type="tel"`. | ||
|
||
<Example name="phoneInput.root" /> | ||
|
||
## Example with Helper Text | ||
|
||
<Example name="phoneInput.withHelperText" /> | ||
|
||
## Example with Icon on the right | ||
|
||
<Example name="phoneInput.withRightIcon" /> | ||
|
||
## Theme | ||
|
||
To learn more about how to customize the appearance of components, please see the [Theme docs](/docs/customize/theme). | ||
dhavalveera marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<Theme name="phoneInput" /> | ||
|
||
## References | ||
|
||
- [Flowbite Phone Input](https://flowbite.com/docs/forms/phone-input/) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { root } from "./phoneInput.root"; | ||
export { withHelperText } from "./phoneInput.withHelperText"; | ||
export { withRightIcon } from "./phoneInput.withRightIcon"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { PhoneInput } from "flowbite-react"; | ||
import type { CodeData } from "~/components/code-demo"; | ||
|
||
const code = ` | ||
"use client"; | ||
|
||
import { PhoneInput } from "flowbite-react"; | ||
|
||
function Component() { | ||
return ( | ||
dhavalveera marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<form className="max-w-sm mx-auto"> | ||
<PhoneInput /> | ||
</form> | ||
) | ||
} | ||
`; | ||
|
||
const codeRSC = ` | ||
function Component() { | ||
return ( | ||
<form className="max-w-sm mx-auto"> | ||
<PhoneInput /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not same content There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry @SutuSebastian Sir, I am not clear on this, need little bit more info on this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SutuSebastian Sorry for follow up, awaiting for the clarification for this review. |
||
</form> | ||
) | ||
} | ||
`; | ||
|
||
function Component() { | ||
return ( | ||
<form className="mx-auto max-w-sm"> | ||
<PhoneInput pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" required placeholder="123-456-7890" /> | ||
</form> | ||
); | ||
} | ||
|
||
export const root: CodeData = { | ||
type: "single", | ||
code: [ | ||
{ | ||
fileName: "client", | ||
language: "tsx", | ||
code, | ||
}, | ||
{ | ||
fileName: "server", | ||
language: "tsx", | ||
code: codeRSC, | ||
}, | ||
], | ||
githubSlug: "/phoneInput/phoneInput.root.tsx", | ||
component: <Component />, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { PhoneInput } from "flowbite-react"; | ||
import type { CodeData } from "~/components/code-demo"; | ||
|
||
const code = ` | ||
"use client"; | ||
|
||
import { PhoneInput } from "flowbite-react"; | ||
|
||
function Component() { | ||
return ( | ||
<form className="max-w-sm mx-auto"> | ||
<PhoneInput helperText="Select a phone number that matches the format." /> | ||
</form> | ||
) | ||
} | ||
`; | ||
|
||
const codeRSC = ` | ||
function Component() { | ||
return ( | ||
<form className="max-w-sm mx-auto"> | ||
<PhoneInput helperText="Select a phone number that matches the format." /> | ||
</form> | ||
) | ||
} | ||
`; | ||
|
||
function Component() { | ||
return ( | ||
<form className="mx-auto max-w-sm"> | ||
<PhoneInput | ||
pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" | ||
required | ||
placeholder="123-456-7890" | ||
helperText="Select a phone number that matches the format." | ||
/> | ||
</form> | ||
); | ||
} | ||
|
||
export const withHelperText: CodeData = { | ||
type: "single", | ||
code: [ | ||
{ | ||
fileName: "client", | ||
language: "tsx", | ||
code, | ||
}, | ||
{ | ||
fileName: "server", | ||
language: "tsx", | ||
code: codeRSC, | ||
}, | ||
], | ||
githubSlug: "/phoneInput/phoneInput.withHelperText.tsx", | ||
component: <Component />, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { PhoneInput } from "flowbite-react"; | ||
import { FaPhoneAlt } from "react-icons/fa"; | ||
import type { CodeData } from "~/components/code-demo"; | ||
|
||
const code = ` | ||
"use client"; | ||
|
||
import { PhoneInput } from "flowbite-react"; | ||
|
||
function Component() { | ||
return ( | ||
<form className="max-w-sm mx-auto"> | ||
<PhoneInput helperText="Select a phone number that matches the format." rightIcon={FaPhoneAlt}/> | ||
dhavalveera marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</form> | ||
) | ||
} | ||
`; | ||
|
||
const codeRSC = ` | ||
function Component() { | ||
return ( | ||
<form className="max-w-sm mx-auto"> | ||
<PhoneInput helperText="Select a phone number that matches the format." rightIcon={FaPhoneAlt} /> | ||
</form> | ||
) | ||
} | ||
`; | ||
|
||
function Component() { | ||
return ( | ||
<form className="mx-auto max-w-sm"> | ||
<PhoneInput | ||
pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" | ||
required | ||
placeholder="123-456-7890" | ||
helperText="Select a phone number that matches the format." | ||
rightIcon={FaPhoneAlt} | ||
/> | ||
</form> | ||
); | ||
} | ||
|
||
export const withRightIcon: CodeData = { | ||
type: "single", | ||
code: [ | ||
{ | ||
fileName: "client", | ||
language: "tsx", | ||
code, | ||
}, | ||
{ | ||
fileName: "server", | ||
language: "tsx", | ||
code: codeRSC, | ||
}, | ||
], | ||
githubSlug: "/phoneInput/phoneInput.withRightIcon.tsx", | ||
component: <Component />, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { render } from "@testing-library/react"; | ||
import { FaPhoneAlt } from "react-icons/fa"; | ||
import { describe, expect, it } from "vitest"; | ||
import { PhoneInput } from "./PhoneInput"; | ||
|
||
describe.concurrent("Components / PhoneInput", () => { | ||
describe.concurrent("A11y", () => { | ||
it('should have `role="textbox"` by default', () => { | ||
const textInput = render(<PhoneInput />).getByRole("textbox"); | ||
|
||
expect(textInput).toBeInTheDocument(); | ||
}); | ||
|
||
it("should have Left Icon", () => { | ||
const leftPage = render(<PhoneInput />).getAllByTestId("left-icon"); | ||
|
||
leftPage.forEach((leftIcon) => { | ||
expect(leftIcon).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it("should have Right Icon", () => { | ||
const rightPage = render(<PhoneInput rightIcon={FaPhoneAlt} />).getAllByTestId("right-icon"); | ||
|
||
rightPage.forEach((rightIcon) => { | ||
expect(rightIcon).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { Meta, StoryFn } from "@storybook/react"; | ||
import { FaPhoneAlt } from "react-icons/fa"; | ||
import type { PhoneInputProps } from "./PhoneInput"; | ||
import { PhoneInput } from "./PhoneInput"; | ||
|
||
export default { | ||
title: "Components/PhoneInput", | ||
component: PhoneInput, | ||
} as Meta; | ||
|
||
const Template: StoryFn<PhoneInputProps> = (args) => { | ||
return ( | ||
<form className="mx-auto max-w-sm"> | ||
<PhoneInput {...args} /> | ||
</form> | ||
); | ||
}; | ||
|
||
export const Default = Template.bind({}); | ||
Default.storyName = "Phone Input"; | ||
Default.args = { | ||
placeholder: "123-456-7890", | ||
pattern: "[0-9]{3}-[0-9]{3}-[0-9]{4}", | ||
required: true, | ||
}; | ||
|
||
export const WithRightIcon = Template.bind({}); | ||
WithRightIcon.storyName = "Phone Input with Right Icon"; | ||
WithRightIcon.args = { | ||
placeholder: "123-456-7890", | ||
pattern: "[0-9]{3}-[0-9]{3}-[0-9]{4}", | ||
required: true, | ||
rightIcon: FaPhoneAlt, | ||
}; | ||
|
||
export const WithHelperText = Template.bind({}); | ||
WithHelperText.storyName = "Phone Input with Helper Text"; | ||
WithHelperText.args = { | ||
placeholder: "123-456-7890", | ||
pattern: "[0-9]{3}-[0-9]{3}-[0-9]{4}", | ||
required: true, | ||
helperText: "Select a phone number that matches the format.", | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { forwardRef, type ComponentProps, type FC, type ReactNode } from "react"; | ||
import { FaPhoneAlt } from "react-icons/fa"; | ||
import { twMerge } from "tailwind-merge"; | ||
import { mergeDeep } from "../../helpers/merge-deep"; | ||
import { getTheme } from "../../theme-store"; | ||
import type { DeepPartial } from "../../types"; | ||
import { FlowbiteSizes } from "../Flowbite"; | ||
dhavalveera marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { HelperText } from "../HelperText"; | ||
|
||
export interface FlowbitePhoneInputTheme { | ||
root: { | ||
base: string; | ||
input: { | ||
base: string; | ||
sizes: FlowbitePhoneInputSizes; | ||
startIcon: { | ||
base: string; | ||
icon: string; | ||
}; | ||
rightIcon: { | ||
base: string; | ||
icon: string; | ||
}; | ||
}; | ||
}; | ||
} | ||
|
||
export interface FlowbitePhoneInputSizes extends Pick<FlowbiteSizes, "sm" | "md" | "lg"> { | ||
[key: string]: string; | ||
} | ||
|
||
export interface PhoneInputProps extends Omit<ComponentProps<"input">, "ref"> { | ||
helperText?: ReactNode; | ||
icon?: FC<ComponentProps<"svg">>; | ||
rightIcon?: FC<ComponentProps<"svg">>; | ||
sizing?: keyof FlowbitePhoneInputSizes; | ||
theme?: DeepPartial<FlowbitePhoneInputTheme>; | ||
} | ||
|
||
export const PhoneInput = forwardRef<HTMLInputElement, PhoneInputProps>( | ||
( | ||
{ helperText, icon: LeftIcon, rightIcon: RightIcon, sizing = "md", theme: customTheme = {}, className, ...props }, | ||
ref, | ||
) => { | ||
const theme = mergeDeep(getTheme().phoneInput.root, customTheme); | ||
|
||
const StartIcon = LeftIcon ? LeftIcon : FaPhoneAlt; | ||
|
||
return ( | ||
<> | ||
<div className={twMerge(theme.base, className)}> | ||
<div data-testid="left-icon" className={theme.input.startIcon.base}> | ||
<StartIcon aria-hidden className={theme.input.startIcon.icon} /> | ||
</div> | ||
<div className={theme.input.rightIcon.base}> | ||
{RightIcon && ( | ||
<div data-testid="right-icon" className={theme.input.rightIcon.base}> | ||
<RightIcon className={theme.input.rightIcon.icon} /> | ||
</div> | ||
)} | ||
</div> | ||
<input | ||
type="tel" | ||
id="phone-input" | ||
aria-describedby="phone-input" | ||
data-testid="phone-input-field" | ||
className={theme.input.base} | ||
ref={ref} | ||
{...props} | ||
/> | ||
</div> | ||
{helperText && <HelperText color="grey">{helperText}</HelperText>} | ||
</> | ||
); | ||
}, | ||
); | ||
|
||
PhoneInput.displayName = "PhoneInput"; |
Uh oh!
There was an error while loading. Please reload this page.