Skip to content

Commit e526057

Browse files
committed
Select country
1 parent 8cc448f commit e526057

File tree

3 files changed

+147
-11
lines changed

3 files changed

+147
-11
lines changed

input/SelectCountry.vue

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<template>
2+
<div class="input-group">
3+
<label v-if="label" :for="name">{{ label }} <slot></slot></label>
4+
<div :class="{ 'custom-select': true, 'custom-select-open': open }" :data-uid="uid" @blur="open = false" @click.stop="openSelect">
5+
<div class="selected" :class="{ open: open, inactive: inactive }">{{ selected }} <IconChevronDown class="selected-icon" /></div>
6+
<div class="rounded">
7+
<input type="text" placeholder="Search" @click.stop="" @keyup="filterOptions" />
8+
<div ref="items" class="items" :tabindex="tabindex">
9+
<div v-if="search_check" class="select-search-error">{{ searchError }}</div>
10+
<div v-for="(o, index) in filtered_options" :key="index" :value="o.name" @click.stop="updateClick(o)" :class="{ highlight: o.name == modelValue }">{{ o.emoji }} {{ o.name }} <IconCheckmark v-if="o.name == modelValue" class="checkmark" /></div>
11+
</div>
12+
</div>
13+
<input ref="input" type="hidden" v-model="modelValue" :name="name" />
14+
</div>
15+
</div>
16+
</template>
17+
18+
<script setup lang="js">
19+
import { ref, onMounted, toRefs } from 'vue'
20+
import options from './json/country.json'
21+
import IconChevronDown from './icons/IconChevronDown.vue'
22+
import IconCheckmark from './icons/IconCheckmark.vue'
23+
24+
const emit = defineEmits(['update:modelValue', 'change', 'click', 'blur'])
25+
const props = defineProps({
26+
label: { type: String },
27+
name: { type: String },
28+
modelValue: { type: String, default: 'Poland' },
29+
tabindex: { type: Number, default: 0 },
30+
searchError: { type: String, default: 'Option does not exists.' },
31+
})
32+
let { label, name, modelValue, tabindex, searchError } = toRefs(props)
33+
const input = ref(null)
34+
const selected = ref(null)
35+
const inactive = ref(false)
36+
const open = ref(false)
37+
const search_check = ref(false)
38+
const filtered_options = ref(options)
39+
const uid = ref('custom-select-' + Date.now())
40+
41+
function openSelect() {
42+
let all = document.querySelectorAll('.custom-select-open')
43+
all.forEach((el) => {
44+
let data = el.dataset.uid ?? null
45+
if (data != uid.value) {
46+
el.click()
47+
}
48+
})
49+
50+
open.value = !open.value
51+
}
52+
53+
onMounted(() => {
54+
if (modelValue.value !== null) {
55+
let option = options?.find((o) => o.name === modelValue.value)
56+
if (option?.name) {
57+
selected.value = `${option.emoji} ${option.name}`
58+
} else {
59+
clear()
60+
}
61+
} else {
62+
clear()
63+
}
64+
65+
document.addEventListener('click', (e) => {
66+
let a = document.querySelectorAll('.custom-select-open')
67+
a.forEach((el) => el.click())
68+
})
69+
})
70+
71+
function clear() {
72+
selected.value = `${options[0].emoji} ${options[0].name}`
73+
modelValue.value = options[0].name
74+
inactive.value = false
75+
}
76+
77+
function updateClick(option = null) {
78+
if (option == null) {
79+
clear()
80+
} else {
81+
modelValue.value = option.name ?? null
82+
selected.value = `${option.emoji} ${option.name}`
83+
inactive.value = false
84+
}
85+
open.value = false
86+
emit('update:modelValue', modelValue.value)
87+
}
88+
89+
function filterOptions(e) {
90+
filtered_options.value = options.filter((o) => o.name.toLowerCase().startsWith(e.target.value.toLowerCase()))
91+
search_check.value = filtered_options.value.length == 0 ? true : false
92+
}
93+
</script>
94+
95+
<style>
96+
@import './css/input-root.css';
97+
</style>
98+
99+
<style scoped>
100+
@import './css/input.css';
101+
</style>
102+
103+
<!--
104+
<script setup>
105+
import SelectPrefix from '@/components/form/prefix/SelectPrefix.vue'
106+
107+
const prefix_selected = ref(48)
108+
109+
function onSubmit(e) {
110+
let data = new FormData(e.target);
111+
for (var pair of data.entries()) {
112+
console.log("Key:", pair[0], "Value:", pair[1]);
113+
}
114+
// axios request here send to server
115+
}
116+
</sctipt>
117+
<template>
118+
<form @submit.prevent="onSubmit">
119+
<SelectPrefix label="Prefix" v-model="prefix_selected" name="prefix" />
120+
121+
<button> Send </button>
122+
</form>
123+
</template>
124+
-->

