ink-select-input icon indicating copy to clipboard operation
ink-select-input copied to clipboard

Accept an `action` property in the `items` item object

Open sindresorhus opened this issue 7 years ago • 4 comments

When all of the select items have different actions, it's annoying having to define values and then do a switch on them in the onSelect handler. Would be nicer to be able to define the actions directly.

sindresorhus avatar Mar 24 '18 18:03 sindresorhus

Since onSelect returns the current item, why don't you do this:

const items = [{label: 'Unicorn', value: 'unicorn', action: () => {}}];

const handleSelect = item => {
	item.action();
};

<SelectInput items={items} onSelect={handleSelect}/>;

Feels cleaner than handling it in this module.

kevva avatar Mar 29 '18 16:03 kevva

@kevva That works too. Good idea! Although, I still think action() should be called by default if defined, as your solution was not obvious, and having it called by default would be a nice convenience and also make it clear as it would be documented in the readme.

sindresorhus avatar May 07 '18 18:05 sindresorhus

In my mind, @kevva's solution is more idiomatic React

SimenB avatar Mar 09 '19 07:03 SimenB

It should at least be documented in the readme.

sindresorhus avatar Mar 09 '19 14:03 sindresorhus