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

Feature Request: Prop default value & validation & required #236

Closed
MatanYadaev opened this issue Oct 11, 2017 · 4 comments
Closed

Feature Request: Prop default value & validation & required #236

MatanYadaev opened this issue Oct 11, 2017 · 4 comments

Comments

@MatanYadaev
Copy link

Prop should have the same propeties that props in vue has
type (already have)
required
default (should be callback)
validation

@Hotell
Copy link
Contributor

Hotell commented Oct 12, 2017

All of this is already within stencil :)

default values

@Component({ tag: 'x-greet' })
class Hello {
  @Prop() who = 'World'
  render(){  return <div>Hello  {this.who} !</div>
}
<x-greet></x-greet>

will render -> <x-greet><div>Hello World !</div></x-greet>

required / validations

@Component({ tag: 'x-greet' })
class Hello {
  @Prop() who: string
  @PropWillChange('who')
  validateWho(newValue: string) {
    const isBlank = typeof newValue == null
    const atLeast2chars = typeof newValue === 'string' && newValue.length >= 2
    if (isBlank){ throw new Error('who: required') } 
    if ( !atLeast2chars ) { throw new Error('who: atLeast2chars')  }
  }
}

also I would definitely enable those validations only for Development env

@adamdbradley
Copy link
Contributor

Right, thanks @Hotell!

@adamdbradley
Copy link
Contributor

Reminds me to add this to the docs.

@leonheess
Copy link

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

4 participants