Skip to content

Commit bd919d8

Browse files
feat: add media upload and switcher components to repeater item
1 parent 4290bbd commit bd919d8

File tree

1 file changed

+89
-3
lines changed

1 file changed

+89
-3
lines changed

assets/apps/customizer-controls/src/repeater/RepeaterItemContent.js

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import {
55
ToggleControl,
66
TextareaControl,
77
ExternalLink,
8+
ButtonGroup,
9+
Placeholder,
810
} from '@wordpress/components';
911
import IconSelector from './IconSelector';
1012
import { getIcons, ColorControl } from '@neve-wp/components';
1113
import PropTypes from 'prop-types';
1214
import { __ } from '@wordpress/i18n';
1315
import Range from '../range/Range';
16+
import { MediaUpload } from '@wordpress/media-utils';
1417

1518
const RepeaterItemContent = ({
1619
fields,
@@ -163,14 +166,97 @@ const RepeaterItemContent = ({
163166
{...fields[key]}
164167
/>
165168
);
169+
case 'switcher':
170+
return <Switcher fieldId={key} currentField={currentField} />;
171+
case 'media':
172+
return (
173+
<Placeholder
174+
icon="format-image"
175+
label={__('Custom Icon', 'neve')}
176+
>
177+
{!value[index][key] ? (
178+
<>
179+
<p>
180+
{__(
181+
'Select from the Media Library or upload a new image',
182+
'neve'
183+
)}
184+
</p>
185+
<MediaUpload
186+
onSelect={(imageData) => {
187+
changeContent(key, imageData.url);
188+
}}
189+
allowedTypes={['image']}
190+
render={({ open }) => (
191+
<Button isSecondary onClick={open}>
192+
{__('Add Image', 'neve')}
193+
</Button>
194+
)}
195+
/>
196+
</>
197+
) : (
198+
<>
199+
<img src={value[index][key]} alt="icon" />
200+
<Button
201+
disabled={!wp.media}
202+
className="remove-image"
203+
isDestructive
204+
isSecondary
205+
onClick={() => {
206+
changeContent(key, '');
207+
}}
208+
>
209+
{__('Remove Image', 'neve')}
210+
</Button>
211+
</>
212+
)}
213+
</Placeholder>
214+
);
166215
}
167216
};
168217

218+
const Switcher = ({ fieldId, currentField }) => {
219+
const selectedOption =
220+
value[index][fieldId] || Object.keys(currentField.options)[0];
221+
222+
return (
223+
<>
224+
<ButtonGroup className="neve-background-type-control">
225+
{Object.entries(currentField.options).map(
226+
([optionKey, option]) => {
227+
return (
228+
<Button
229+
key={optionKey}
230+
isPrimary={optionKey === selectedOption}
231+
isSecondary={optionKey !== selectedOption}
232+
onClick={() => {
233+
changeContent(fieldId, optionKey);
234+
}}
235+
>
236+
{option.label}
237+
</Button>
238+
);
239+
}
240+
)}
241+
</ButtonGroup>
242+
{currentField.options?.[selectedOption]?.fields.map(
243+
(componentId) => {
244+
return toComponent(componentId, value[index]);
245+
}
246+
)}
247+
</>
248+
);
249+
};
250+
169251
return (
170252
<div className="nv-repeater-content">
171-
{Object.entries(currentFields).map(([key]) => {
172-
return toComponent(key, value[index]);
173-
})}
253+
{Object.entries(currentFields)
254+
.filter(([, options]) => {
255+
return !options?.parent;
256+
})
257+
.map(([key]) => {
258+
return toComponent(key, value[index]);
259+
})}
174260
{value.length > 1 && !isBlocked && (
175261
<Button
176262
className="nv-repeater-remove-button"

0 commit comments

Comments
 (0)