index.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useState } from 'react'
  4. import s from './style.module.css'
  5. import cn from '@/utils/classnames'
  6. import ImagePreview from '@/app/components/base/image-uploader/image-preview'
  7. type Props = {
  8. srcs: string[]
  9. }
  10. const getWidthStyle = (imgNum: number) => {
  11. if (imgNum === 1) {
  12. return {
  13. maxWidth: '100%',
  14. }
  15. }
  16. if (imgNum === 2 || imgNum === 4) {
  17. return {
  18. width: 'calc(50% - 4px)',
  19. }
  20. }
  21. return {
  22. width: 'calc(33.3333% - 5.3333px)',
  23. }
  24. }
  25. const ImageGallery: FC<Props> = ({
  26. srcs,
  27. }) => {
  28. const [imagePreviewUrl, setImagePreviewUrl] = useState('')
  29. const imgNum = srcs.length
  30. const imgStyle = getWidthStyle(imgNum)
  31. return (
  32. <div className={cn(s[`img-${imgNum}`], 'flex flex-wrap')}>
  33. {/* TODO: support preview */}
  34. {srcs.map((src, index) => (
  35. // eslint-disable-next-line @next/next/no-img-element
  36. <img
  37. key={index}
  38. className={s.item}
  39. style={imgStyle}
  40. src={src}
  41. alt=''
  42. onClick={() => setImagePreviewUrl(src)}
  43. onError={e => e.currentTarget.remove()}
  44. />
  45. ))}
  46. {
  47. imagePreviewUrl && (
  48. <ImagePreview
  49. url={imagePreviewUrl}
  50. onCancel={() => setImagePreviewUrl('')}
  51. />
  52. )
  53. }
  54. </div>
  55. )
  56. }
  57. export default React.memo(ImageGallery)
  58. export const ImageGalleryTest = () => {
  59. const imgGallerySrcs = (() => {
  60. const srcs = []
  61. for (let i = 0; i < 6; i++)
  62. // srcs.push('https://placekitten.com/640/360')
  63. // srcs.push('https://placekitten.com/360/640')
  64. srcs.push('https://placekitten.com/360/360')
  65. return srcs
  66. })()
  67. return (
  68. <div className='space-y-2'>
  69. {imgGallerySrcs.map((_, index) => (
  70. <div key={index} className='p-4 pb-2 rounded-lg bg-[#D1E9FF80]'>
  71. <ImageGallery srcs={imgGallerySrcs.slice(0, index + 1)} />
  72. </div>
  73. ))}
  74. </div>
  75. )
  76. }