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

Make input field bind to required HTML attribute #178

Merged
merged 2 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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