Skip to content

Commit

Permalink
Edit label (#146)
Browse files Browse the repository at this point in the history
* Edit label
  • Loading branch information
MauserBitfly authored Mar 25, 2024
1 parent abcdf60 commit d7038ca
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 1 deletion.
7 changes: 6 additions & 1 deletion frontend/assets/css/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ a {
.link {
color: var(--link-color);
cursor: pointer;
&:hover {
&:hover:not([disabled="true"]) {
opacity: 0.8;
}

&[disabled="true"]{
color: var(--disabled-color);
cursor: unset;
}
}

@mixin container() {
Expand Down
94 changes: 94 additions & 0 deletions frontend/components/bc/input/BcInputLabel.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<script setup lang="ts">
import {
faCheck,
faEdit
} from '@fortawesome/pro-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
interface Props {
value?: string,
label?: string, // used if not in edit mode, defaults to value,
disabled?: boolean,
canBeEmpty?: boolean,
}
const props = defineProps<Props>()
const inputRef = ref<ComponentPublicInstance | null>(null)
const emit = defineEmits<{(e: 'setValue', value: string): void }>()
const isEditing = ref(false)
const editValue = ref<string>(props.value ?? '')
const iconClick = () => {
if (props.disabled) {
return
}
if (!isEditing.value) {
isEditing.value = true
return
}
if (!editValue.value && !props.canBeEmpty) {
return
}
if (editValue.value !== props.value) {
emit('setValue', editValue.value)
}
isEditing.value = false
}
const icon = computed(() => ({
icon: isEditing.value ? faCheck : faEdit,
disabled: props.disabled || (isEditing.value && (!editValue.value && !props.canBeEmpty))
}))
watch(() => props.value, (v) => {
editValue.value = v ?? ''
})
watch([isEditing, inputRef], ([edit, input]) => {
if (edit) {
input?.$el?.focus()
}
})
</script>

<template>
<div class="input-container">
<div v-if="isEditing" class="input-wrapper">
<InputText ref="inputRef" v-model="editValue" @keypress.enter="iconClick" />
</div>
<span v-if="!isEditing" class="label">
{{ label || value }}
</span>
<FontAwesomeIcon v-if="!disabled" class="link" :icon="icon.icon" :disabled="icon.disabled" @click="iconClick" />
</div>
</template>

<style lang="scss" scoped>
@use "~/assets/css/utils.scss";
.input-container {
display: flex;
gap: var(--padding);
align-items: center;
height: 24px;
.input-wrapper {
flex-grow: 1;
input {
width: 100%;
height: 100%;
}
}
.label {
flex-grow: 1;
margin-left: 8px;
@include utils.truncate-text;
}
}
</style>
3 changes: 3 additions & 0 deletions frontend/components/playground/PlaygroundComponents.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<TabPanel header="Icons">
<PlaygroundIcons />
</TabPanel>
<TabPanel header="Input">
<PlaygroundInput />
</TabPanel>
<TabPanel header="Dialog">
<PlaygroundDialog />
</TabPanel>
Expand Down
43 changes: 43 additions & 0 deletions frontend/components/playground/PlaygroundInput.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<script setup lang="ts">
const input1 = ref('My name')
const input2 = ref('Not empty')
const input3 = ref('')
</script>

<template>
<div class="container">
<div>
<BcInputLabel :value="input1" @set-value="(v: string) => input1 = v" />
</div>
<div>
<BcInputLabel :value="input2" @set-value="(v: string) => input2 = v" />
</div>
<div>
<BcInputLabel :value="input3" :label="input3 || 'Default'" :can-be-empty="true" @set-value="(v: string) => input3 = v" />
</div>
<div>
<BcInputLabel :value="input1" :disabled="true" />
</div>
</div>
</template>

<style lang="scss" scoped>
.container {
margin: 10px;
padding: 10px;
gap: 10px;
display: flex;
overflow: auto;
background: var(--container-background);
color: var(--container-color);
border: solid 1px var(--container-border-color);
>div {
width: 200px;
border: solid 1px var(--container-border-color);
padding: var(--padding-small);
}
}
</style>

0 comments on commit d7038ca

Please sign in to comment.