question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature] Generic dropdown component

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
SaphuAcommented, Jul 13, 2018

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 type Animal which means I can insert an Animal[] into the options field and have the onChange event return an Animal.

Whereas - unless I´m using it wrong - the current version of the Dropdown would first require me to map Animal[] array to { value: number, text: string }[]. Then the onChange would return an number which I would use to search the Animal[] for the correct item. Some of this boiler plating can either be removed or rewritten more cleanly with a generic version of the Dropdown.

0reactions
stale[bot]commented, Nov 16, 2019

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found