create-guten-block icon indicating copy to clipboard operation
create-guten-block copied to clipboard

Repeating blocks

Open braco opened this issue 6 years ago • 11 comments

Do you have any plans to add a repeating input helper like Gutenblock?

We've included a Select MediaSelect Input Inspector Repeat and other form fields to help you build blocks faster. A repeat component will handle the hard work of letting users add infinite items to an array of form fields, replacing items, and deleting them.

braco avatar Oct 13 '18 19:10 braco

I think that would make this start way too opinionated. Don't ya think so?

ahmadawais avatar Oct 13 '18 19:10 ahmadawais

Definitely not, at least not a good implementation. That's a huge missing piece in Gutenberg development – it's a huge pain to build components that have repeating elements.

braco avatar Oct 13 '18 20:10 braco

A dirty functional approach

https://gist.github.com/braco/5c48ad162dcd27f072debc3967aee0b0

braco avatar Oct 14 '18 03:10 braco

it's a huge pain to build components that have repeating elements.

Not exactly the same thing, but InnerBlocks can be used in most cases when I would have used a repeater in the past. Create a container block to contain your repeating element, and then create a separate block for use inside.

You can restrict the container to ONLY contain your repeater, and you can restrict your repeater to ONLY be used inside your container.

https://github.com/WordPress/gutenberg/tree/master/packages/editor/src/components/inner-blocks

import { registerBlockType } from '@wordpress/blocks';
import { InnerBlocks } from '@wordpress/editor';

registerBlockType( 'my-plugin/container', {
	// ...

	edit( { className } ) {
		return (
			<div className={ className }>
				<InnerBlocks
          allowedBlocks={ 'my-plugin/repeater' }
          template={ [ 'my-plugin/repeater' ] }
        />
			</div>
		);
	},

	save() {
		return (
			<div>
				<InnerBlocks.Content />
			</div>
		);
	}
} );

registerBlockType( 'my-plugin/repeater', {
  parent: [ 'my-plugin/container' ],

	edit( { className } ) {
		return (
			<div className={ className }>
         {/* whatever */}
			</div>
		);
	},

	save() {
		return (
			<div>
				{/* whatever */}
			</div>
		);
	}
} );

drdogbot7 avatar Dec 10 '18 14:12 drdogbot7

Thanks @drdogbot7, how good is the UX around that feature?

braco avatar Dec 10 '18 15:12 braco

It can still be a bit rough around the edges, but the core functionality works well, especially if you're keeping it relatively simple. From an end-user perspective I think the most confusing part is determining whether you have the container block or a child block selected—if you've played around with the core/columns block, you know what I'm talking about.

Since it's a core feature, it feels pretty safe to make use of it. I assume the implementation will only improve.

drdogbot7 avatar Dec 10 '18 15:12 drdogbot7

For anyone else who sees this, InnerBlocks presents some problems, like three levels of extra markup between the container and children. It will save() the way you expect, but the editing experience is bad IMO.

braco avatar Dec 17 '18 05:12 braco

I think the most confusing part is determining whether you have the container block or a child block selected

This is the main reason why I'm trying to develop a single block to deal with repeating elements.

For anyone else who sees this, InnerBlocks presents some problems, like three levels of extra markup between the container and children.

I've also noticed that, which makes it a bit complicated to properly arrange the inner elements in the desired way.

I found out that Kadence Blocks use the kind of approach that I'm trying to achieve in their Advanced Button and Icon blocks, but I haven't been able to replicate it in my own block.

Have you made any progress regarding this matter, @braco @drdogbot7 ?

rodrigodagostino avatar Mar 17 '19 05:03 rodrigodagostino

This discussion should take place in the Gutenberg repo, it has nothing to do with CGB.

Jebble avatar Jun 21 '19 10:06 Jebble

@Jebble thanks for your amazing contribution! [clap] [clap]

rodrigodagostino avatar Jun 21 '19 21:06 rodrigodagostino

@Jebble thanks for your amazing contribution! [clap] [clap]

It will simply have a lot more traction in the correct repo as this has simply nothing to do with what you're discussing. I'm sorry you can't handle some feedback :)

Jebble avatar Jun 22 '19 10:06 Jebble