summaryrefslogtreecommitdiff
path: root/src/client/app/common/views/components/ui/input.vue
blob: ce28bfb12ac938d739ed25a9d5a421627c1a5aa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<template>
<div class="ui-input" :class="[{ focused, filled }, styl]">
	<div class="icon" ref="icon"><slot name="icon"></slot></div>
	<div class="input">
		<div class="password-meter" v-if="withPasswordMeter" v-show="passwordStrength != ''" :data-strength="passwordStrength">
			<div class="value" ref="passwordMetar"></div>
		</div>
		<span class="label" ref="label"><slot></slot></span>
		<div class="prefix" ref="prefix"><slot name="prefix"></slot></div>
		<template v-if="type != 'file'">
			<input ref="input"
					:type="type"
					v-model="v"
					:required="required"
					:readonly="readonly"
					:pattern="pattern"
					:autocomplete="autocomplete"
					:spellcheck="spellcheck"
					@focus="focused = true"
					@blur="focused = false">
		</template>
		<template v-else>
			<input ref="input"
					type="text"
					:value="placeholder"
					readonly
					@click="chooseFile">
			<input ref="file"
					type="file"
					:value="value"
					@change="onChangeFile">
		</template>
		<div class="suffix" ref="suffix"><slot name="suffix"></slot></div>
	</div>
	<div class="text"><slot name="text"></slot></div>
</div>
</template>

<script lang="ts">
import Vue from 'vue';
const getPasswordStrength = require('syuilo-password-strength');

export default Vue.extend({
	props: {
		value: {
			required: false
		},
		type: {
			type: String,
			required: false
		},
		required: {
			type: Boolean,
			required: false
		},
		readonly: {
			type: Boolean,
			required: false
		},
		pattern: {
			type: String,
			required: false
		},
		autocomplete: {
			required: false
		},
		spellcheck: {
			required: false
		},
		withPasswordMeter: {
			type: Boolean,
			required: false,
			default: false
		}
	},
	data() {
		return {
			v: this.value,
			focused: false,
			passwordStrength: '',
			styl: 'fill'
		};
	},
	computed: {
		filled(): boolean {
			return this.v != '' && this.v != null;
		},
		placeholder(): string {
			if (this.type != 'file') return null;
			if (this.v == null) return null;

			if (typeof this.v == 'string') return this.v;

			if (Array.isArray(this.v)) {
				return this.v.map(file => file.name).join(', ');
			} else {
				return this.v.name;
			}
		}
	},
	watch: {
		value(v) {
			this.v = v;
		},
		v(v) {
			this.$emit('input', v);

			if (this.withPasswordMeter) {
				if (v == '') {
					this.passwordStrength = '';
					return;
				}

				const strength = getPasswordStrength(v);
				this.passwordStrength = strength > 0.7 ? 'high' : strength > 0.3 ? 'medium' : 'low';
				(this.$refs.passwordMetar as any).style.width = `${strength * 100}%`;
			}
		}
	},
	inject: {
		isCardChild: { default: false }
	},
	created() {
		if (this.isCardChild) {
			this.styl = 'line';
		}
	},
	mounted() {
		if (this.$refs.prefix) {
			this.$refs.label.style.left = (this.$refs.prefix.offsetLeft + this.$refs.prefix.offsetWidth) + 'px';
			if (this.$refs.prefix.offsetWidth) {
				this.$refs.input.style.paddingLeft = this.$refs.prefix.offsetWidth + 'px';
			}
		}
		if (this.$refs.suffix) {
			if (this.$refs.suffix.offsetWidth) {
				this.$refs.input.style.paddingRight = this.$refs.suffix.offsetWidth + 'px';
			}
		}
	},
	methods: {
		focus() {
			this.$refs.input.focus();
		},
		chooseFile() {
			this.$refs.file.click();
		},
		onChangeFile() {
			this.v = Array.from((this.$refs.file as any).files);
			this.$emit('input', this.v);
			this.$emit('change', this.v);
		}
	}
});
</script>

<style lang="stylus" scoped>
@import '~const.styl'

root(isDark, fill)
	margin 32px 0

	> .icon
		position absolute
		top 0
		left 0
		width 24px
		text-align center
		line-height 32px
		color isDark ? rgba(#fff, 0.7) : rgba(#000, 0.54)

		&:not(:empty) + .input
			margin-left 28px

	> .input

		if !fill
			&:before
				content ''
				display block
				position absolute
				bottom 0
				left 0
				right 0
				height 1px
				background isDark ? rgba(#fff, 0.7) : rgba(#000, 0.42)

			&:after
				content ''
				display block
				position absolute
				bottom 0
				left 0
				right 0
				height 2px
				background $theme-color
				opacity 0
				transform scaleX(0.12)
				transition border 0.3s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.3s cubic-bezier(0.4, 0, 0.2, 1), transform 0.3s cubic-bezier(0.4, 0, 0.2, 1)
				will-change border opacity transform

		> .password-meter
			position absolute
			top 0
			left 0
			width 100%
			height 100%
			border-radius 6px
			overflow hidden
			opacity 0.3

			&[data-strength='']
				display none

			&[data-strength='low']
				> .value
					background #d73612

			&[data-strength='medium']
				> .value
					background #d7ca12

			&[data-strength='high']
				> .value
					background #61bb22

			> .value
				display block
				width 0%
				height 100%
				background transparent
				border-radius 6px
				transition all 0.1s ease

		> .label
			position absolute
			z-index 1
			top fill ? 6px : 0
			left 0
			pointer-events none
			transition 0.4s cubic-bezier(0.25, 0.8, 0.25, 1)
			transition-duration 0.3s
			font-size 16px
			line-height 32px
			color isDark ? rgba(#fff, 0.7) : rgba(#000, 0.54)
			pointer-events none
			//will-change transform
			transform-origin top left
			transform scale(1)

		> input
			display block
			width 100%
			margin 0
			padding 0
			font inherit
			font-weight fill ? bold : normal
			font-size 16px
			line-height 32px
			color isDark ? #fff : #000
			background transparent
			border none
			border-radius 0
			outline none
			box-shadow none

			if fill
				padding 6px 12px
				background rgba(#000, 0.035)
				border-radius 6px

			&[type='file']
				display none

		> .prefix
		> .suffix
			display block
			position absolute
			z-index 1
			top 0
			font-size 16px
			line-height fill ? 44px : 32px
			color isDark ? rgba(#fff, 0.7) : rgba(#000, 0.54)
			pointer-events none

			&:empty
				display none

			> *
				display block
				min-width 16px
				max-width 150px
				overflow hidden
				white-space nowrap
				text-overflow ellipsis

		> .prefix
			left 0
			padding-right 4px

			if fill
				padding-left 12px

		> .suffix
			right 0
			padding-left 4px

			if fill
				padding-right 12px

	> .text
		margin 6px 0
		font-size 13px

		*
			margin 0

	&.focused
		> .input
			if fill
				background rgba(#000, 0.05)
			else
				&:after
					opacity 1
					transform scaleX(1)

			> .label
				color $theme-color

	&.focused
	&.filled
		> .input
			> .label
				top fill ? -24px : -17px
				left 0 !important
				transform scale(0.75)

.ui-input[data-darkmode]
	&.fill
		root(true, true)
	&:not(.fill)
		root(true, false)

.ui-input:not([data-darkmode])
	&.fill
		root(false, true)
	&:not(.fill)
		root(false, false)

</style>