Skip to content

Commit

Permalink
Merge pull request #178 from samdemaeyer/add-required-functionality
Browse files Browse the repository at this point in the history
Make input field bind to required HTML attribute
  • Loading branch information
dcyriller authored Jan 16, 2020
2 parents c2afcf7 + 71de005 commit 002758b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
11 changes: 10 additions & 1 deletion addon/components/phone-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { isPresent } from '@ember/utils';
allowDropdown=false
autoPlaceholder='aggressive'
disabled=true
required=required
initialCountry='fr'
number='123'
onlyCountries=europeanCountries
Expand All @@ -25,7 +26,7 @@ import { isPresent } from '@ember/utils';
export default Component.extend({
tagName: 'input',

attributeBindings: ['type', 'disabled'],
attributeBindings: ['type', 'disabled', 'required'],
type: 'tel',

phoneInput: service(),
Expand All @@ -43,6 +44,14 @@ export default Component.extend({
*/
this.disabled = this.disabled || false;

/**
* Setting this to true will make the input field required. This will enabled client side form validation.
* Defaults to `false`
* @argument required
* @type {boolean}
*/
this.required = this.required || false;

/**
The international phone number. This is the main data supposed
to be persisted / handled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export default Component.extend({
updateSeparateDialOption(separateDialNumber, metaData) {
this.set('separateDialNumber', separateDialNumber);
this.setProperties(metaData);
},

submitForm() {
alert('The form has been submitted');
}
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,21 @@

{{demo.snippet "phone-input-allow-dropdown-option.hbs"}}
{{/docs-demo}}

<h2>`required` option</h2>
{{#docs-demo as |demo|}}
{{#demo.example name="phone-input-required-option.hbs"}}
<form {{action "submitForm" on="submit"}}>
<p>{{phone-input required=true initialCountry="us" number=number}}</p>

<p>The input is required.</p>

<p>The input is required and you will not be able to submit the form when the field is empty as the clients-side HTML5 form validation will prvent it.</p>

<button name="button" type="submit" class="docs-bg-grey-darkest docs-text-white docs-no-underline docs-rounded docs-py-2 docs-px-4">submit</button>
</form>
{{/demo.example}}

{{demo.snippet "phone-input-required-option.hbs"}}
{{/docs-demo}}

12 changes: 12 additions & 0 deletions tests/integration/components/phone-input-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ module('Integration | Component | phone-input', function(hooks) {
assert.ok(find('input').disabled);
});

test('can be required', async function(assert) {
this.set('number', null);

await render(hbs`{{phone-input number=number}}`);

assert.notOk(find('input').required);

await render(hbs`{{phone-input required=true number=number}}`);

assert.ok(find('input').required);
});

test('can prevent the dropdown', async function(assert) {
assert.expect(1);

Expand Down

0 comments on commit 002758b

Please sign in to comment.