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

Add getDefaultProps Functionality #4

Closed
evrowe opened this issue May 23, 2017 · 2 comments
Closed

Add getDefaultProps Functionality #4

evrowe opened this issue May 23, 2017 · 2 comments

Comments

@evrowe
Copy link
Member

evrowe commented May 23, 2017

We need to set up the ability to set default values for properties passed into components. These defaults will only be utilized in cases where a developer has not supplied a value to any properties which have defaults defined.

Utilizing this should be similar to defining prop types; on a component, an object that defines default prop values can be created which will automatically get looked up by getDefaultProps. The Component code will automatically take care of the work of checking for the default values and using them when no value has been passed in.

Example:

import Component from 'ember-component';
import hbs from 'htmlbars-inline-precompile';
import PropTypes from 'prop-types';

export default Component.extend({
  
  defaultProps: {
    bar: 5,
    greeting: 'Hello, ',
    name: 'smart and attractive developer'
  }

  propTypes: {
    bar: PropTypes.number,
    greeting: PropTypes.string,
    name: PropTypes.string
  },

  layout: hbs`{{greeting}}{{name}}! You are looking like {{bar}} million major currency units today!`

});

Under the hood, we will need to make additions to our Component initializer to set the default values from defaultProps to their corresponding properties on the component itself.

@DHedgecock
Copy link
Member

For maximum perf reasons the props validations are only run in dev builds (we don't ship the validators in prod builds). For this reason we cannot set defaults when a prop is invalid.

I think we should mirror the React pattern here, which is to set a default prop when a prop is undefined during initialization of component.

Thoughts?

@evrowe
Copy link
Member Author

evrowe commented May 24, 2017 via email

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

2 participants