facilmap/frontend/src/lib/components/ui/field-input.vue

81 wiersze
2.0 KiB
Vue
Czysty Zwykły widok Historia

2023-10-23 21:05:19 +00:00
<script setup lang="ts">
2023-11-02 14:14:05 +00:00
import type { Field } from "facilmap-types";
import { computed } from "vue";
import { formatFieldName, normalizeFieldValue } from "facilmap-utils";
2023-10-23 21:05:19 +00:00
const props = withDefaults(defineProps<{
field: Field;
ignoreDefault?: boolean;
2023-11-01 18:45:16 +00:00
modelValue?: string;
2023-10-23 21:05:19 +00:00
id?: string;
showCheckboxLabel?: boolean;
2023-10-23 21:05:19 +00:00
}>(), {
ignoreDefault: false
});
const emit = defineEmits<{
2023-11-01 18:45:16 +00:00
"update:modelValue": [value: string | undefined];
2023-10-23 21:05:19 +00:00
}>();
2023-11-01 18:45:16 +00:00
const value = computed<string | undefined>({
get: () => normalizeFieldValue(props.field, props.modelValue, props.ignoreDefault),
2023-10-23 21:05:19 +00:00
set: (value) => {
2023-11-01 18:45:16 +00:00
emit("update:modelValue", value);
2023-10-23 21:05:19 +00:00
}
});
</script>
<template>
<div class="fm-field-input">
<template v-if="field.type === 'textarea'">
2023-11-01 18:45:16 +00:00
<textarea class="form-control" :id="id" v-model="value"></textarea>
2023-10-23 21:05:19 +00:00
</template>
<template v-else-if="field.type === 'dropdown'">
2023-11-01 18:45:16 +00:00
<select class="form-select" :id="id" v-model="value">
<option v-for="option in props.field.options" :key="option.value">
2023-10-23 21:05:19 +00:00
{{option.value}}
</option>
</select>
</template>
<template v-else-if="field.type === 'checkbox'">
<template v-if="props.showCheckboxLabel">
<div class="form-check">
<input
type="checkbox"
class="form-check-input"
:id="id"
:checked="value === '1'"
@input="value = ($event.target as HTMLInputElement).checked ? '1' : '0'"
/>
<label v-if="props.showCheckboxLabel" :for="id" class="form-check-label">
{{formatFieldName(field.name)}}
</label>
</div>
</template>
<template v-else>
<input
type="checkbox"
class="form-check-input fm-large-checkbox"
:id="id"
:checked="value === '1'"
@input="value = ($event.target as HTMLInputElement).checked ? '1' : '0'"
/>
</template>
2023-10-23 21:05:19 +00:00
</template>
<template v-else>
2023-11-01 18:45:16 +00:00
<input type="text" class="form-control" :id="id" v-model="value"/>
2023-10-23 21:05:19 +00:00
</template>
</div>
</template>
<style lang="scss">
.fm-field-input {
.fm-large-checkbox {
margin-top: 0;
height: 1.5rem;
width: 1.5rem;
2023-10-23 21:05:19 +00:00
}
}
</style>