2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 19:09:01 +00:00

incr: add RowEditForm

- simplify QuickEditForm markdown
This commit is contained in:
18alantom 2023-04-17 12:19:41 +05:30
parent 653ea9f800
commit db90546282
4 changed files with 168 additions and 59 deletions

View File

@ -1,5 +1,5 @@
<template> <template>
<div class="text-sm border-t"> <div class="text-sm">
<template v-for="df in formFields"> <template v-for="df in formFields">
<!-- Table Field Form (Eg: PaymentFor) --> <!-- Table Field Form (Eg: PaymentFor) -->
<Table <Table

View File

@ -70,7 +70,7 @@
<div v-if="hasDoc" class="overflow-auto custom-scroll"> <div v-if="hasDoc" class="overflow-auto custom-scroll">
<CommonFormSection <CommonFormSection
v-for="([name, fields], idx) in activeGroup.entries()" v-for="([name, fields], idx) in activeGroup.entries()"
@editrow="(doc: Doc) => toggleQuickEditDoc(doc)" @editrow="(doc: Doc) => showRowEditForm(doc)"
:key="name + idx" :key="name + idx"
ref="section" ref="section"
class="p-4" class="p-4"
@ -120,30 +120,44 @@
</div> </div>
</template> </template>
<template #quickedit> <template #quickedit>
<Transition name="quickedit"> </Transition>
<Transition name="quickedit"> <Transition name="quickedit">
<LinkedEntries <LinkedEntries
v-if="showLinks && !hasQeDoc" v-if="showLinks && canShowLinks"
:doc="doc" :doc="doc"
@close="showLinks = false" @close="showLinks = false"
/> />
</Transition> </Transition>
<Transition name="quickedit">
<RowEditForm
v-if="row && !showLinks"
:doc="doc"
:fieldname="row.fieldname"
:index="row.index"
@previous="(i:number) => row!.index = i"
@next="(i:number) => row!.index = i"
@close="() => (row = null)"
/>
</Transition>
</template> </template>
</FormContainer> </FormContainer>
</template> </template>
<script lang="ts"> <script lang="ts">
import { DocValue } from 'fyo/core/types'; import { DocValue } from 'fyo/core/types';
import { Doc } from 'fyo/model/doc'; import { Doc } from 'fyo/model/doc';
import { DEFAULT_CURRENCY } from 'fyo/utils/consts';
import { ValidationError } from 'fyo/utils/errors'; import { ValidationError } from 'fyo/utils/errors';
import { getDocStatus } from 'models/helpers'; import { getDocStatus } from 'models/helpers';
import { ModelNameEnum } from 'models/types'; import { ModelNameEnum } from 'models/types';
import { Field, Schema } from 'schemas/types'; import { Field, Schema } from 'schemas/types';
import Button from 'src/components/Button.vue'; import Button from 'src/components/Button.vue';
import Barcode from 'src/components/Controls/Barcode.vue';
import ExchangeRate from 'src/components/Controls/ExchangeRate.vue';
import DropdownWithActions from 'src/components/DropdownWithActions.vue'; import DropdownWithActions from 'src/components/DropdownWithActions.vue';
import FormContainer from 'src/components/FormContainer.vue'; import FormContainer from 'src/components/FormContainer.vue';
import FormHeader from 'src/components/FormHeader.vue'; import FormHeader from 'src/components/FormHeader.vue';
import StatusBadge from 'src/components/StatusBadge.vue'; import StatusBadge from 'src/components/StatusBadge.vue';
import { getErrorMessage } from 'src/utils'; import { getErrorMessage } from 'src/utils';
import { shortcutsKey } from 'src/utils/injectionKeys';
import { docsPathMap } from 'src/utils/misc'; import { docsPathMap } from 'src/utils/misc';
import { docsPathRef } from 'src/utils/refs'; import { docsPathRef } from 'src/utils/refs';
import { ActionGroup, DocRef, UIGroupedFields } from 'src/utils/types'; import { ActionGroup, DocRef, UIGroupedFields } from 'src/utils/types';
@ -156,17 +170,11 @@ import {
isPrintable, isPrintable,
routeTo, routeTo,
} from 'src/utils/ui'; } from 'src/utils/ui';
import { computed, defineComponent, nextTick } from 'vue';
import QuickEditForm from '../QuickEditForm.vue';
import CommonFormSection from './CommonFormSection.vue';
import { inject } from 'vue';
import { shortcutsKey } from 'src/utils/injectionKeys';
import { ref } from 'vue';
import { useDocShortcuts } from 'src/utils/vueUtils'; import { useDocShortcuts } from 'src/utils/vueUtils';
import Barcode from 'src/components/Controls/Barcode.vue'; import { computed, defineComponent, inject, nextTick, ref } from 'vue';
import { DEFAULT_CURRENCY } from 'fyo/utils/consts'; import CommonFormSection from './CommonFormSection.vue';
import ExchangeRate from 'src/components/Controls/ExchangeRate.vue';
import LinkedEntries from './LinkedEntries.vue'; import LinkedEntries from './LinkedEntries.vue';
import RowEditForm from './RowEditForm.vue';
export default defineComponent({ export default defineComponent({
props: { props: {
@ -198,16 +206,16 @@ export default defineComponent({
errors: {}, errors: {},
activeTab: this.t`Default`, activeTab: this.t`Default`,
groupedFields: null, groupedFields: null,
quickEditDoc: null,
isPrintable: false, isPrintable: false,
showLinks: false, showLinks: false,
row: null,
} as { } as {
errors: Record<string, string>; errors: Record<string, string>;
activeTab: string; activeTab: string;
groupedFields: null | UIGroupedFields; groupedFields: null | UIGroupedFields;
quickEditDoc: null | Doc;
isPrintable: boolean; isPrintable: boolean;
showLinks: boolean; showLinks: boolean;
row: null | { index: number; fieldname: string };
}; };
}, },
async mounted() { async mounted() {
@ -243,6 +251,7 @@ export default defineComponent({
deactivated(): void { deactivated(): void {
docsPathRef.value = ''; docsPathRef.value = '';
this.showLinks = false; this.showLinks = false;
this.row = null;
}, },
computed: { computed: {
canShowBarcode(): boolean { canShowBarcode(): boolean {
@ -292,7 +301,7 @@ export default defineComponent({
return !this.doc.isCancelled && !this.doc.dirty && this.isPrintable; return !this.doc.isCancelled && !this.doc.dirty && this.isPrintable;
}, },
canShowLinks(): boolean { canShowLinks(): boolean {
if (!this.hasDoc || this.hasQeDoc) { if (!this.hasDoc) {
return false; return false;
} }
@ -305,9 +314,6 @@ export default defineComponent({
hasDoc(): boolean { hasDoc(): boolean {
return this.docOrNull instanceof Doc; return this.docOrNull instanceof Doc;
}, },
hasQeDoc(): boolean {
return this.quickEditDoc instanceof Doc;
},
status(): string { status(): string {
if (!this.hasDoc) { if (!this.hasDoc) {
return ''; return '';
@ -324,15 +330,6 @@ export default defineComponent({
} }
return doc; return doc;
}, },
qeDoc(): Doc {
const doc = this.quickEditDoc as Doc | null;
if (!doc) {
throw new ValidationError(
this.t`Doc ${this.schema.label} ${this.name} not set`
);
}
return doc;
},
title(): string { title(): string {
if (this.schema.isSubmittable && this.docOrNull?.notInserted) { if (this.schema.isSubmittable && this.docOrNull?.notInserted) {
return this.t`New Entry`; return this.t`New Entry`;
@ -402,18 +399,18 @@ export default defineComponent({
this.name this.name
); );
}, },
async toggleQuickEditDoc(doc: Doc | null) { async showRowEditForm(doc: Doc) {
if (this.quickEditDoc && doc) { if (this.showLinks) {
this.quickEditDoc = null;
await nextTick();
}
if (doc && this.showLinks) {
this.showLinks = false; this.showLinks = false;
await nextTick(); await nextTick();
} }
this.quickEditDoc = doc; const index = doc.idx;
const fieldname = doc.parentFieldname;
if (typeof index === 'number' && typeof fieldname === 'string') {
this.row = { index, fieldname };
}
}, },
async onValueChange(field: Field, value: DocValue) { async onValueChange(field: Field, value: DocValue) {
const { fieldname } = field; const { fieldname } = field;
@ -442,6 +439,7 @@ export default defineComponent({
Barcode, Barcode,
ExchangeRate, ExchangeRate,
LinkedEntries, LinkedEntries,
RowEditForm,
}, },
}); });
</script> </script>

View File

@ -0,0 +1,113 @@
<template>
<div
class="border-s h-full overflow-auto w-quick-edit bg-white custom-scroll"
>
<!-- Row Edit Tool bar -->
<div class="sticky top-0 border-b bg-white" style="z-index: 1">
<div class="flex items-center justify-between px-4 h-row-largest">
<!-- Close Button -->
<Button :icon="true" @click="$emit('close')">
<feather-icon name="x" class="w-4 h-4" />
</Button>
<!-- Actions, Badge and Status Change Buttons -->
<div class="flex items-stretch gap-2">
<Button
v-if="previous >= 0"
:icon="true"
@click="$emit('previous', previous)"
>
<feather-icon name="chevron-left" class="w-4 h-4" />
</Button>
<Button v-if="next >= 0" :icon="true" @click="$emit('next', next)">
<feather-icon name="chevron-right" class="w-4 h-4" />
</Button>
</div>
</div>
<FormHeader
class="border-t"
:form-title="t`Row ${index + 1}`"
:form-sub-title="fieldlabel"
/>
</div>
<TwoColumnForm
class="w-full"
ref="form"
:doc="row"
:fields="fields"
:column-ratio="[1.1, 2]"
/>
</div>
</template>
<script lang="ts">
import { Doc } from 'fyo/model/doc';
import { ValueError } from 'fyo/utils/errors';
import Button from 'src/components/Button.vue';
import FormHeader from 'src/components/FormHeader.vue';
import TwoColumnForm from 'src/components/TwoColumnForm.vue';
import { shortcutsKey } from 'src/utils/injectionKeys';
import { computed } from 'vue';
import { inject } from 'vue';
import { defineComponent } from 'vue';
const COMPONENT_NAME = 'RowEditForm';
export default defineComponent({
setup() {
return { shortcuts: inject(shortcutsKey) };
},
emits: ['next', 'previous', 'close'],
props: {
doc: { type: Doc, required: true },
index: { type: Number, required: true },
fieldname: { type: String, required: true },
},
provide() {
return {
doc: computed(() => this.row),
};
},
mounted() {
this.shortcuts?.set(COMPONENT_NAME, ['Escape'], () => this.$emit('close'));
},
unmounted() {
this.shortcuts?.delete(COMPONENT_NAME);
},
computed: {
fieldlabel() {
return (
this.fyo.getField(this.doc.schemaName, this.fieldname)?.label ?? ''
);
},
row() {
const rows = this.doc.get(this.fieldname);
if (Array.isArray(rows) && rows[this.index] instanceof Doc) {
return rows[this.index];
}
const label = `${this.doc.name}.${this.fieldname}[${this.index}]`;
throw new ValueError(this.t`Invalid value found for ${label}`);
},
fields() {
const fieldnames = this.row.schema.quickEditFields ?? [];
return fieldnames.map((f) => this.fyo.getField(this.row.schemaName, f));
},
previous(): number {
return this.index - 1;
},
next() {
const rows = this.doc.get(this.fieldname);
if (!Array.isArray(rows)) {
return -1;
}
if (rows.length - 1 === this.index) {
return -1;
}
return this.index + 1;
},
},
components: { Button, TwoColumnForm, FormHeader },
});
</script>

View File

@ -15,37 +15,35 @@
" "
style="z-index: 1" style="z-index: 1"
> >
<!-- Close Button and Status Text --> <!-- Close Button -->
<Button :icon="true" @click="routeToPrevious"> <Button :icon="true" @click="routeToPrevious">
<feather-icon name="x" class="w-4 h-4" /> <feather-icon name="x" class="w-4 h-4" />
</Button> </Button>
<!-- Actions, Badge and Status Change Buttons --> <!-- Save & Submit Buttons -->
<div class="flex items-stretch gap-2"> <Button
<Button :icon="true"
:icon="true" @click="sync"
@click="sync" type="primary"
type="primary" v-if="doc?.canSave"
v-if="doc?.canSave" class="text-white text-xs"
class="text-white text-xs" >
> {{ t`Save` }}
{{ t`Save` }} </Button>
</Button> <Button
<Button :icon="true"
:icon="true" @click="submit"
@click="submit" type="primary"
type="primary" v-else-if="doc?.canSubmit"
v-else-if="doc?.canSubmit" class="text-white text-xs"
class="text-white text-xs" >
> {{ t`Submit` }}
{{ t`Submit` }} </Button>
</Button>
</div>
</div> </div>
<!-- Name and image --> <!-- Name and image -->
<div <div
class="items-center" class="items-center border-b"
:class="imageField ? 'grid' : 'flex justify-center'" :class="imageField ? 'grid' : 'flex justify-center'"
:style="{ :style="{
height: `calc(var(--h-row-mid) * ${!!imageField ? '2 + 1px' : '1'})`, height: `calc(var(--h-row-mid) * ${!!imageField ? '2 + 1px' : '1'})`,