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

feat: key value component #651

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

feat: key value component #651

wants to merge 1 commit into from

Conversation

tonai
Copy link
Collaborator

@tonai tonai commented Feb 12, 2025

PR pour l'issue #643

Utilisation:

import KeyValueList, { getKeyValueSchema } from "@/components/Input/KeyValueList";

const schema = yup.object({
  // use the getKeyValueSchema function to get the yup schema for the KeyValueList component 
  keyValue: getKeyValueSchema([
    { name: "isInteger", message: "value is not an integer", test: isInteger }, // you can add validators for the value field
  ]),
});

function Form() {
    const [data, setData] = useData(); // Get data from API (this is just for example, you probable want to use useQuery)
    const { keyValue, transformKeyValue } = useKeyValue(data); // Transform data from API to match keyValue schema
    const methods = useForm({
        mode: "onSubmit",
        resolver: yupResolver(schema),
        values: { keyValue },
    }); // Init form
    const { handleSubmit } = methods;
    const onSubmit = handleSubmit((values) => {
        console.log("onSubmit", values);
        // use the transformKeyValue function to transform from the keyValue schema back to array or object initial form
        setData(transformKeyValue(values.keyValue));
    });

    // You need to set up the FormProvider for the KeyValueList to access the form context
    return (
        <FormProvider {...methods}>
            <form onSubmit={onSubmit}>
                <h2>Form</h2>
                <KeyValueList label="Valeurs de la BDD"  name="keyValue"/>
                <Button type="submit">Enregistrer</Button>
                <pre>{JSON.stringify(data, null, 2)}</pre>
            </form>
        </FormProvider>
    );
}

Features:

  • permet de gérer des listes de valeurs (array)
  • permet de gérer des listes de clé/valeurs (object)
  • bouton switch pour passer du mode array au mode object
  • ajout/suppression de lignes
  • drag and drop pour réordonner la liste
  • fournit un schema yup prêt à l'emploi
  • le schéma yup intègre un validateur pour éviter les doublons (pour les valeurs si array ou pour les clés si object)
  • possible de passer des props au composant input pour les valeurs (par exemple { type: "date" })
  • possible d'utiliser un select pour les valeurs et de fournir les options

@tonai tonai requested a review from pprev94 February 12, 2025 11:23
@tonai tonai marked this pull request as draft February 12, 2025 11:23
@tonai tonai changed the title [WIP] feat: testing page feat: key value component Feb 13, 2025
@tonai tonai marked this pull request as ready for review February 13, 2025 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant