很高兴为您提供这个项目。在这个项目中,我们将使用 React、TypeScript 和 Tailwind CSS 来创建一个漂亮的手机号码和短信验证码登录页面。

首先,我们需要安装必要的依赖项。在终端中,进入您的项目文件夹并运行以下命令:

npm install react react-dom typescript tailwindcss @craco/craco autoprefixer postcss-cli

以上命令将安装 React、TypeScript、Tailwind CSS、Craco、Autoprefixer 和 PostCSS CLI。

接下来,我们需要在项目的根目录中创建一个 'tailwind.config.js' 文件。在这个文件中,我们可以配置 Tailwind CSS 的样式:

module.exports = {
  purge: ['./src/**/*.{js,jsx,ts,tsx}', './public/index.html'],
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
}

在 'purge' 属性中,我们指定了哪些文件包含了我们的样式。在这个例子中,我们使用了默认的设置,它将查找所有的 '.js', '.jsx', '.ts', '.tsx' 和 '.html' 文件。

接下来,我们可以在 'src' 文件夹中创建一个 'App.tsx' 文件,这将是我们的主应用程序组件。在这个组件中,我们将创建一个带有手机号码和短信验证码输入框的表单。

import React, { useState } from 'react'

const App: React.FC = () => {
  const [phoneNumber, setPhoneNumber] = useState('')
  const [verificationCode, setVerificationCode] = useState('')

  const handlePhoneNumberChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    setPhoneNumber(event.target.value)
  }

  const handleVerificationCodeChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    setVerificationCode(event.target.value)
  }

  const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
    event.preventDefault()
    console.log('Phone number: ' + phoneNumber + ', Verification code: ' + verificationCode)
  }

  return (
    <div className='min-h-screen bg-gray-100 flex flex-col justify-center py-12 sm:px-6 lg:px-8'>
      <div className='sm:mx-auto sm:w-full sm:max-w-md'>
        <h2 className='mt-6 text-center text-3xl font-extrabold text-gray-900'>
          Sign in to your account
        </h2>
      </div>

      <div className='mt-8 sm:mx-auto sm:w-full sm:max-w-md'>
        <div className='bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10'>
          <form className='space-y-6' onSubmit={handleSubmit}>
            <div>
              <label htmlFor='phone-number' className='block text-sm font-medium text-gray-700'>
                Phone number
              </label>
              <div className='mt-1'>
                <input
                  id='phone-number'
                  name='phone-number'
                  type='tel'
                  autoComplete='tel'
                  required
                  className='appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm'
                  value={phoneNumber}
                  onChange={handlePhoneNumberChange}
                />
              </div>
            </div>

            <div>
              <label htmlFor='verification-code' className='block text-sm font-medium text-gray-700'>
                Verification code
              </label>
              <div className='mt-1'>
                <input
                  id='verification-code'
                  name='verification-code'
                  type='tel'
                  autoComplete='one-time-code'
                  required
                  className='appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm'
                  value={verificationCode}
                  onChange={handleVerificationCodeChange}
                />
              </div>
            </div>

            <div>
              <button
                type='submit'
                className='w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500'
              >
                Sign in
              </button>
            </div>
          </form>
        </div>
      </div>
    </div>
  )
}

export default App

在上面的代码中,我们使用了 React 的 'useState' 钩子来管理手机号码和短信验证码的状态。我们还编写了三个函数来处理输入框和表单的变化和提交。

在 'render' 方法中,我们渲染了一个带有两个输入框和一个提交按钮的表单。我们使用了 Tailwind CSS 来设置表单的样式。

最后,我们需要在项目的根目录中创建一个 'craco.config.js' 文件。在这个文件中,我们可以配置 Craco 来使用 Tailwind CSS:

module.exports = {
  style: {
    postcss: {
      plugins: [require('tailwindcss'), require('autoprefixer')],
    },
  },
}

现在,您可以在终端中运行以下命令来启动应用程序:

npm start

恭喜您,您已经成功地创建了一个漂亮的手机号码和短信验证码登录页面!

使用 React + TypeScript + Tailwind CSS 构建手机号码和短信验证码登录页面

原文地址: https://www.cveoy.top/t/topic/lNK6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录