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

Property definition with reusable propTypes #32

Open
SoftHai opened this issue Apr 17, 2020 · 1 comment
Open

Property definition with reusable propTypes #32

SoftHai opened this issue Apr 17, 2020 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@SoftHai
Copy link

SoftHai commented Apr 17, 2020

Hi,

I'm trying the following:
I have some propTypes I want to define central and reuse it in many components.
There fore I have create a file "baseTypes"

import PropTypes from 'prop-types'

export const baseTypes = {
    alignSelf: PropTypes.oneOf(['start', 'center', 'end', 'stretch'])
}

Now I want to use this definition in many other components like this:

import * as React from 'react'
import PropTypes from 'prop-types'
import { baseTypes } from './propTypeDefinitions'

class Box extends React.Component {

    static propTypes =  {
        align: PropTypes.oneOf([
            'start',
            'center',
            'end',
            'baseline',
            'stretch',
          ]),
          alignContent: PropTypes.oneOf([
            'start',
            'center',
            'end',
            'between',
            'around',
            'stretch',
          ]),
          alignSelf: baseTypes.alignSelf
    }

    render() {
       //...
    }
}

How ever, only the 2 in the file defined properties are shown in the props panel. alignSelf (defined in another file) is not show.
image

How are you reading the props definitions?
By parsing the source code or by using Component.propTypes?
Looks for my like toe first one is true.

There reason why I have this issue is, that I using a UI Framework. They already have created all this propTypes definitions and I just want to reuse it instead of copy and paste it to my source code.
But for any reason it don't works as expected.

@ipselon
Copy link
Contributor

ipselon commented Apr 17, 2020

@SoftHai,

Here is how you can reuse PropTypes:

export const baseTypes = {
    alignSelf: PropTypes.oneOf(['start', 'center', 'end', 'stretch'])
}
    static propTypes =  {
        align: PropTypes.oneOf([
            'start',
            'center',
            'end',
            'baseline',
            'stretch',
          ]),
          alignContent: PropTypes.oneOf([
            'start',
            'center',
            'end',
            'between',
            'around',
            'stretch',
          ]),
          additionalAlign: PropTypes.shape(baseTypes),
    }

@ipselon ipselon added the documentation Improvements or additions to documentation label Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants