Testimonials.jsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // components
  2. import Spring from '@components/Spring';
  3. import Testimonial from '@components/Testimonial';
  4. import ScrollContainer from '@components/ScrollContainer';
  5. import AddFormContainer from '@components/AddFormContainer';
  6. import CustomRating from '@ui/CustomRating';
  7. // hooks
  8. import useMeasure from 'react-use-measure';
  9. import {useForm, Controller} from 'react-hook-form';
  10. import {useState, useEffect, useRef} from 'react';
  11. // utils
  12. import classNames from 'classnames';
  13. const data = [
  14. {
  15. text: 'Wore these with my training tee and pods to a graduation bbq. Love the look and feel.',
  16. author: 'Jacob Sullivan',
  17. rating: 4.5,
  18. },
  19. {
  20. text: 'I\'m enjoying playing football!',
  21. author: 'Max Power',
  22. rating: 5,
  23. },
  24. {
  25. text: 'I\'m enjoying playing football!',
  26. author: 'Sunny Drew',
  27. rating: 5,
  28. }
  29. ]
  30. const Testimonials = () => {
  31. const {register, handleSubmit, formState: {errors}, reset, control} = useForm({
  32. defaultValues: {
  33. text: '',
  34. author: '',
  35. rating: 5,
  36. }
  37. });
  38. const [isFormOpen, setIsFormOpen] = useState(false);
  39. const [ref, {height}] = useMeasure();
  40. const trackRef = useRef(null);
  41. const onReset = () => {
  42. reset();
  43. setIsFormOpen(false);
  44. }
  45. const onSubmit = (data) => {
  46. // do something with data
  47. onReset();
  48. }
  49. useEffect(() => {
  50. trackRef.current && trackRef.current.scrollTo(0, 0);
  51. }, [isFormOpen]);
  52. return (
  53. <Spring className="card h-2">
  54. <ScrollContainer height={height} isCompact>
  55. <div className="track" ref={trackRef}>
  56. <AddFormContainer open={isFormOpen}>
  57. <form className="card_header d-flex flex-column g-16" onSubmit={handleSubmit(onSubmit)}>
  58. <textarea className={classNames('field', {'field--error': errors.text})}
  59. placeholder="Your comment"
  60. {...register('text', {required: true})}/>
  61. <input className={classNames('field', {'field--error': errors.author})}
  62. type="text"
  63. placeholder="Your name"
  64. {...register('author', {required: true})}/>
  65. <div className="d-flex align-items-center justify-content-between">
  66. Rate:
  67. <Controller name="rating"
  68. control={control}
  69. render={({field}) => (
  70. <CustomRating readOnly={false}
  71. value={field.value}
  72. onChange={(value) => field.onChange(value)}/>
  73. )}/>
  74. </div>
  75. <div className="d-grid col-2 g-10">
  76. <button className="btn" type="submit">Submit</button>
  77. <button className="btn btn--outlined" type="reset" onClick={onReset}>
  78. Cancel
  79. </button>
  80. </div>
  81. </form>
  82. </AddFormContainer>
  83. {
  84. data.map((item, index) => (
  85. <Testimonial key={index} data={item} index={index}/>
  86. ))
  87. }
  88. </div>
  89. </ScrollContainer>
  90. <div className="card-padded" ref={ref}>
  91. <button className="btn w-100" onClick={() => setIsFormOpen(true)} disabled={isFormOpen}>
  92. Add new comment
  93. </button>
  94. </div>
  95. </Spring>
  96. )
  97. }
  98. export default Testimonials