Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor form components #659

Merged
merged 8 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 171 additions & 0 deletions client/components/forms/BarcodeInput.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<template>
<input-wrapper v-bind="inputWrapperProps">
<template #label>
<slot name="label" />
</template>

<div
v-if="isScanning"
class="relative w-full"
>
<CameraUpload
:is-barcode-mode="true"
:decoders="decoders"
@stop-webcam="stopScanning"
@barcode-detected="handleBarcodeDetected"
/>
</div>

<div
v-else-if="scannedValue"
class="flex items-center justify-between border border-gray-300 dark:border-gray-600 w-full bg-white text-gray-700 dark:bg-notion-dark-light dark:text-gray-300 rounded-lg px-4 py-2"
>
<div class="flex-1 break-all">
{{ scannedValue }}
</div>
<button
v-if="!disabled"
type="button"
class="pt-1 text-gray-400 hover:text-gray-600"
@click="clearValue"
>
<Icon
name="i-heroicons-x-mark-20-solid"
class="h-5 w-5"
/>
</button>
</div>

<div
v-else
:style="inputStyle"
class="flex flex-col w-full items-center justify-center transition-colors duration-40"
:class="[
{'!cursor-not-allowed':disabled, 'cursor-pointer':!disabled},
theme.fileInput.input,
theme.fileInput.borderRadius,
theme.fileInput.spacing.horizontal,
theme.fileInput.spacing.vertical,
theme.fileInput.fontSize,
theme.fileInput.minHeight,
{'border-red-500 border-2':hasError},
'focus:outline-none focus:ring-2'
]"
tabindex="0"
role="button"
aria-label="Click to open a camera"
@click="startScanning"
@keydown.enter.prevent="startScanning"
>
<div class="flex w-full items-center justify-center">
<div class="text-center">
<template v-if="!scannedValue && !isScanning">
<div class="text-gray-500 w-full flex justify-center">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-5 h-5"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5m-13.5-9L12 3m0 0l4.5 4.5M12 3v13.5"
/>
</svg>
</div>

<p class="mt-2 text-sm text-gray-500 font-medium select-none">
{{ $t('forms.barcodeInput.clickToOpenCamera') }}
</p>
<div class="w-full items-center justify-center mt-2 hidden sm:flex">
<UButton
icon="i-heroicons-camera"
color="white"
class="px-2"
@click.stop="startScanning"
/>
</div>
</template>
</div>
</div>
</div>

<template #error>
<slot name="error" />
</template>
</input-wrapper>
</template>

<script>
import { inputProps, useFormInput } from './useFormInput.js'
import InputWrapper from './components/InputWrapper.vue'
import CameraUpload from './components/CameraUpload.vue'

export default {
name: 'BarcodeInput',
components: { InputWrapper, CameraUpload },

props: {
...inputProps,
decoders: {
type: Array,
default: () => []
}
},

setup(props, context) {
return {
...useFormInput(props, context),
}
},

data: () => ({
isScanning: false,
scannedValue: null
}),

watch: {
scannedValue: {
handler(value) {
this.compVal = value
},
immediate: true
}
},

beforeUnmount() {
this.stopScanning()
},

methods: {
startScanning() {
if (this.disabled) return
this.isScanning = true
},

stopScanning() {
this.isScanning = false
},

handleBarcodeDetected(code) {
this.scannedValue = code
this.stopScanning()
},

clearValue() {
this.scannedValue = null
}
}
}
</script>

<style scoped>
video {
/* Ensure the video displays properly */
max-width: 100%;
height: auto;
}
</style>
6 changes: 4 additions & 2 deletions client/components/forms/DateInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
</div>
</button>

<template #panel="{ close }">
<template #panel>
<DatePicker
v-if="props.dateRange"
v-model.range="modeledValue"
Expand Down Expand Up @@ -157,9 +157,11 @@ const inputClasses = computed(() => {
classes.push('!cursor-not-allowed !bg-gray-200 dark:!bg-gray-800')
}
if (input.hasError.value) {

classes.push('!ring-red-500 !ring-2 !border-transparent')
}
if (!props.disabled && !input.hasError.value && pickerOpen.value) {
classes.push('ring-2 ring-opacity-100 border-transparent')
}
return classes.join(' ')
})

