|
1 | 1 | import { DokanButton, MediaUploader } from '@src/components'; |
2 | 2 | import { __ } from '@wordpress/i18n'; |
3 | | -import { Upload } from 'lucide-react'; |
| 3 | +import { Upload, X } from 'lucide-react'; |
4 | 4 | import CustomField from './CustomField'; |
5 | 5 |
|
6 | | -const FeatureImage = ( { data, field, onChange }: any ) => { |
| 6 | +export const ImagePreview = ( { |
| 7 | + images, |
| 8 | + onRemove, |
| 9 | + multiple = false, |
| 10 | +}: { |
| 11 | + images: any; |
| 12 | + onRemove: ( index: number ) => void; |
| 13 | + multiple?: boolean; |
| 14 | +} ) => { |
| 15 | + const items = Array.isArray( images ) ? images : [ images ]; |
| 16 | + if ( items.length === 0 ) { |
| 17 | + return null; |
| 18 | + } |
| 19 | + |
| 20 | + return ( |
| 21 | + <div className="flex flex-wrap gap-3 mt-3"> |
| 22 | + { items.map( ( item: any, index: number ) => ( |
| 23 | + <div |
| 24 | + key={ index } |
| 25 | + className={ `relative group border border-gray-200 rounded-md overflow-hidden ${ |
| 26 | + multiple && 'w-20 h-20' |
| 27 | + }` } |
| 28 | + > |
| 29 | + <img |
| 30 | + src={ item.url } |
| 31 | + alt="product" |
| 32 | + className="w-full h-full object-cover" |
| 33 | + /> |
| 34 | + <button |
| 35 | + type="button" |
| 36 | + className="absolute top-1 right-1 bg-white rounded-full p-0.5 shadow-sm opacity-0 group-hover:opacity-100 transition-opacity duration-200 hover:text-red-500" |
| 37 | + onClick={ ( e ) => { |
| 38 | + e.preventDefault(); |
| 39 | + onRemove( index ); |
| 40 | + } } |
| 41 | + > |
| 42 | + <X size={ 14 } /> |
| 43 | + </button> |
| 44 | + </div> |
| 45 | + ) ) } |
| 46 | + </div> |
| 47 | + ); |
| 48 | +}; |
| 49 | + |
| 50 | +const FeatureImage = ( { field, onChange }: any ) => { |
7 | 51 | const onSelect = ( value: any ) => { |
| 52 | + let ids, values; |
| 53 | + |
| 54 | + if ( Array.isArray( value ) ) { |
| 55 | + ids = value.map( ( item: any ) => item.id ); |
| 56 | + values = value; |
| 57 | + } else { |
| 58 | + ids = value.id; |
| 59 | + values = value; |
| 60 | + } |
| 61 | + |
| 62 | + onChange( { |
| 63 | + [ field.id ]: ids, |
| 64 | + [ `$${ field.id }_value` ]: values, |
| 65 | + } ); |
| 66 | + }; |
| 67 | + |
| 68 | + const onRemove = () => { |
8 | 69 | onChange( { |
9 | | - [ field.id ]: value.id, |
10 | | - [ `$${ field.id }_url` ]: value.url, |
| 70 | + [ field.id ]: [], |
| 71 | + [ `$${ field.id }_value` ]: [], |
11 | 72 | } ); |
12 | 73 | }; |
13 | 74 |
|
14 | | - const label = field.field_type === 'gallery' ? '' : field.label; |
| 75 | + const multiple = field.field_type === 'gallery'; |
| 76 | + const images = field.value; |
15 | 77 |
|
16 | 78 | return ( |
17 | | - <CustomField label={ label }> |
18 | | - <MediaUploader |
19 | | - onSelect={ onSelect } |
20 | | - className={ `product-${ field.id }` } |
21 | | - multiple={ field.field_type === 'gallery' } |
22 | | - > |
23 | | - <DokanButton variant="secondary" className="uppercase"> |
24 | | - <Upload size={ 16 } /> |
25 | | - { __( 'Upload File', 'dokan-lite' ) } |
26 | | - </DokanButton> |
27 | | - <span> |
28 | | - { __( 'A product cover image here.', 'dokan-lite' ) } |
29 | | - </span> |
30 | | - { data[ `$${ field.id }_url` ] && ( |
31 | | - <img |
32 | | - src={ data[ `$${ field.id }_url` ] } |
33 | | - alt="Product" |
34 | | - style={ { |
35 | | - marginTop: '10px', |
36 | | - maxWidth: '100%', |
37 | | - width: '150px', |
38 | | - } } |
39 | | - /> |
40 | | - ) } |
41 | | - </MediaUploader> |
| 79 | + <CustomField label={ field.label } className={ `${ field.id }-field` }> |
| 80 | + { images.length ? ( |
| 81 | + <ImagePreview images={ images } onRemove={ onRemove } /> |
| 82 | + ) : ( |
| 83 | + <MediaUploader |
| 84 | + onSelect={ onSelect } |
| 85 | + className={ `product-${ field.id }` } |
| 86 | + multiple={ field.field_type === 'gallery' } |
| 87 | + > |
| 88 | + { } |
| 89 | + <DokanButton variant="secondary" className="uppercase"> |
| 90 | + <Upload size={ 16 } /> |
| 91 | + { __( 'Upload File', 'dokan-lite' ) } |
| 92 | + </DokanButton> |
| 93 | + <span> |
| 94 | + { __( 'A product cover image here.', 'dokan-lite' ) } |
| 95 | + </span> |
| 96 | + </MediaUploader> |
| 97 | + ) } |
42 | 98 | </CustomField> |
43 | 99 | ); |
44 | 100 | }; |
|
0 commit comments