App.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. { isOpen && <div>{children}</div>}
  20. </>
  21. )
  22. }
  23. const RangeInput = ({min, max}) => {
  24. const [color, setColor] = useState('')
  25. return (
  26. <input onChange={e => e.target.value.length < min || e.target.value.length > max ? setColor('red') : setColor('green')} style={{color}} />
  27. )
  28. }
  29. const PasswordConfirm = ({min}) => {
  30. const [password, setPassword] = useState('')
  31. const [password2, setPassword2] = useState('')
  32. return (
  33. <>
  34. <input type="password" value={password} onChange={e => setPassword(e.target.value)} />
  35. <input type="password" value={password2} onChange={e => setPassword2(e.target.value)} />
  36. <button disabled={ password !== password2 || password.length < min }>Confirm</button>
  37. </>
  38. )
  39. }
  40. function App() {
  41. return (
  42. <div>
  43. <LoginForm onLogin={(l, p) => console.log(l, p)} />
  44. <Spoiler header={'тык сюда'} open>
  45. children
  46. <p>more children</p>
  47. </Spoiler>
  48. <RangeInput min={5} max={10} />
  49. <PasswordConfirm min={5} />
  50. </div>
  51. );
  52. }
  53. export default App;