Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$toggle support for Sets #80

Open
mnpenner opened this issue Jan 4, 2018 · 1 comment
Open

$toggle support for Sets #80

mnpenner opened this issue Jan 4, 2018 · 1 comment

Comments

@mnpenner
Copy link

mnpenner commented Jan 4, 2018

For lists of checkboxes like this:

image

I like to store the IDs of the checked elements in a Set.

To "toggle" a checkbox right now I have to do this:

checkOccasion = id => ev => {
    const spec = {
        enabled: {
            [ev.target.checked ? '$add' : '$remove']: [id],
        }
    };
    this::updateState(spec);
}
<CheckboxLabel value={occ.id} checked={enabled.has(occ.id)} onChange={this.checkOccasion(occ.id)}>{occ.name}</CheckboxLabel>

Would be nicer if instead I could just do:

const spec = {
    enabled: {
        $toggle: [id],
    }
};

i.e. enabled is a Set, so if id is already contained in the set, it would be removed, otherwise it would be added.


Actually, alternatively $toggle could accept an object [too] which would explicitly let you add or remove specific elements:

const spec = {
    enabled: {
        $toggle: {
            [id]: ev.target.checked
        },
    }
};

That'd let us skip the .has check too.

@mnpenner mnpenner changed the title $toggle support for set $toggle support for Sets Jan 4, 2018
@linq2js
Copy link

linq2js commented Aug 11, 2018

this suppots toogle on many props https://www.npmjs.com/package/immhelper

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants