- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 476
 
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
          
     Open
      
      
            dhavalveera
  wants to merge
  7
  commits into
  themesberg:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
dhavalveera:feat/phone-input
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      34b3732
              
                feat/phoneInput: added phoneInput Form Feature
              
              
                dhavalveera 709b3be
              
                fix: Example Updated for Docs
              
              
                dhavalveera 6e2e16f
              
                feat: Docs Example added with PhoneInput with Right Icon
              
              
                dhavalveera 3dd1832
              
                fix: renamed the heading in the docs
              
              
                dhavalveera d9752f7
              
                Docs typo mistake fixed
              
              
                dhavalveera 45be937
              
                Fixes based on the reviews
              
              
                dhavalveera cff0a50
              
                Docs fix based on AI Review
              
              
                dhavalveera File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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,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, leveraging the native type="tel" attribute, simplifies entering phone numbers in form fields. | ||
| 
     | 
||
| The examples are built with the utility classes from Tailwind CSS, and they are fully responsive, dark mode compatible, and support RTL layouts, making them suitable for any type of web project. | ||
| 
     | 
||
| To start using the component, make sure that you have imported it from Flowbite React: | ||
| 
     | 
||
| ```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 | ||
| 
     | 
||
| For detailed instructions on customizing component appearances, refer to the [Theme documentation](/docs/customize/theme). | ||
| 
     | 
||
| <Theme name="phoneInput" /> | ||
| 
     | 
||
| ## References | ||
| 
     | 
||
| - [Flowbite Phone Input](https://flowbite.com/docs/forms/phone-input/) | ||
  
    
      This file contains hidden or 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
    
  
  
    
              
  
    
      This file contains hidden or 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
    
  
  
    
              
  
    
      This file contains hidden or 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,3 @@ | ||
| export { root } from "./phoneInput.root"; | ||
| export { withHelperText } from "./phoneInput.withHelperText"; | ||
| export { withRightIcon } from "./phoneInput.withRightIcon"; | 
  
    
      This file contains hidden or 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,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 ( | ||
| <form className="max-w-sm mx-auto"> | ||
| <PhoneInput /> | ||
| </form> | ||
| ) | ||
| } | ||
| `; | ||
| 
     | 
||
| const codeRSC = ` | ||
| function Component() { | ||
| return ( | ||
| <form className="max-w-sm mx-auto"> | ||
| <PhoneInput /> | ||
| </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 />, | ||
| }; | 
        
          
          
            57 changes: 57 additions & 0 deletions
          
          57 
        
  apps/web/examples/phoneInput/phoneInput.withHelperText.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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,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 />, | ||
| }; | 
  
    
      This file contains hidden or 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,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} /> | ||
| </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 />, | ||
| }; | 
  
    
      This file contains hidden or 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
    
  
  
    
              
  
    
      This file contains hidden or 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,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(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | 
        
          
          
            43 changes: 43 additions & 0 deletions
          
          43 
        
  packages/ui/src/components/PhoneInput/PhoneInput.stories.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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,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.", | ||
| }; | 
  
    
      This file contains hidden or 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,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 type { FlowbiteSizes } from "../Flowbite"; | ||
| 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"; | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.