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

highlight drag container #40

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 6 additions & 6 deletions packages/former/src/Playground.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ import Button from './sample/Button.vue';
import Checkbox from './sample/Checkbox.vue';
import Select from './sample/Select.vue';

const mode = useStorage<Mode>('former:mode', 'edit');
const mode = useStorage<Mode>('former:mode', 'build');

const options = [
{ label: 'build', value: 'build' },
{ label: 'edit', value: 'edit' },
{ label: 'read', value: 'read' },
{ label: 'build', value: 'build' },
];

const isValid = ref(true);
Expand Down Expand Up @@ -263,7 +263,6 @@ const components: { [k: string]: FormFieldType } = {
required: true,
},
},
showIfProp,
{
type: 'text',
name: 'label',
Expand All @@ -280,6 +279,7 @@ const components: { [k: string]: FormFieldType } = {
placeholder: 'Enter a placeholder',
},
},
showIfProp,
{
type: 'checkbox',
name: 'required',
Expand Down Expand Up @@ -323,7 +323,6 @@ const components: { [k: string]: FormFieldType } = {
placeholder: 'Enter the name of the data field',
},
},
showIfProp,
{
type: 'text',
name: 'label',
Expand All @@ -332,6 +331,7 @@ const components: { [k: string]: FormFieldType } = {
placeholder: 'Enter a label',
},
},
showIfProp,
],
},
select: {
Expand All @@ -346,7 +346,6 @@ const components: { [k: string]: FormFieldType } = {
placeholder: 'Enter the name of the data field',
},
},
showIfProp,
{
type: 'text',
name: 'label',
Expand All @@ -355,6 +354,7 @@ const components: { [k: string]: FormFieldType } = {
placeholder: 'Enter a label',
},
},
showIfProp,
{
type: 'repeater',
name: 'options',
Expand Down Expand Up @@ -396,7 +396,6 @@ const components: { [k: string]: FormFieldType } = {
placeholder: 'Enter the name of the data field',
},
},
showIfProp,
{
type: 'text',
name: 'label',
Expand All @@ -405,6 +404,7 @@ const components: { [k: string]: FormFieldType } = {
placeholder: 'Enter a label',
},
},
showIfProp,
],
},
};
Expand Down
11 changes: 9 additions & 2 deletions packages/former/src/components/FormDragContainer.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
<template>
<div class="relative flex flex-col gap-3" :class="{ 'former-drag-container min-h-24 w-full': mode === 'build' }" :data-parent-node="node?._id" :data-category="category">
<div
class="relative flex flex-col gap-3 before:content-[attr(data-drag-hint)] before:hidden before:m-auto before:text-center"
:class="{ 'former-drag-container min-h-24 w-full border-2 border-dashed rounded-md border-zinc-200 empty:before:block empty:bg-zinc-100': mode === 'build' }"
:data-parent-node="node?._id"
:data-category="category"
:data-drag-hint="texts.dragHint"
>
<slot />
</div>
</template>

<script setup lang="ts">
import { inject } from 'vue';
import { inject } from '~/compositions/injectProvide';
import type { InternalSchemaNode } from '~/types';

defineProps<{ node?: InternalSchemaNode; category?: string }>();

const mode = inject('mode');
const texts = inject('texts');
ShahwarZia marked this conversation as resolved.
Show resolved Hide resolved
</script>
8 changes: 6 additions & 2 deletions packages/former/src/components/Former.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { isEqual } from 'lodash';
import { computed, ref, toRef, watch } from 'vue';
import { provide } from '~/compositions/injectProvide';
import type { FormData, FormFieldType, InternalSchemaNode, Mode, SchemaNode, ShowIfPredicate, Validator } from '~/types';
import type { FormData, FormFieldType, InternalSchemaNode, Mode, SchemaNode, ShowIfPredicate, Texts, Validator } from '~/types';
import { generateFormId, toInternalSchema, toSchema } from '~/utils';
import FormContent from './FormContent.vue';

Expand All @@ -17,7 +17,8 @@ const props = withDefaults(defineProps<{
showIf?: ShowIfPredicate;
validator?: Validator;
mode?: Mode;
}>(), { mode: 'edit' });
texts?: Partial<Texts>;
}>(), { mode: 'edit', texts: () => ({}) });

const emit = defineEmits<{
(e: 'valid', valid: boolean): void;
Expand Down Expand Up @@ -77,4 +78,7 @@ watch(

const selectedNode = ref<InternalSchemaNode | undefined>(undefined);
provide('selectedNode', selectedNode);

const texts = toRef(props, 'texts');
provide('texts', computed<Texts>(() => ({ dragHint: 'Here you can drag elements', ...texts.value })));
</script>
3 changes: 2 additions & 1 deletion packages/former/src/compositions/injectProvide.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getCurrentInstance, type InjectionKey, type Ref, inject as vueInject, provide as vueProvide } from 'vue';
import type { FormData, FormFieldType, InternalSchemaNode, Mode, ShowIfPredicate, Validator } from '~/types';
import type { FormData, FormFieldType, InternalSchemaNode, Mode, ShowIfPredicate, Texts, Validator } from '~/types';

export type InjectKeys = {
mode: Ref<Mode>;
Expand All @@ -11,6 +11,7 @@ export type InjectKeys = {
validityMap: Ref<Record<string, boolean | undefined>>;
showIf?: ShowIfPredicate;
validator: Validator;
texts: Ref<Texts>;
};

export function inject<T extends keyof InjectKeys>(key: T): InjectKeys[T];
Expand Down
4 changes: 4 additions & 0 deletions packages/former/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ export type FormFieldType = {
propsSchema: SchemaNode[];
component: FormFieldComponent | Raw<FormFieldComponent>;
};

export type Texts = {
dragHint: string;
};