Skip to content

Commit c5583bc

Browse files
committed
Add dynamicPlaceholder prop to set placeholder as a sample number from the current country
1 parent 55a6884 commit c5583bc

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ Read more on `vue-form-generator`'s [instruction page](https://icebob.gitbooks.i
115115
| `dropdownOptions` | `Object` | `{ disabledDialCode: false, tabindex: 0 }` | Options for dropdown, supporting `disabledDialCode` and `tabindex`|
116116
| `inputOptions` | `Object` | `{ showDialCode: false, tabindex: 0 }` | Options for input, supporting `showDialCode` (always show dial code in the input) and `tabindex`|
117117
| `validCharactersOnly` | `Boolean` | `false` | Only allow valid characters in a phone number (will also verify in `mounted`, so phone number with invalid characters will be shown as an empty string) |
118-
| `mode` | `String` | `''` | Format number to `'International'` (with + dial code) or `'National'` (with 0...) |
118+
| `mode` | `String` | `''` | Format number to `'International'` (with + dial code) or `'National'` (with 0...), available from [v3.1.0](https://github.com/EducationLink/vue-tel-input/releases/tag/v3.1.0) |
119+
| `dynamicPlaceholder` | `Boolean` | `false` | Placeholder as a sample phone number in the current country, available from [v3.1.0](https://github.com/EducationLink/vue-tel-input/releases/tag/v3.1.0) |
119120

120121
### Events
121122

@@ -128,7 +129,7 @@ Read more on `vue-form-generator`'s [instruction page](https://icebob.gitbooks.i
128129
| `enter` | | Fires on keyup.enter event | `onEnter` deprecated |
129130
| `open` | | Fires when the flags dropdown opens | |
130131
| `close` | | Fires when the flags dropdown closes | |
131-
| `country-changed` | `Object` | Fires when country changed (even for the first time) | Available from `v2.4.2` |
132+
| `country-changed` | `Object` | Fires when country changed (even for the first time) | Available from [v2.4.2](https://github.com/EducationLink/vue-tel-input/releases/tag/v2.4.2) |
132133

133134
### Slots
134135
| Slot | Description | Notes |

src/components/vue-tel-input.vue

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<input
3737
ref="input"
3838
v-model="phone"
39-
:placeholder="placeholder"
39+
:placeholder="parsedPlaceholder"
4040
:disabled="disabled"
4141
:required="required"
4242
:autocomplete="autocomplete"
@@ -284,18 +284,33 @@ export default {
284284
type: Boolean,
285285
default: false,
286286
},
287+
dynamicPlaceholder: {
288+
type: Boolean,
289+
default: false,
290+
},
287291
},
288292
data() {
289293
return {
290294
phone: '',
291295
activeCountry: { iso2: '' },
292296
open: false,
297+
finishMounted: false,
293298
selectedIndex: null,
294299
typeToFindInput: '',
295300
typeToFindTimer: null,
296301
};
297302
},
298303
computed: {
304+
parsedPlaceholder() {
305+
if (!this.finishMounted) {
306+
return '';
307+
}
308+
if (this.dynamicPlaceholder) {
309+
const mode = this.mode || 'international';
310+
return PhoneNumber.getExample(this.activeCountry.iso2, 'mobile').getNumber(mode);
311+
}
312+
return this.placeholder;
313+
},
299314
parsedMode() {
300315
if (this.mode) {
301316
if (!['international', 'national'].includes(this.mode)) {
@@ -379,16 +394,21 @@ export default {
379394
},
380395
},
381396
mounted() {
382-
this.initializeCountry().then(() => {
383-
if (!this.phone
397+
this.initializeCountry()
398+
.then(() => {
399+
if (!this.phone
384400
&& this.inputOptions
385401
&& this.inputOptions.showDialCode
386-
&& this.activeCountry) {
387-
this.phone = `+${this.activeCountry.dialCode}`;
388-
}
389-
this.$emit('validate', this.phoneObject);
390-
this.$emit('onValidate', this.phoneObject); // Deprecated
391-
}).catch(console.error); // eslint-disable-line
402+
&& this.activeCountry.dialCode) {
403+
this.phone = `+${this.activeCountry.dialCode}`;
404+
}
405+
this.$emit('validate', this.phoneObject);
406+
this.$emit('onValidate', this.phoneObject); // Deprecated
407+
})
408+
.catch(console.error)
409+
.finally(() => {
410+
this.finishMounted = true;
411+
});
392412
},
393413
created() {
394414
if (this.value) {

0 commit comments

Comments
 (0)