Skip to content

Commit 1fc3767

Browse files
committed
[feature] Add required option
1 parent e02d847 commit 1fc3767

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

src/App.vue

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@
7373
:dark="darkMode"
7474
type="number"
7575
/>
76+
<br>
77+
<VueInputUi
78+
v-model="value9"
79+
label="Required input"
80+
hint="is required"
81+
:dark="darkMode"
82+
required
83+
/>
7684
</div>
7785
</div>
7886
</div>
@@ -96,6 +104,7 @@
96104
value6: 'Hello world!',
97105
value7: null,
98106
value8: null,
107+
value9: null,
99108
darkMode: false
100109
}
101110
}

src/VueInputUi/index.vue

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
'has-error': error,
88
'is-disabled': disabled,
99
'is-dark': dark,
10-
'is-focused': valid
10+
'is-focused': valid,
11+
'has-hint': hint && !value
1112
}, size]"
1213
class="field"
1314
@click="focusInput"
@@ -16,25 +17,26 @@
1617
:id="id"
1718
ref="VueInputUi"
1819
v-model="inputValue"
19-
:placeholder="label"
20+
:placeholder="labelValue"
2021
:disabled="disabled"
2122
:style="[borderStyle]"
2223
:type="type"
2324
class="field-input"
2425
:readonly="readonly"
26+
:required="required"
2527
@focus="onFocus"
2628
@blur="onBlur"
2729
@click="$emit('click')"
2830
>
2931
<label
3032
ref="label"
3133
:for="id"
32-
:class="error ? 'text-danger' : null"
34+
:class="error ? 'lm-text-danger' : null"
3335
:style="[colorStyle]"
3436
class="field-label"
3537
@click="focusInput"
3638
>
37-
{{ hint || label }}
39+
{{ hintValue || labelValue }}
3840
</label>
3941
</div>
4042
</template>
@@ -55,7 +57,8 @@
5557
type: { type: String, default: 'text' },
5658
readonly: { type: Boolean, default: false },
5759
valid: { type: Boolean, default: false },
58-
validColor: { type: String, default: 'yellowgreen' }
60+
validColor: { type: String, default: 'yellowgreen' },
61+
required: { type: Boolean, default: false }
5962
},
6063
data: function () {
6164
return {
@@ -82,6 +85,20 @@
8285
set (value) {
8386
this.$emit('input', value)
8487
}
88+
},
89+
labelValue () {
90+
let label = this.label
91+
if (this.required && label) {
92+
label += ` *`
93+
}
94+
return label
95+
},
96+
hintValue () {
97+
let hint = this.hint
98+
if (this.required && hint) {
99+
hint += ` *`
100+
}
101+
return hint
85102
}
86103
},
87104
methods: {
@@ -123,7 +140,7 @@
123140
}
124141
.field-label{
125142
position: absolute;
126-
top: 3px;
143+
top: 4px;
127144
cursor: pointer;
128145
left: 13px;
129146
-webkit-transform: translateY(25%);
@@ -152,11 +169,6 @@
152169
font-size: 14px;
153170
z-index: 0;
154171
}
155-
&.has-error {
156-
.field-input {
157-
border-color: orangered !important;
158-
}
159-
}
160172
&.has-value {
161173
.field-label {
162174
opacity: 1;
@@ -168,6 +180,16 @@
168180
padding-top: 14px;
169181
}
170182
}
183+
&.has-hint {
184+
.field-label{
185+
opacity: 1;
186+
transform: translateY(0);
187+
font-size: 11px;
188+
}
189+
.field-input {
190+
padding-top: 14px;
191+
}
192+
}
171193
&.is-focused {
172194
.field-input {
173195
border-color: dodgerblue;
@@ -176,6 +198,11 @@
176198
color: dodgerblue;
177199
}
178200
}
201+
&.has-error {
202+
.field-input {
203+
border-color: orangered !important;
204+
}
205+
}
179206
&.is-disabled {
180207
.field-input {
181208
border-color: #CCC;
@@ -185,7 +212,7 @@
185212
cursor: default;
186213
}
187214
}
188-
.text-danger {
215+
.lm-text-danger {
189216
color: orangered !important;
190217
}
191218
&.is-dark {

0 commit comments

Comments
 (0)