index.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import React, { ChangeEvent, forwardRef } from 'react';
  2. import './styles.scss';
  3. type InputProps = {
  4. [x: string]: any,
  5. label?: string,
  6. type?: 'text' | 'number' ,
  7. onChange?: (e: React.FormEvent<HTMLInputElement>) => void,
  8. error?: string,
  9. }
  10. export const Input = ({label, type = 'text', error, children, ...props }: InputProps) => {
  11. return (
  12. <div className={`cbk-light-input${error ? ' cbk-light-input--invalid' : ''}`}>
  13. { label && (<label htmlFor={props.name}>{label}</label>)}
  14. <input
  15. className={props.value ? 'has-value': ''}
  16. type={type}
  17. {...props}
  18. />
  19. <span></span>
  20. { error && (
  21. <div className="cbk-light-input--invalid__error"> { error } </div>
  22. )}
  23. </div>
  24. )
  25. }
  26. export const ControlledInput = forwardRef<HTMLInputElement, InputProps>(({label, type = 'text', error, children, ...props }, ref) => {
  27. return (
  28. <div className={`cbk-light-input${error ? ' cbk-light-input--invalid' : ''}`}>
  29. { label && (<label htmlFor={props.name}>{label}</label>)}
  30. <input
  31. ref={ref}
  32. className={props.value ? 'has-value': ''}
  33. type={type}
  34. {...props}
  35. />
  36. <span></span>
  37. { error && (
  38. <div className="cbk-light-input--invalid__error"> { error } </div>
  39. )}
  40. </div>
  41. )
  42. })
  43. type TextareaProps = {
  44. [x: string]: any,
  45. label?: string,
  46. rows?: number
  47. onChange?: (e: ChangeEvent<HTMLTextAreaElement>) => void,
  48. error?: string,
  49. }
  50. export const Textarea = ({label, type = 'text', rows = 3, error, children, ...props }: TextareaProps) => {
  51. return (
  52. <div className={`cbk-light-input${error ? ' cbk-light-input--invalid' : ''}`}>
  53. { label && (<label htmlFor={props.name}>{label}</label>)}
  54. <textarea
  55. rows={rows}
  56. className={props.value ? 'has-value': ''}
  57. {...props}
  58. />
  59. <span></span>
  60. { error && (
  61. <div className="cbk-light-input--invalid__error"> { error } </div>
  62. )}
  63. </div>
  64. )
  65. }
  66. export default Input;