App.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import './App.css';
  2. import { useState } from 'react'
  3. const LoginForm = ({onLogin}) => {
  4. const [login, setLogin] = useState('')
  5. const [password, setPassword] = useState('')
  6. return (
  7. <>
  8. <input type='text' value={login} onChange={(e) => setLogin(e.target.value)} />
  9. <input type='password' value={password} onChange={(e) => setPassword(e.target.value)} />
  10. <button onClick={() => onLogin(login, password)} disabled={!login.length || !password.length}>Log in</button>
  11. </>
  12. )
  13. }
  14. const Spoiler = ({header="+", open, children}) => {
  15. const [isOpen, setOpen] = useState(open)
  16. return (
  17. <>
  18. <div onClick={() => setOpen(!isOpen)} style={{cursor: 'pointer'}}>{header}</div>
  19. <div>
  20. { isOpen ? children : undefined}
  21. </div>
  22. </>
  23. )
  24. }
  25. const RangeInput = ({min, max}) => {
  26. const [color, setColor] = useState('')
  27. return (
  28. <input onChange={e => e.target.value.length < min || e.target.value.length > max ? setColor('red') : setColor('green')} style={{color}} />
  29. )
  30. }
  31. const PasswordConfirm = ({min}) => {
  32. const [password, setPassword] = useState('')
  33. const [password2, setPassword2] = useState('')
  34. return (
  35. <>
  36. <input type="password" value={password} onChange={e => setPassword(e.target.value)} />
  37. <input type="password" value={password2} onChange={e => setPassword2(e.target.value)} />
  38. <button disabled={ password !== password2 || password.length < min }>Confirm</button>
  39. </>
  40. )
  41. }
  42. function App() {
  43. return (
  44. <div>
  45. <LoginForm onLogin={(l, p) => console.log(l, p)} />
  46. <Spoiler header={'тык сюда'} open>
  47. children
  48. <p>more children</p>
  49. </Spoiler>
  50. <RangeInput min={5} max={10} />
  51. <PasswordConfirm min={5} />
  52. </div>
  53. );
  54. }
  55. export default App;