Issue #2552Opened February 6, 2020by kickbk2 reactions

Only allow one component to be dropped

Question

Sorry for the newbie question, but say I have a text field component and I only want to allow the user to drop a specific number of them into the canvas, say just one. How do I limit it? Perhaps a way that shows an alert to explain they cannot add another text field component to the canvas unless they remove the existing one?

Answers (2)

kickbkFebruary 6, 20200 reactions

So I have a very ugly solution right now:

	editor.on('load',
		function() {
			var existingComponents = editor.getComponents();
			if (existingComponents.models.filter(function(e) { return e.attributes.tagName === 'form'; }).length > 0) {
				$($($("body .gjs-block-category")[1]).find(".gjs-block")[0]).hide();
			}
		}
	);

	editor.on('component:add', component => {
		if (component.is('form')) {
			$($($("body .gjs-block-category")[1]).find(".gjs-block")[0]).hide();
		}
	});
	editor.on('component:remove', component => {
		if (component.is('form')) {
			$($($("body .gjs-block-category")[1]).find(".gjs-block")[0]).show();
		}
	});

This basically looks whether the component is in the canvas on load. If it is then it hides the draggable component button - Here I could use a better way to hide the button - how? I can get to the component with const form = editor.BlockManager.get('form'); but how do I hide it once I have it? Then I have listeners for add and remove to hide and show the component button - same issue as above.

This seems like a horrible way to do it. There must be some setting I am missing for "only allowed one". Any suggestions?

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.