input/css/input.css

+13-7
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ textarea {
4141
input:hover,
4242
select:hover,
4343
textarea:hover {
44-
border-color: var(--wow-border-hover);
45-
border: 1px solid var(--wow-border-hover);
44+
border-color: var(--wow-border-hover) !important;
45+
border: 1px solid var(--wow-border-hover) !important;
4646
}
4747

4848
input:focus,
@@ -196,6 +196,11 @@ option {
196196
cursor: pointer;
197197
}
198198

199+
.custom-select .selected:hover {
200+
border-color: var(--wow-border-hover) !important;
201+
border: 1px solid var(--wow-border-hover) !important;
202+
}
203+
199204
.custom-select .selected.open {
200205
border: 1px solid var(--wow-accent);
201206
box-shadow: 0px 0px 0px 4px #1b5bff11;
@@ -317,7 +322,7 @@ option {
317322
margin-bottom: 0px;
318323
}
319324
.checkbox-line .checkbox {
320-
position: absolute;
325+
position: absolute;
321326
height: 30px;
322327
width: 30px;
323328
opacity: 0;
@@ -361,19 +366,20 @@ option {
361366

362367
/* Checkbox onoff */
363368

364-
.checkbox-checkmark-onoff {
369+
.checkbox-checkmark-onoff {
365370
min-height: 40px;
366-
min-width: 70px;
371+
min-width: 65px;
367372
right: 5px;
373+
padding: 0;
368374
}
369375

370-
.checkmark-onoff {
376+
.checkmark-onoff {
371377
float: left;
372378
box-sizing: border-box;
373379
height: 35px;
374380
width: 70px;
375381
padding: 4px;
376-
margin-left: 15px;
382+
margin-left: 0;
377383
background: var(--wow-input-bg);
378384
border: 1px solid var(--wow-border);
379385
border-radius: var(--wow-border-radius);

input/example/DemoPageView.vue

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Select from '@/components/input/Select.vue'
77
import Password from '@/components/input/Password.vue'
88
import Textarea from '@/components/input/Textarea.vue'
99
import SelectPrefix from '@/components/input/SelectPrefix.vue'
10+
import SelectCountry from '@/components/input/SelectCountry.vue'
1011
import Checkbox from '@/components/input/Checkbox.vue'
1112
import Radiobox from '@/components/input/Radiobox.vue'
1213
import CheckboxOnOff from '@/components/input/CheckboxOnOff.vue'
@@ -28,6 +29,7 @@ let select_country = ref(48)
2829
let select_code = ref(1)
2930
let select_name = ref('Ala')
3031
let lights = ref(false)
32+
let country_name = ref('Poland')
3133
3234
onMounted(() => {
3335
// Route
@@ -66,8 +68,12 @@ function onSubmit(e) {
6668
<Input :focus="'true'" type="text" name="name" v-model="input" placeholder="Name" label="Name" @keydown="validate" />
6769

6870
<Password type="password" name="password" v-model="password" placeholder="Password" label="Password" @valid="validPass" @invalid="invalidPass" />
71+
72+
<SelectPrefix name="prefix" v-model="select_country" :label="$t('Prefix')" />
6973

70-
<SelectPrefix name="prefix" v-model="select_country" :label="$t('register.Prefix')" />
74+
<SelectCountry name="country" v-model="country_name" :label="$t('Country')" />
75+
76+
<!-- <Select name="country" v-model="select_country_name" :label="$t('Country')" :options="['Poland', 'England', 'Usa']" /> -->
7177

7278
<Select
7379
name="code"
@@ -78,9 +84,9 @@ function onSubmit(e) {
7884
{ key: 2, value: 'Css' },
7985
{ key: 3, value: 'Html' },
8086
{ key: 4, value: 'JavaScript' },
81-
]" />
87+
]" />
8288

83-
<Select name="code" v-model="select_name" :label="$t('Language')" :options="['Ala', 'Ma', 'Kota']" />
89+
<Select name="name" v-model="select_name" :label="$t('Name')" :options="['Ala', 'Ma', 'Kota']" />
8490

8591
<Textarea name="desc" v-model="textarea" placeholder="Some text" label="Description" />
8692

@@ -97,7 +103,7 @@ function onSubmit(e) {
97103

98104
<button class="button">Update</button>
99105

100-
<h4>{{ lights }} | {{ select_code }} | {{ select_country }} | {{ select_name }} | {{ input }} | {{ password }} | {{ payment }} | {{ remember_me }} | {{ textarea }} | {{ radio }}</h4>
106+
<h4> {{ country_name }} {{ lights }} | {{ select_code }} | {{ select_country }} | {{ select_name }} | {{ input }} | {{ password }} | {{ payment }} | {{ remember_me }} | {{ textarea }} | {{ radio }}</h4>
101107
</form>
102108
</template>
103109

0 commit comments

Comments
 (0)