Skip to content

Démarrage rapide

WARNING

Ayez déjà un projet Vue initialisé, puis commencez le démarrage rapide.

Installation des dépendances

bash
npm install @duplojs/form@0 @duplojs/utils@1
bash
yarn add @duplojs/form@0 @duplojs/utils@1
bash
pnpm add @duplojs/form@0 @duplojs/utils@1

Initialisation avec templates Grid par défaut

ts
import "@duplojs/form/vueGrid.css";
import "@duplojs/form/vueDesignSystem.css";

import { 
createForm
} from "@duplojs/form/vue";
import {
templateFormAddButton
,
templateFormRemoveButton
,
templateFormResetButton
,
templateFormNextButton
,
templateFormPreviousButton
,
templateFormSelect
,
} from "@duplojs/form/vueDesignSystem"; import {
createGridTemplates
} from "@duplojs/form/vueGrid";
export const
templatesGrid
=
createGridTemplates
({
repeat
: {
addLabel
: "Add another item",
removeLabel
: "Remove this item",
addButton
:
templateFormAddButton
,
removeButton
:
templateFormRemoveButton
,
resetButton
:
templateFormResetButton
,
},
step
: {
nextLabel
: "Continue",
previousLabel
: "Back",
resetButton
:
templateFormResetButton
,
nextButton
:
templateFormNextButton
,
previousButton
:
templateFormPreviousButton
,
},
union
: {
selectInputKind
:
templateFormSelect
},
}); export const
useForm
=
createForm
(
templatesGrid
.
useTemplates
());

INFO

Tout ce qui provient de @duplojs/form/vueGrid et de @duplojs/form/vueDesignSystem n'est fourni que par défaut. Tout est interchangeable et peut être surchargé. Cela permet de configurer rapidement un environnement dans lequel les formulaires sont opérationnels.

N'oubliez pas d'importer @duplojs/form/vueGrid.css et @duplojs/form/vueDesignSystem.css si vous les utilisez.

Créer un input

ts
import { createInput } from "@duplojs/form/vue";
import FirstInput from "./FirstInput.vue";

export const useMyTextInput = createInput(
	FirstInput,
	{ defaultValue: "" },
);
vue
<script setup lang="ts">
const model = defineModel({ required: true });
</script>

<template>
	<input
		type="text"
		v-model="model"
	/>
</template>

Déclarer un formulaire

ts
ts
import * as 
DP
from "@duplojs/utils/dataParser";
import {
useMultiLayout
} from "@duplojs/form/vue";
import {
useForm
} from "./init";
import {
useMyTextInput
} from "./firstInput";
import {
useNumberInput
} from "@duplojs/form/vueDesignSystem";
export function
useFirstForm
() {
const {
component
,
check
,
reset
,
currentValue
} =
useForm
(
useMultiLayout
({
name
:
useMyTextInput
({
label
: "superLabel",
}),
age
:
useNumberInput
({
label
: "Age",
dataParser
:
DP
.
number
()
.
addChecker
(
DP
.
checkerNumberMin
(
18, {
errorMessage
: "Too young !" },
), ), }), }), ); return {
TheForm
:
component
,
checkForm
:
check
,
currentFormValue
:
currentValue
,
resetForm
:
reset
,
}; }

Implémenter le formulaire

vue
vue
<script setup lang="ts">
import * as EE from "@duplojs/utils/either";
import { PrimaryButton } from "@duplojs/form/vueDesignSystem";
import { useFirstForm } from "./firstForm";

const {
	TheForm,
	checkForm,
} = useFirstForm();

function onSubmit() {
	void EE.whenIsRight(
		checkForm(),
		(result) => {
			alert(JSON.stringify(result));
		},
	);
}
</script>

<template>
	<TheForm @submit="onSubmit">
		<PrimaryButton
			type="submit"
			label="submit"
		/>
	</TheForm>
</template>

Résultat

Released under the MIT License.