Flowbite 폼 — 전화번호 입력(Phone Input)
Flowbite의 전화번호 입력(Phone Input) 컴포넌트는 국가 코드 선택 드롭다운과 전화번호 입력 필드를 결합한 폼 요소예요. 사용자가 국가 코드를 선택하면 해당 플래그 아이콘과 함께 전화번호를 입력할 수 있고, 인증 코드 폼 등에도 활용할 수 있어요. Tailwind CSS와 Flowbite의 클래스로 쉽게 구성할 수 있어요.
출처: 문서
본문
기본 전화번호 입력은 국가 코드 드롭다운(플래그 + 코드)과 전화번호 텍스트 필드로 이뤄져요. 아래는 기본 예시예요.
<label for="phone" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Phone number:</label>
<div class="flex">
<div class="flex-shrink-0 z-10 inline-flex items-center py-2.5 px-4 text-sm font-medium text-center text-gray-900 bg-gray-100 border border-e-0 border-gray-300 rounded-s-lg dark:bg-gray-700 dark:text-white dark:border-gray-600">
<svg class="w-4 h-4 me-2" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 20 15">
<rect width="19.6" height="14" y=".5" rx="2" fill="#fff"/>
<mask id="a" width="20" height="15" x="0" y="0" maskUnits="userSpaceOnUse" style="mask-type:alpha">
<rect width="19.6" height="14" y=".5" rx="2" fill="#fff"/>
</mask>
<g mask="url(#a)">
<path fill="#D02F44" fill-rule="evenodd" d="M0 11.5h19.6V14a1 1 0 0 1-1 1H1a1 1 0 0 1-1-1v-2.5Z" clip-rule="evenodd"/>
<path fill="#46467A" fill-rule="evenodd" d="M0 0h19.6v3.5H0V0Z" clip-rule="evenodd"/>
<path stroke="#fff" stroke-width="1.5" d="M0 2.5h19.6M0 5h19.6M0 7.5h19.6M0 10h19.6"/>
</g>
</svg>
+1
<svg class="w-2 h-2 ms-2" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 10 6">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 1 4 4 4-4"/>
</svg>
</div>
<div class="relative w-full">
<input type="tel" id="phone" aria-describedby="helper-text-explanation" class="block p-2.5 w-full z-20 text-sm text-gray-900 bg-gray-50 rounded-e-lg border border-gray-300 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:border-blue-500" placeholder="123-456-7890" required>
</div>
</div>
국가 코드 선택 부분은 플래그 SVG와 국가 코드(+1), 드롭다운 화살표로 구성돼요. 각 나라마다 해당하는 플래그 아이콘을 넣고, 드롭다운을 실제 동작하게 하려면 Flowbite의 드롭다운 컴포넌트 구조와 자바스크립트를 함께 사용해요.
인증 코드 폼(Verification code)
전화번호 인증 코드를 입력받는 폼도 자주 사용해요. 아래는 기본 구조예요.
<div class="flex flex-col items-center justify-center h-screen bg-gray-50 dark:bg-gray-900">
<div class="flex items-center justify-center mb-8">
<span class="me-2 text-sm font-medium text-gray-900 dark:text-white">Country:</span>
<button id="country-button" type="button" class="inline-flex items-center rounded-lg border border-gray-300 bg-white px-4 py-2.5 text-sm font-medium text-gray-900 hover:bg-gray-100 focus:outline-none focus:ring-4 focus:ring-gray-100 dark:border-gray-600 dark:bg-gray-800 dark:text-white dark:hover:bg-gray-700 dark:focus:ring-gray-700">
<img src="https://flowbite.com/docs/images/flags/us.png" class="me-2 h-3.5 w-5" alt="United States">
+1 (US)
<svg class="ms-2 h-2.5 w-2.5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 10 6">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 1 4 4 4-4"/>
</svg>
</button>
</div>
<div class="w-full max-w-sm p-4 bg-white border border-gray-200 rounded-lg shadow sm:p-6 md:p-8 dark:bg-gray-800 dark:border-gray-700">
<form class="space-y-6" action="#">
<h5 class="text-xl font-medium text-gray-900 dark:text-white">Verify your phone number</h5>
<div>
<div class="flex items-center mb-2">
<label for="code" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Verification code</label>
</div>
<div class="relative">
<div class="flex gap-2">
<input type="text" id="code" class="w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-center text-sm text-gray-900 focus:border-blue-500 focus:ring-blue-500 dark:border-gray-600 dark:bg-gray-700 dark:text-white" maxlength="1" placeholder="-">
<input type="text" class="w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-center text-sm text-gray-900 focus:border-blue-500 focus:ring-blue-500 dark:border-gray-600 dark:bg-gray-700 dark:text-white" maxlength="1" placeholder="-">
<input type="text" class="w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-center text-sm text-gray-900 focus:border-blue-500 focus:ring-blue-500 dark:border-gray-600 dark:bg-gray-700 dark:text-white" maxlength="1" placeholder="-">
</div>
</div>
<p class="mt-2 text-sm text-gray-500 dark:text-gray-400">We sent a 3-digit code to your phone number. Please enter it here.</p>
</div>
<button type="submit" class="w-full rounded-lg bg-blue-700 px-5 py-2.5 text-center text-sm font-medium text-white hover:bg-blue-800 focus:outline-none focus:ring-4 focus:ring-blue-300 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Verify</button>
</form>
</div>
</div>
전화번호 입력은 국가 코드 선택이 필요한 국제 전화번호 폼, 인증 코드 입력, 연락처 정보 수집 등에 널리 쓰여요. 국가 코드 드롭다운을 완전히 동작하게 하려면 해당 국가 목록과 플래그, 자바스크립트(dropdown) 초기화가 필요해요. 필요한 국가만 골라 드롭다운에 담으면 폼이 훨씬 가벼워진답니다.