All Projects → jkusa → Ember Arg Types

jkusa / Ember Arg Types

Licence: mit
Runtime type checking & defaulting for glimmer component arguments powered by prop-types & decorators

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Ember Arg Types

Ember Decorators
Useful decorators for Ember applications.
Stars: ✭ 360 (+737.21%)
Mutual labels:  decorators, ember
Mber
Fast and minimal Ember.js CLI alternative, without broccoli.
Stars: ✭ 30 (-30.23%)
Mutual labels:  ember
Ember Inspector
Adds an Ember tab to the browser's Developer Tools that allows you to inspect Ember objects in your application.
Stars: ✭ 936 (+2076.74%)
Mutual labels:  ember
Ember Paper
The Ember approach to Material Design.
Stars: ✭ 871 (+1925.58%)
Mutual labels:  ember
Ember Cli Mirage
An Ember Addon to easily add Mirage JS to your Ember app.
Stars: ✭ 848 (+1872.09%)
Mutual labels:  ember
Ember Polymer
Use Polymer in your ambitious Ember application! 💎
Stars: ✭ 20 (-53.49%)
Mutual labels:  ember
Active decorator
ORM agnostic truly Object-Oriented view helper for Rails 4, 5, and 6
Stars: ✭ 928 (+2058.14%)
Mutual labels:  decorators
Ember Searchable Select
Data-down, actions up select-like menu with searching and tagging capabilities.
Stars: ✭ 38 (-11.63%)
Mutual labels:  ember
Ember Accessibility
An EmberJS addon to help identify accessibility violations during development
Stars: ✭ 29 (-32.56%)
Mutual labels:  ember
Thinkful Workshop React Redux Node Mongodb Webpack2
Stars: ✭ 12 (-72.09%)
Mutual labels:  decorators
Mongoose Model Decorators
ES2016 decorator functions for building Mongoose models
Stars: ✭ 11 (-74.42%)
Mutual labels:  decorators
Dugong
Minimal State Store Manager for React Apps using RxJS
Stars: ✭ 10 (-76.74%)
Mutual labels:  decorators
Team Time Zone
Distributed teams are awesome. Time zones are awful.
Stars: ✭ 21 (-51.16%)
Mutual labels:  ember
Decorum
Tasteful decorators for Ruby.
Stars: ✭ 7 (-83.72%)
Mutual labels:  decorators
Ember Cli Updater
ember-cli addon to help you update your ember-cli application or addon.
Stars: ✭ 32 (-25.58%)
Mutual labels:  ember
Pretzel
Javascript full-stack framework for Big Data visualisation and analysis
Stars: ✭ 26 (-39.53%)
Mutual labels:  ember
Ember Api Feature Flags
API based, read-only feature flags for Ember
Stars: ✭ 11 (-74.42%)
Mutual labels:  ember
Tvrboreact
Dream starter project: React, Redux, React Router, Webpack
Stars: ✭ 13 (-69.77%)
Mutual labels:  decorators
React Apollo Decorators
Better decorators for Apollo and React
Stars: ✭ 39 (-9.3%)
Mutual labels:  decorators
Injection Js
Dependency injection library for JavaScript and TypeScript in 5.1K. It is an extraction of the Angular's ReflectiveInjector which means that it's well designed, feature complete, fast, reliable and well tested.
Stars: ✭ 962 (+2137.21%)
Mutual labels:  decorators

ember-arg-types

Build Status

Runtime type checking & defaulting for glimmer component arguments powered by prop-types & decorators.

Motivation

ember-arg-types provides a decorator (@arg) that maps glimmer arguments to local component properties. This allows default values and type checking to be easily declared (and documented) in your component JS file.

Example:

@arg(string)
sortBy = 'id';

Instead of this:

get sortBy() {
  const { sortBy='id' } = this.args;
  assert('`sortBy` must be a string', typeof sortBy === 'string');
  return sortBy;
}

Argument Mapping & Default Values

The @arg decorator maps this.args values to local component properties. If a mapped argument has a value of undefined, @arg will return the local property's initializer value.

Here the value of this.sortBy is the value of this.args.sortBy, unless this.args.sortBy is undefined. If undefined, the value of this.sortBy will be 'id'.

@arg
sortBy = 'id';

Here the value of this.id is the value of this.args.id, unless this.args.id is undefined. If undefined, the value of this.id will be computed by the getter.

@arg
get id() {
  return guidFor(this);
}

Type Checking

ember-arg-types uses the popular prop-types library for runtime type checking.

By importing type validators from prop-types, you can specify a type check parameter to @arg:

import Component from '@glimmer/component';
import { arg } from 'ember-arg-types';
import { string } from 'prop-types';

export default class CharacterComponent extends Component {
  // `name` string arg that is required
  @arg(string.isRequired)
  name;
}

Example Type Check Error

{{!-- @name should be a string, not a number --}}
<CharacterComponent @name={{123}}>

Error Example

Prop Type Docs

You can find more information on prop-type validators here: Prop Type Usage Docs

Disable Errors

If an argument value fails a validation check, an Error will be thrown (in non-prod environments) by default. To disable throwing Errors , update your config/environment.js with the following:

'ember-arg-types': {
  // Throw errors instead of logging (default is true)
  throwErrors: false
}

Production

Since component type checks are not typically performed in production, prop-types replaces the library with function shims for production builds, resulting in a smaller bundle size.

Installation

ember install ember-arg-types

Usage

Example Component Definition

// character-component.js

import Component from '@glimmer/component';
import { arg } from 'ember-arg-types';
import { func, number, oneOf, string } from 'prop-types';
import { guidFor } from '@ember/object/internals';

const tunics = ['green', 'red', 'blue'];

export default class CharacterComponent extends Component {
  // `id` string arg with a getter default value
  @arg(string)
  get id() {
    return guidFor(this);
  }

  // `name` string arg that is required
  @arg(string.isRequired)
  name;

  // `title` arg with default value and no type check
  @arg
  title = 'hero';

  // `tunic` arg with set of valid string values and a default
  @arg(oneOf(tunics))
  tunic = tunics[0];

  // `hearts` number arg with default value
  @arg(number)
  hearts = 12;

  // `level` number arg without default value
  @arg(number)
  level;

  // `onClick` action (function) arg with noop default value
  @arg(func)
  onClick = () => null;
}
{{!-- character-component.hbs --}}

{{!-- args are mapped to local properties, thus we use this.<argName> instead of @<argName> --}}
<div class="character" role="button" {{on "click" this.onClick}}>
  <div class="id">{{this.id}}</div>
  <div class="name">{{this.name}}</div>
  <div class="title">{{this.title}}</div>
  <div class="tunic">{{this.tunic}}</div>
  <div class="hearts">{{this.hearts}}</div>
  <div class="level">{{this.level}}</div>
</div>

Example Component Invocation

<Character
  @name="link"
  @title="hero of time"
  @level=2
  @onClick={{this.onClick}}
/>

Compatibility

  • Ember.js v3.8 or above
  • Ember CLI v2.13 or above
  • Node.js v8 or above

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].