|
| 1 | +<!-- Demo vue3 app for testing purposes --> |
| 2 | +<!-- Examples copied from the component markdown --> |
| 3 | +<template> |
| 4 | + <div id="app"> |
| 5 | + <div id="value-display-div"> |
| 6 | + <checkbox v-model="masked" /> |
| 7 | + </div> |
| 8 | + |
| 9 | + <h1>Basic Usage</h1> |
| 10 | + <p><code>lazy</code> custom v-model modifier will only emit input event on change event.</p> |
| 11 | + <field label="US Phone Number"> |
| 12 | + <input-facade mask="(###) ### - ####" v-model.lazy="basicUsageValue" :masked="masked" /> |
| 13 | + </field> |
| 14 | + <display :value="basicUsageValue" /> |
| 15 | + |
| 16 | + <h1>Optional character</h1> |
| 17 | + <p> |
| 18 | + Use a question mark (?) to indicate that a character is optional. Similar to regular expression this means 0 or 1. |
| 19 | + </p> |
| 20 | + <field label="IP address"> |
| 21 | + <input-facade |
| 22 | + name="ip" |
| 23 | + mask="##?#?.##?#?.##?#?.##?#?" |
| 24 | + v-model="optionalCharvalue" |
| 25 | + :masked="masked" |
| 26 | + :formatter="validateIP" |
| 27 | + /> |
| 28 | + </field> |
| 29 | + <display :value="optionalCharvalue" /> |
| 30 | + |
| 31 | + <h1>Repeating character</h1> |
| 32 | + <p> |
| 33 | + Use an asterisk (*) as a suffix to set a masking character as repeating, similar to regular expression. Note that |
| 34 | + this means that 0 or more of said character will match. If you need to match 1 or more than you must specify it. |
| 35 | + </p> |
| 36 | + <field label="One or more numbers"> |
| 37 | + <input-facade mask="##* AA" v-model="repeatingCharValue" :masked="masked" /> |
| 38 | + </field> |
| 39 | + <display :value="repeatingCharValue" /> |
| 40 | + |
| 41 | + <h1>Alternation (Pipe)</h1> |
| 42 | + <p> |
| 43 | + Use a pipe symbol to indicate altarnative <b>static</b> values that can be used in the mask. This is case |
| 44 | + insensitive and can match letters irregarless of accents. For example å = A. Android webview and Opera dont fully |
| 45 | + support that type of matching. |
| 46 | + <i> |
| 47 | + Note that because this only works with static values there is no need to escape characters that are also used as |
| 48 | + tokens. |
| 49 | + </i> |
| 50 | + </p> |
| 51 | + <field label="ID Code"> |
| 52 | + <input-facade mask="A|B|C-####" v-model="alternationValue" :masked="masked" /> |
| 53 | + </field> |
| 54 | + <display :value="alternationValue" /> |
| 55 | + |
| 56 | + <h1>Dynamic Masks</h1> |
| 57 | + <p> |
| 58 | + Accepts an array of masking pattern and dynamically chooses the appropriate one based on the number of characters |
| 59 | + in the field. |
| 60 | + </p> |
| 61 | + <field label="US Zip Code"> |
| 62 | + <input-facade v-model="USPostal" :mask="['#####', '#####-####']" :masked="masked" /> |
| 63 | + </field> |
| 64 | + |
| 65 | + <field label="UK Postal Code"> |
| 66 | + <input-facade v-model="UKPostal" :mask="['A# #AA', 'AXX #AA', 'AA#X #AA']" :masked="masked" /> |
| 67 | + </field> |
| 68 | + |
| 69 | + <display label="Zip Code" :value="USPostal" /> |
| 70 | + <display label="Postal Code" :value="UKPostal" /> |
| 71 | + |
| 72 | + <h1>Custom Tokens</h1> |
| 73 | + <p> |
| 74 | + You can override the tokens on a per field basis. Just pass in your own token definition to the field. This can |
| 75 | + also be used to add internatilization support. |
| 76 | + </p> |
| 77 | + <field label="Hex Color"> |
| 78 | + <input-facade mask="\#FFFFFF" :tokens="hexTokens" :masked="masked" v-model="customTokenValue" /> |
| 79 | + </field> |
| 80 | + <display :value="customTokenValue" /> |
| 81 | + |
| 82 | + <h1>Post masking input formatter</h1> |
| 83 | + <p> |
| 84 | + Returning a string in the format function will re-run that value through the masker routine, Ensuring that the end |
| 85 | + result still confirms to the mask. |
| 86 | + </p> |
| 87 | + <field label="Date as MM/YY"> |
| 88 | + <input-facade v-model="formatterValue" mask="##/##" :formatter="date" /> |
| 89 | + </field> |
| 90 | + <display :value="formatterValue" /> |
| 91 | + <p> |
| 92 | + Returning a boolean `true` will leave the masked or unmasked value as is, the value is passed by reference so if |
| 93 | + you modify them here, that will be their final value. However if a `false` is returned, the user's input will be |
| 94 | + ignored and the value will remain as it was prior. |
| 95 | + </p> |
| 96 | + <field label="Enter an even num"> |
| 97 | + <input-facade v-model="boolFormatterValue" mask="#########" :formatter="evenMoney" masked /> |
| 98 | + </field> |
| 99 | + <display :value="boolFormatterValue" /> |
| 100 | + </div> |
| 101 | +</template> |
| 102 | + |
| 103 | +<script> |
| 104 | +import InputFacade from '../src/component.vue' |
| 105 | +import Checkbox from '../styleguide/components/Checkbox.vue' |
| 106 | +import Display from '../styleguide/components/Display.vue' |
| 107 | +import Field from '../styleguide/components/Field.vue' |
| 108 | + |
| 109 | +export default { |
| 110 | + name: 'App', |
| 111 | + components: { |
| 112 | + Checkbox, |
| 113 | + Display, |
| 114 | + Field, |
| 115 | + InputFacade |
| 116 | + }, |
| 117 | + data() { |
| 118 | + return { |
| 119 | + basicUsageValue: '', |
| 120 | + optionalCharvalue: '', |
| 121 | + repeatingCharValue: '', |
| 122 | + alternationValue: '', |
| 123 | + USPostal: '', |
| 124 | + UKPostal: '', |
| 125 | + customTokenValue: '', |
| 126 | + formatterValue: '', |
| 127 | + boolFormatterValue: '', |
| 128 | + masked: true, |
| 129 | + hexTokens: { |
| 130 | + F: { |
| 131 | + pattern: /[0-9A-F]/i, |
| 132 | + transform: (v) => v.toLocaleUpperCase() |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + }, |
| 137 | + methods: { |
| 138 | + date(value, event) { |
| 139 | + // do not format on deletion, this could leave the input in bad state |
| 140 | + // but allows user to delete the leading 0 if needed for some reason |
| 141 | + if (event.inputType !== 'deleteContentBackward') { |
| 142 | + const [month] = value.masked.split('/') |
| 143 | + |
| 144 | + if (month > 12) { |
| 145 | + return '0' + value.unmasked |
| 146 | + } |
| 147 | + } |
| 148 | + }, |
| 149 | + evenMoney(value, event) { |
| 150 | + if (event.data && event.data % 2 !== 0) { |
| 151 | + // odd number, ignore it |
| 152 | + return false |
| 153 | + } else if (value.unmasked) { |
| 154 | + const formatted = value.unmasked.match(/\d{1,3}/g).join(',') |
| 155 | + value.masked = `$${formatted}` |
| 156 | + return true |
| 157 | + } |
| 158 | + }, |
| 159 | + validateIP(value) { |
| 160 | + const parts = value.masked.split('.') |
| 161 | + |
| 162 | + if (parts.length < 4 && parts[parts.length - 1] > 25) { |
| 163 | + return value.masked + '.' |
| 164 | + } |
| 165 | + |
| 166 | + return !parts.some((part) => part > 255) |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | +</script> |
| 171 | + |
| 172 | +<style scoped> |
| 173 | +#value-display-div { |
| 174 | + position: fixed; |
| 175 | + background-color: white; |
| 176 | + border: solid 0.75px black; |
| 177 | + border-radius: 5px; |
| 178 | + padding: 5px; |
| 179 | + box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.5); |
| 180 | + top: 15px; |
| 181 | + right: 15px; |
| 182 | +} |
| 183 | +code { |
| 184 | + padding: 2px; |
| 185 | + border-radius: 2px; |
| 186 | + display: inline-block; |
| 187 | + background-color: #eee; |
| 188 | +} |
| 189 | +</style> |
0 commit comments