[Feature] Generic dropdown component
See original GitHub issueI’m toying around with making Dropdown generic. It will allow us to remove a lot of boiler plating from our app. Is this the right place to share this to spark a discussion? Obviously there’s some performance and feature issues to work - but this proves that it can work.
import * as React from "react";
import { Dropdown as SemanticDropdown, SemanticShorthandContent } from "semantic-ui-react";
interface IProps<T> {
value: T | null | undefined;
options: T[] | null | undefined;
mapValue: (v: T) => boolean | number | string;
mapText: (v: T) => SemanticShorthandContent;
onChange: (v: T | undefined) => void;
}
export class Dropdown<T> extends React.Component<IProps<T>, any> {
public render(): JSX.Element {
const props = this.props;
const options = props.options && props.options.map((o) => ({
value: props.mapValue(o),
text: props.mapText(o),
}));
return (
<SemanticDropdown
options={options || []}
value={props.value && props.mapValue(props.value) || undefined}
onChange={(e, d) => props.onChange(props.options && props.options.find((o) => props.mapValue(o) === d.value) || undefined)} />
);
}
}
Usage is incredibly easy - especially since typescript 2.9:
interface Props {
animals: Animal[];
}
interface State {
selectedAnimal: Animal;
}
interface Animal {
id: number;
name: string;
color: string;
}
<Dropdown<Animal>
value={this.state.selectedAnimal}
options={this.props.animals}
mapValue={(animal) => animal.id}
mapText={(animal) => (<span style={{ color: animal.color }}>{animal.name}</span>)}
onChange={(animal) => { this.setState({ selectedAnimal: animal }); }} />
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Internalpedia: Dropdown Component - Internal.io
A dropdown component is a module that displays a data set when clicked on. The data set is presented as a list, and...
Read more >Customize a reusable React dropdown menu component
A dropdown component, which is a custom version of the dropdown select option with extended styles, sorts and filters content on webpages.
Read more >Blazor creating a generic drop-down - Stack Overflow
I'm trying to create a generic dropdown component for use across our system. However, I'm encountering issues when binding the EventCallback ...
Read more >Create a generic dropdown using Bootstrap, Angular and RxJS!
Create a dropdown having search functionality, keyboard navigation and that can be reusable in all the components.
Read more >How to write a Generic Function (Code) to Handle ... - YouTube
In this video, I have explained How to write a Generic Function to Handle DropDown in Selenium (Interview Question).
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

First of all this is obviously meant as a PoC and not something production ready. I think this is something this library would benefit from which is why I´m starting the discussion. Also I´m talking about generics like in C#.
As you can see in the usage example I´m using
Dropdown<T>with the generic typeAnimalwhich means I can insert anAnimal[]into the options field and have theonChangeevent return anAnimal.Whereas - unless I´m using it wrong - the current version of the
Dropdownwould first require me to mapAnimal[]array to{ value: number, text: string }[]. Then theonChangewould return an number which I would use to search theAnimal[]for the correct item. Some of this boiler plating can either be removed or rewritten more cleanly with a generic version of theDropdown.This issue will be closed due to lack of activity for 12 months. If you’d like this to be reopened, just leave a comment; we do monitor them!