Skip to content

support Object.keys within oneOf #153

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

Closed
chrisdrackett opened this issue Jan 30, 2017 · 12 comments · Fixed by #211
Closed

support Object.keys within oneOf #153

chrisdrackett opened this issue Jan 30, 2017 · 12 comments · Fixed by #211

Comments

@chrisdrackett
Copy link

I'd like to use Object.keys to generate the list of values possible for oneOf but have found that this currently does not work. Has anyone run into this before? Not sure if this is a feature request or if there is a better way I could be writing my propType.

@fkling
Copy link
Member

fkling commented Jan 30, 2017

Can you give a concrete example and the result you expect? We could certainly special case Object.keys, but only if the passed value can also be statically resolved to an object literal.

@chrisdrackett
Copy link
Author

sure, this is what I'm doing:

In an external file that is shared by a couple components:

const status = {
  info: {
    color: 'gray',
    icon: 'info',
  },
  normal: {
    color: 'green',
    icon: 'check',
  },
  warning: {
    color: 'orange',
    icon: 'warning',
  },
  error: {
    color: 'red',
    icon: 'x',
  },
}

const statusValues = Object.keys(status)

In a component that accepts one of the statuses listed above:

status: PropTypes.oneOf(statusValues)

I'm looking for the list ['info', 'normal', 'warning', 'error']

@fkling
Copy link
Member

fkling commented Jan 30, 2017

react-docgen doesn't analyze code across files... however, depending on what exactly you are doing with react-docgen, you could get the list of keys in a different way (e.g. by actually loading that file) and insert the values into the documentation object that react-docgen produces.

@chrisdrackett
Copy link
Author

ok, I'm using react-styleguidist which uses docgen. I'll see if I can figure out another method (or just write them by hand again)

@chrisdrackett
Copy link
Author

also, of note, I did just try and do this with an object in the same file and I still get this output. So while this might not ever work for an object from an external file it might still be nice for something within the same file.

@fkling
Copy link
Member

fkling commented Jan 31, 2017

Yeah, that's what I meant when I said that we could special case Object.keys(<resolves to object literal>) being passed to .oneOf.

@chrisdrackett
Copy link
Author

ah, right, I have the memory of a goldfish. carry on!

@motleydev
Copy link

I would like this as well. Seems that variables should be evaluated before the docs get "scraped" from the file.

@MaxInMoon
Copy link

+1

@dmitry-zaets
Copy link

dmitry-zaets commented Aug 24, 2017

Related to #122

@andreapiras
Copy link

If props are imported from external file, values are still not displayed.
You get something like this "value": "status"
Is there any workaround for this?

@gaurav5430
Copy link

any updates on this?

Object.keys seems to work if the object is declared in the same file, but not when imported.

Would this be fixed when #352 is merged ?

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants