Skip to content

Commit

Permalink
Merge pull request #37 from pdud/pd-glimmer
Browse files Browse the repository at this point in the history
feat: octanify
  • Loading branch information
arenoir authored Jan 17, 2023
2 parents fac0355 + 3478402 commit dba13b3
Show file tree
Hide file tree
Showing 28 changed files with 515 additions and 358 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ jobs:
try-scenario:
- ember-lts-3.24
- ember-lts-3.28
# - ember-release
# - ember-beta
# - ember-canary
- ember-release
- ember-beta
- ember-canary
- ember-classic
- ember-default-with-jquery
- embroider-safe
Expand Down
32 changes: 0 additions & 32 deletions .jshintrc

This file was deleted.

81 changes: 39 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ ember install ember-credit-cards
Usage (Components)
------------------------------------------------------------------------------

### credit-card-form
### CreditCardForm
Full credit card form with validations and formatting.

Attributes:
Arguments:
* number
* name
* month
Expand All @@ -41,47 +41,47 @@ Attributes:
* zipcodeRequired

Events:
* onUpdate
* onValidate


Example:

``` html
//templates/credit-cards/new.hbs

<div>
{{ credit-card-form
number=attrs.number
name=attrs.name
month=attrs.month
year=attrs.year
cvc=attrs.cvc
onValidate=(mut disabled)
}}
</div>

<button {{action "save"}} disabled={{disabled}}>
Save
</button>

``` hbs
<!-- templates/credit-cards/new.hbs -->
<div>
<CreditCardForm
@number={{this.number}}
@name={{this.name}}
@month={{this.month}}
@year={{this.year}}
@cvc={{this.cvc}}
@onUpdate={{this.setValue}}
@onValidate={{mut disabled}}
/>
</div>
<button {{action "save"}} disabled={{disabled}}>
Save
</button>
```



### input-credit-card-number
### `<InputCreditCardNumber />`
Formats credit card number on entry. Discards non-numeric and extra characters. Parses sets number attribute.

Attributes:
* number

### input-credit-card-cvc
### `<InputCreditCardCvc />`

Formats cvc number on entry. Discards non-numeric and extra characters. Parses sets cvc attribute.

Attributes:
* cvc

### input-credit-card-expiration
### `<InputCreditCardExpiration />`

Validates and formats expiration date. Discards non-numeric and extra characters. Parses and sets month, year attributes.

Expand All @@ -90,7 +90,7 @@ Attributes:
* year


### input-credit-card-zipcode
### `<InputCreditCardZipcode />`

Validates and formats zip code. Discards non-numeric and extra characters. Sets zipcode attribute.

Expand Down Expand Up @@ -175,26 +175,23 @@ validations.validateZipcode('94611-24'); //=> false

## Custom Labels


You can provide custom labels for localization.

``` html
<div>
{{
credit-card-form
number=ccNumber
name=ccName
month=ccMonth
year=ccYear
cvc=ccCvc
numberLabel=(t 'credit-card-form.number')
securityCodeLabel=(t 'credit-card-form.security-code')
nameOnCardLabel=(t 'credit-card-form.name-on-card')
expirationLabel=(t 'credit-card-form.expiration')
zipCodeLabel=(t 'credit-card-form.zip-code')
on-validate='ccValidate'
}}
</div>
``` hbs
<CreditCardForm
@number={{this.number}}
@name={{this.name}}
@month={{this.month}}
@year={{this.year}}
@cvc={{this.cvc}}
@numberLabel={{t 'credit-card-form.number'}}
@securityCodeLabel={{t 'credit-card-form.security-code'}}
@nameOnCardLabel={{t 'credit-card-form.name-on-card'}}
@expirationLabel={{t 'credit-card-form.expiration'}}
@zipCodeLabel={{t 'credit-card-form.zip-code'}}
@onUpdate={{this.setValue}}
@onValidate={{mut disabled}}
/>
```

Contributing
Expand Down
56 changes: 56 additions & 0 deletions addon/components/credit-card-form.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<form class="credit-card-form {{if this.isValid 'isValid'}}" ...attributes>
<div class="clearfix">
<div class="{{if this.numberValid 'has-success'}} form-group cc-number">
<label class="control-label">{{if @numberLabel @numberLabel 'Card Number'}}</label>
<InputCreditCardNumber
@number={{@number}}
@onUpdate={{fn this.setValue 'number'}}
class="form-control"
/>
</div>

<div class="{{if this.cvcValid 'has-success'}} form-group cc-cvc">
<label class="control-label">{{if @securityCodeLabel @securityCodeLabel 'Security Code'}}</label>
<InputCreditCardCvc
@cvc={{@cvc}}
@onUpdate={{fn this.setValue 'cvc'}}
class="form-control"
/>
</div>
</div>

<div class="clearfix">
<div class="{{if this.nameValid 'has-success'}} form-group cc-name">
<label class="control-label">{{ if @nameOnCardLabel @nameOnCardLabel 'Name on Card'}}</label>
{{!-- template-lint-disable require-input-label --}}
<Input @type="text" @value={{@name}} class="form-control" />
</div>

<div class="{{if this.expirationValid 'has-success'}} form-group cc-expiration">
<label class="control-label">{{ if @expirationLabel @expirationLabel 'Expiration'}}</label>
<InputCreditCardExpiration
@month={{@month}}
@year={{@year}}
@onUpdateMonth={{fn this.setValue 'month'}}
@onUpdateYear={{fn this.setValue 'year'}}
class="form-control"
/>
</div>
</div>

{{#if @zipcodeRequired}}
<div class="{{if this.zipcodeValid 'has-success'}} form-group cc-zipcode">
<label class="control-label">{{ if @zipCodeLabel @zipCodeLabel 'Zip Code'}}</label>
<InputCreditCardZipcode
@zipcode={{@zipcode}}
@onUpdate={{fn this.setValue "zipcode"}}
class="form-control"
/>
</div>
{{/if}}


<div class="type">
{{this.type}}
</div>
</form>
100 changes: 44 additions & 56 deletions addon/components/credit-card-form.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,75 @@
/* eslint-disable ember/require-tagless-components */
/* eslint-disable ember/no-classic-components */
/* eslint-disable ember/no-classic-classes */
/* eslint-disable ember/no-observers */

import { and } from '@ember/object/computed';
import Component from '@ember/component';
import { computed, observer } from '@ember/object';
import Component from '@glimmer/component';
import { action } from '@ember/object';
import Validations from 'ember-credit-cards/utils/validations';
import Cards from 'ember-credit-cards/utils/cards';

export default Component.extend({
tagName: 'form',
classNames: ['credit-card-form'],
classNameBindings: ['isValid'],
name: null,
number: null,
month: null,
year: null,
cvc: null,
zipcode: null,
zipcodeRequired: false,
onValidate() {},

isValid: and(
'nameValid',
'numberValid',
'expirationValid',
'cvcValid',
'zipcodeValid'
),

becameValid: observer('isValid', function () {
this.onValidate(this.isValid);
}),

nameValid: computed('name', function () {
var name = this.name;
export default class CreditCardFormComponent extends Component {
get isValid() {
return (
this.nameValid &&
this.numberValid &&
this.expirationValid &&
this.cvcValid &&
this.zipcodeValid
);
}

get nameValid() {
var name = this.args.name;

if (name) {
return true;
}

return false;
}),

numberValid: computed('number', function () {
var number = this.number;
}

return Validations.validateNumber(number);
}),
get numberValid() {
return Validations.validateNumber(this.args.number);
}

expirationValid: computed('month', 'year', function () {
var month = this.month;
var year = this.year;
get expirationValid() {
var month = this.args.month;
var year = this.args.year;

return Validations.validateExpiration(month, year);
}),
}

cvcValid: computed('cvc', 'type', function () {
var cvc = this.cvc;
get cvcValid() {
var cvc = this.args.cvc;
var type = this.type;

return Validations.validateCVC(cvc, type);
}),
}

zipcodeValid: computed('zipcodeRequired', 'zipcode', function () {
if (this.zipcodeRequired) {
var zip = this.zipcode;
get zipcodeValid() {
if (this.args.zipcodeRequired) {
var zip = this.args.zipcode;

return Validations.validateZipcode(zip);
}

return true;
}),
}

type: computed('number', function () {
var number = this.number;
get type() {
var number = this.args.number;
var card = Cards.fromNumber(number);

if (card) {
return card.type;
} else {
return '';
}
}),
});
}

@action
setValue(key, value) {
this.args.onUpdate(key, value);
this._onValidate();
}

_onValidate() {
(this.args.onValidate || (() => {}))(this.isValid);
}
}
8 changes: 8 additions & 0 deletions addon/components/input-credit-card-cvc.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Input
@type="tel"
@value={{this.cvc}}
class="input-credit-card-cvc"
placeholder="•••"
...attributes
{{on "keypress" this.keyPress}}
/>
Loading

0 comments on commit dba13b3

Please sign in to comment.