mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-23 18:35:58 +00:00
form provider
This commit is contained in:
12
packages/web/src/forms/FormProvider.svelte
Normal file
12
packages/web/src/forms/FormProvider.svelte
Normal file
@@ -0,0 +1,12 @@
|
||||
<script lang="ts">
|
||||
import { writable } from 'svelte/store';
|
||||
import FormProviderCore from './FormProviderCore.svelte';
|
||||
|
||||
export let initivalValues = {};
|
||||
|
||||
const values = writable(initivalValues);
|
||||
</script>
|
||||
|
||||
<FormProviderCore {values}>
|
||||
<slot />
|
||||
</FormProviderCore>
|
||||
32
packages/web/src/forms/FormProviderCore.svelte
Normal file
32
packages/web/src/forms/FormProviderCore.svelte
Normal file
@@ -0,0 +1,32 @@
|
||||
<script lang="ts" context="module">
|
||||
import { getContext, setContext } from 'svelte';
|
||||
const contextKey = 'formProviderContextKey';
|
||||
|
||||
export function getFormContext(): any {
|
||||
return getContext(contextKey);
|
||||
}
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import keycodes from '../utility/keycodes';
|
||||
|
||||
export let values;
|
||||
|
||||
const context = {
|
||||
values,
|
||||
submitActionRef: { current: null },
|
||||
};
|
||||
|
||||
setContext(contextKey, context);
|
||||
|
||||
function handleEnter(e) {
|
||||
if (e.keyCode == keycodes.enter && context.submitActionRef.current) {
|
||||
e.preventDefault();
|
||||
context.submitActionRef.current(values);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<slot />
|
||||
|
||||
<svelte:window on:keydown={handleEnter} />
|
||||
@@ -1,5 +1,15 @@
|
||||
<script>
|
||||
<script lang="ts">
|
||||
import FormStyledButton from '../widgets/FormStyledButton.svelte';
|
||||
import { getFormContext } from './FormProviderCore.svelte';
|
||||
import { createEventDispatcher } from 'svelte';
|
||||
|
||||
const dispatch = createEventDispatcher();
|
||||
|
||||
const { submitActionRef } = getFormContext();
|
||||
|
||||
submitActionRef.current = () => {
|
||||
dispatch('click');
|
||||
};
|
||||
</script>
|
||||
|
||||
<FormStyledButton type="submit" on:click {...$$props} />
|
||||
|
||||
@@ -1,21 +1,24 @@
|
||||
<script lang="ts">
|
||||
import registerCommand from '../commands/registerCommand';
|
||||
import FormButton from '../forms/FormButton.svelte';
|
||||
import FormProvider from '../forms/FormProvider.svelte';
|
||||
import FormSubmit from '../forms/FormSubmit.svelte';
|
||||
|
||||
import ModalBase from './ModalBase.svelte';
|
||||
</script>
|
||||
|
||||
<ModalBase {...$$restProps}>
|
||||
<div slot="header">Add connection</div>
|
||||
xxx
|
||||
<div slot="footer" class="flex">
|
||||
<div class="buttons">
|
||||
<FormButton value="Test" />
|
||||
<FormSubmit value="Save" />
|
||||
<FormProvider>
|
||||
<ModalBase {...$$restProps}>
|
||||
<div slot="header">Add connection</div>
|
||||
xxx
|
||||
<div slot="footer" class="flex">
|
||||
<div class="buttons">
|
||||
<FormButton value="Test" />
|
||||
<FormSubmit value="Save" on:click={() => console.log('SAVE')} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</ModalBase>
|
||||
</ModalBase>
|
||||
</FormProvider>
|
||||
|
||||
<style>
|
||||
.buttons {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import FontIcon from '../icons/FontIcon.svelte';
|
||||
import { closeModal } from './modalTools';
|
||||
import clickOutside from '../utility/clickOutside';
|
||||
import keycodes from '../utility/keycodes';
|
||||
|
||||
export let fullScreen;
|
||||
export let noPadding;
|
||||
@@ -10,6 +11,12 @@
|
||||
function handleCloseModal() {
|
||||
closeModal(modalId);
|
||||
}
|
||||
|
||||
function handleEscape(e) {
|
||||
if (e.keyCode == keycodes.escape) {
|
||||
closeModal(modalId);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<!-- The Modal -->
|
||||
@@ -31,6 +38,8 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<svelte:window on:keydown={handleEscape} />
|
||||
|
||||
<style>
|
||||
.bglayer {
|
||||
position: fixed;
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
export let value;
|
||||
|
||||
function handleClick() {
|
||||
if (disabled) dispatch('click');
|
||||
if (!disabled) dispatch('click');
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user