Skip to content

Commit 5553ae8

Browse files
committed
CountryDropdown example
1 parent 3e5de3d commit 5553ae8

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed

adminforth/documentation/docs/tutorial/03-Customization/02-customFieldRendering.md

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Here is how it looks:
6262
In very similar way you can render how cell is rendered in `'edit'` and `'create'` view.
6363
You can use it for creating custom editors for the fields. Check [component specs](/docs/api/types/Common/interfaces/AdminForthFieldComponents#create) to understand which props are passed to the component
6464

65-
## Parametrizing the custom components
65+
## Parametrize the custom components
6666

6767
Sometimes you need to render same component with different parameters.
6868
You can use [full component declaration](/docs/api/types/Common/interfaces/AdminForthComponentDeclarationFull)
@@ -178,6 +178,79 @@ And simply do `npm install` for the package you need:
178178
npm i <some package> -D
179179
```
180180

181+
## Editing values component
182+
183+
In same way as we define `show` and list component, we can create component for edit/create page.
184+
Let's create custom dropdown for `country` field which will show emoji flags of the countries.
185+
186+
```html title='./custom/CountryDropdown.vue'
187+
<template>
188+
<Select
189+
class="w-full"
190+
:options="column.enum"
191+
:model-value="record[column.name]"
192+
@update:model-value="emit('update:value', $event)"
193+
>
194+
<template #item="{option}">
195+
<span class="text-xl inline-flex">{{ getCountryFlag(option.value) }}</span> {{ option.label }}
196+
</template>
197+
198+
<template #selected-item="{option}">
199+
<span class="text-xl inline-flex">{{ getCountryFlag(option.value) }}</span> {{ option.label }}
200+
</template>
201+
</Select>
202+
</template>
203+
204+
<script setup lang="ts">
205+
import Select from "@/afcl/Select.vue";
206+
import type {
207+
AdminForthResourceColumnCommon,
208+
AdminForthResourceCommon,
209+
AdminUser,
210+
} from "@/types/Common";
211+
212+
const props = defineProps<{
213+
column: AdminForthResourceColumnCommon;
214+
record: any;
215+
meta: any;
216+
resource: AdminForthResourceCommon;
217+
adminUser: AdminUser;
218+
}>();
219+
220+
const emit = defineEmits(["update:value"]);
221+
222+
function getCountryFlag(countryCode: string) {
223+
return countryCode?.toUpperCase()
224+
.replace(/./g, (char) => String.fromCodePoint(char.charCodeAt(0) + 127397));
225+
}
226+
227+
</script>
228+
```
229+
230+
Now you can use this component in the configuration of the resource:
231+
232+
```ts title='./resources/apartments.ts'
233+
{
234+
...
235+
resourceId: 'aparts',
236+
columns: [
237+
...
238+
{
239+
name: 'country',
240+
components: {
241+
//diff-add
242+
edit: '@@/CountryDropdown.vue',
243+
//diff-add
244+
create: '@@/CountryDropdown.vue',
245+
},
246+
...
247+
},
248+
...
249+
],
250+
...
251+
}
252+
```
253+
181254
182255
## Pre-made renderers
183256
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<template>
2+
<Select
3+
class="w-full"
4+
:options="column.enum"
5+
:model-value="record[column.name]"
6+
@update:model-value="emit('update:value', $event)"
7+
>
8+
<template #item="{option}">
9+
<span class="text-xl inline-flex">{{ getCountryFlag(option.value) }}</span> {{ option.label }}
10+
</template>
11+
12+
<template #selected-item="{option}">
13+
<span class="text-xl inline-flex">{{ getCountryFlag(option.value) }}</span> {{ option.label }}
14+
</template>
15+
</Select>
16+
</template>
17+
18+
<script setup lang="ts">
19+
import Select from "@/afcl/Select.vue";
20+
import type {
21+
AdminForthResourceColumnCommon,
22+
AdminForthResourceCommon,
23+
AdminUser,
24+
} from "@/types/Common";
25+
26+
const props = defineProps<{
27+
column: AdminForthResourceColumnCommon;
28+
record: any;
29+
meta: any;
30+
resource: AdminForthResourceCommon;
31+
adminUser: AdminUser;
32+
}>();
33+
34+
const emit = defineEmits(["update:value"]);
35+
36+
function getCountryFlag(countryCode: string) {
37+
return countryCode?.toUpperCase()
38+
.replace(/./g, (char) => String.fromCodePoint(char.charCodeAt(0) + 127397));
39+
}
40+
41+
</script>

dev-demo/resources/apartments.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ export default {
173173
showCountryName: false,
174174
},
175175
},
176+
edit: {
177+
file: "@@/CountryDropdown.vue",
178+
},
179+
create: {
180+
file: "@@/CountryDropdown.vue",
181+
},
176182
},
177183
enum: [
178184
{

0 commit comments

Comments
 (0)