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

Shortcut method for mutuallyExclusiveTrueProps? #21

Open
lencioni opened this issue Jun 9, 2017 · 2 comments
Open

Shortcut method for mutuallyExclusiveTrueProps? #21

lencioni opened this issue Jun 9, 2017 · 2 comments

Comments

@lencioni
Copy link
Collaborator

lencioni commented Jun 9, 2017

Currently, if you want to use mutuallyExclusiveTrueProps, you need to do something like this:

const fooProp = mutuallyExclusiveTrueProps('foo', 'bar', 'baz');

Foo.propTypes = {
  foo: fooProp,
  bar: barProp,
  baz: bazProp,
};

I wonder if it would be useful to be able to do something more like:

Foo.propTypes = {
  ...mutuallyExclusiveTrueProps('foo', 'bar', 'baz'),
};

And, if you wanted to also have this generate the defaultProps for you:

Foo.propTypes = {
  ...mutuallyExclusiveTrueProps('foo', 'bar', 'baz').propTypes,
};
Foo.defaultProps = {
  ...mutuallyExclusiveTrueProps('foo', 'bar', 'baz').defaultProps,
};

or, obviously:

const fooProps = mutuallyExclusiveTrueProps('foo', 'bar', 'baz');
Foo.propTypes = {
  ...fooProps.propTypes,
};
Foo.defaultProps = {
  ...fooProps.defaultProps,
};

The main drawback to this that I think of right now is that it might not work so well with some of the React lint rules.

I'm not sure what this method should be named, or perhaps if we add propTypes and defaultProps, we could just attach those to the return value of the current method as a convenience.

@pillowfication
Copy link

Here's another possible option you can use currently:

const propNames = [ 'foo', 'bar', 'baz' ]
Component.propTypes = propNames.reduce((acc, prop) => (
  { ...acc, [prop]: mutuallyExclusiveProps(validator, ...propNames) }
), { /* additional props */ })

@ljharb
Copy link
Owner

ljharb commented Apr 6, 2019

Also

const propNames = [ 'foo', 'bar', 'baz' ]
Component.propTypes = {
  ...Object.fromEntries(propNames.map(prop => [
    prop,
    mutuallyExclusiveProps(validator, ...propNames),
  ])),
  // additional propTypes
};

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

No branches or pull requests

3 participants