Expand Down
1 change: 1 addition & 0 deletions client/components/forms/FileInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
class="flex flex-col w-full items-center justify-center transition-colors duration-40"
:class="[
{'!cursor-not-allowed':disabled, 'cursor-pointer':!disabled,
'!bg-gray-200 dark:!bg-gray-800': disabled,
[theme.fileInput.inputHover.light + ' dark:'+theme.fileInput.inputHover.dark]: uploadDragoverEvent,
['hover:'+theme.fileInput.inputHover.light +' dark:hover:'+theme.fileInput.inputHover.dark]: !loading},
theme.fileInput.input,
Expand Down
19 changes: 13 additions & 6 deletions client/components/forms/MatrixInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
<slot name="label" />
</template>
<div
class="border border-gray-300 overflow-x-auto"
class="border overflow-x-auto"
:class="[
theme.default.borderRadius,
theme.MatrixInput.cell,
theme.MatrixInput.table,
{
'!ring-red-500 !ring-2 !border-transparent': hasError,
},
Expand All @@ -19,7 +21,8 @@
<td
v-for="column in columns"
:key="column"
class="ltr:border-l rtl:border-r rtl:!border-l-0 border-gray-300 max-w-24 overflow-hidden"
class="ltr:border-l rtl:border-r rtl:!border-l-0 max-w-24 overflow-hidden"
:class="theme.MatrixInput.cell"
>
<div class="p-2 w-full flex items-center justify-center text-sm">
{{ column }}
Expand All @@ -41,10 +44,14 @@
<td
v-for="column in columns"
:key="row + column"
class="ltr:border-l rtl:border-r rtl:!border-l-0 border-gray-300 hover:!bg-gray-100 dark:hover:!bg-gray-800"
:class="{
'!cursor-not-allowed !bg-gray-200 dark:!bg-gray-800 hover:!bg-gray-200 dark:hover:!bg-gray-800': disabled,
}"
class="ltr:border-l rtl:border-r rtl:!border-l-0"
:class="[
theme.MatrixInput.cell,
theme.MatrixInput.cellHover,
{
'!cursor-not-allowed !bg-gray-200 dark:!bg-gray-800 hover:!bg-gray-200 dark:hover:!bg-gray-800': disabled,
},
]"
>
<div
v-if="compVal"
Expand Down
24 changes: 19 additions & 5 deletions client/components/forms/RichTextAreaInput.client.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<template>
<InputWrapper v-bind="inputWrapperProps">
<InputWrapper
v-bind="inputWrapperProps"
wrapper-class="not-draggable"
>
<template #label>
<slot name="label" />
</template>
Expand All @@ -10,19 +13,20 @@
{
'!ring-red-500 !ring-2 !border-transparent': hasError,
'!cursor-not-allowed !bg-gray-200 dark:!bg-gray-800': disabled,
'focus-within:ring-2 focus-within:ring-opacity-100 focus-within:border-transparent': !hasError && !disabled
},
theme.RichTextAreaInput.input,
theme.RichTextAreaInput.borderRadius,
theme.default.fontSize,
]"
:style="{
'--font-size': theme.default.fontSize
'--font-size': theme.default.fontSize,
...inputStyle
}"
>
<QuillyEditor
:id="id ? id : name"
ref="editor"
:key="id+placeholder"
v-model="compVal"
:options="quillOptions"
:disabled="disabled"
Expand All @@ -37,7 +41,6 @@
<template #error>
<slot name="error" />
</template>

<MentionDropdown
v-if="enableMentions && mentionState"
:state="mentionState"
Expand All @@ -53,6 +56,7 @@ import InputWrapper from './components/InputWrapper.vue'
import QuillyEditor from './components/QuillyEditor.vue'
import MentionDropdown from './components/MentionDropdown.vue'
import registerMentionExtension from '~/lib/quill/quillMentionExtension.js'

const props = defineProps({
...inputProps,
editorOptions: {
Expand All @@ -68,7 +72,9 @@ const props = defineProps({
default: () => []
}
})

const emit = defineEmits(['update:modelValue'])

const { compVal, inputStyle, hasError, inputWrapperProps } = useFormInput(props, { emit })
const editor = ref(null)
const mentionState = ref(null)
Expand All @@ -91,6 +97,7 @@ const quillOptions = computed(() => {
]
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Deeply merge Quill modules for reliability
Merging only the first level of modules objects can lead to unexpected overwrites. If props.editorOptions.modules contains multiple nested objects (e.g., syntax, clipboard), consider a deep merge to avoid losing default sub-options.

const mergedOptions = { ...defaultOptions, ...props.editorOptions, modules: { ...defaultOptions.modules, ...props.editorOptions.modules } }

if (props.enableMentions) {
Expand All @@ -112,21 +119,26 @@ const quillOptions = computed(() => {
border-right: 0px !important;
border-left: 0px !important;
font-size: var(--font-size);

.ql-editor {
min-height: 100px !important;
}
}

.ql-toolbar {
border-top: 0px !important;
border-right: 0px !important;
border-left: 0px !important;
}

.ql-header {
@apply rounded-md;
}

.ql-editor.ql-blank:before {
@apply text-gray-400 dark:text-gray-500 not-italic;
}

.ql-snow .ql-toolbar .ql-picker-item.ql-selected,
.ql-snow .ql-toolbar .ql-picker-item:hover,
.ql-snow .ql-toolbar .ql-picker-label.ql-active,
Expand All @@ -144,14 +156,16 @@ const quillOptions = computed(() => {
@apply text-nt-blue;
}
}

.ql-mention {
padding-top: 0px !important;
margin-top: -5px !important;
}
.ql-mention::after {
content: '@';
font-size: 16px;
}


.rich-editor, .mention-input {
span[mention] {
@apply inline-flex items-center align-baseline leading-tight text-sm relative bg-blue-100 text-blue-800 border border-blue-200 rounded-md px-1 py-0.5 mx-0.5;
Expand Down
Loading
Loading