feat: create collection form validation

This commit is contained in:
Anusree Subash 2022-01-19 00:25:08 +05:30
parent 4f7b5187be
commit 7b6efe6044
5 changed files with 46 additions and 13 deletions

View File

@ -27,12 +27,14 @@
"classnames": "^2.3.1", "classnames": "^2.3.1",
"codemirror": "^5.64.0", "codemirror": "^5.64.0",
"escape-html": "^1.0.3", "escape-html": "^1.0.3",
"formik": "^2.2.9",
"markdown-it": "^12.2.0", "markdown-it": "^12.2.0",
"nanoid": "^3.1.30", "nanoid": "^3.1.30",
"react": "^17.0.2", "react": "^17.0.2",
"react-dom": "^17.0.2", "react-dom": "^17.0.2",
"react-tabs": "^3.2.3", "react-tabs": "^3.2.3",
"styled-components": "^5.3.3", "styled-components": "^5.3.3",
"tailwindcss": "^2.2.19" "tailwindcss": "^2.2.19",
"yup": "^0.32.11"
} }
} }

View File

@ -1,21 +1,47 @@
import React from 'react'; import React, { useState } from 'react';
import {useFormik} from 'formik';
import * as Yup from 'yup';
import Modal from '../../Modal'; import Modal from '../../Modal';
const CreateCollection = ({handleConfirm, handleCancel, actions, dispatch}) => { const CreateCollection = ({handleConfirm, handleCancel, actions, dispatch}) => {
const formik = useFormik({
enableReinitialize: true,
initialValues: {
collectionName: ''
},
validationSchema: Yup.object({
collectionName: Yup.string()
.min(3, 'must be atleast 3 characters')
.max(50, 'must be 50 characters or less')
.required('name is required')
}),
onSubmit: (values) => {
handleConfirm(values);
}
});
const onSubmit = () => formik.handleSubmit();
return ( return (
<Modal <Modal
size="sm" size="sm"
title='Create Collection' title='Create Collection'
handleConfirm={handleConfirm} handleConfirm={onSubmit}
handleCancel={handleCancel} handleCancel={handleCancel}
> >
<form className="grafnode-form"> <form className="grafnode-form" onSubmit={formik.handleSubmit}>
<label htmlFor="name" className="block font-semibold">Name</label> <div>
<input <label htmlFor="collectionName" className="block font-semibold">Name</label>
id="collection-name" type="text" name="collection-name" <input
className="block textbox mt-2 w-full" id="collection-name" type="text" name="collectionName"
required className="block textbox mt-2 w-full"
/> onChange={formik.handleChange}
value={formik.values.collectionName || ''}
/>
{formik.touched.collectionName && formik.errors.collectionName ? (
<div className="text-red-500">{formik.errors.collectionName}</div>
) : null}
</div>
</form> </form>
</Modal> </Modal>
); );

View File

@ -23,7 +23,8 @@ const Sidebar = ({collections, actions, dispatch, activeRequestTabId}) => {
setModalOpen(false); setModalOpen(false);
}; };
const handleConfirm = () => { const handleConfirm = (values) => {
console.log(values);
setModalOpen(false); setModalOpen(false);
}; };

View File

@ -47,9 +47,11 @@ module.exports = {
'codemirror': 'codemirror', 'codemirror': 'codemirror',
'graphql': 'graphql', 'graphql': 'graphql',
'escape-html': 'escape-html', 'escape-html': 'escape-html',
'formik': 'formik',
'markdown-it': 'markdown-it', 'markdown-it': 'markdown-it',
'nanoid': 'nanoid', 'nanoid': 'nanoid',
'graphql-request': 'graphql-request' 'graphql-request': 'graphql-request',
'yup': 'yup'
}, },
plugins: [ plugins: [
new MiniCssExtractPlugin() new MiniCssExtractPlugin()

View File

@ -32,6 +32,7 @@
"codemirror": "^5.64.0", "codemirror": "^5.64.0",
"codemirror-graphql": "^1.2.5", "codemirror-graphql": "^1.2.5",
"escape-html": "^1.0.3", "escape-html": "^1.0.3",
"formik": "^2.2.9",
"graphiql": "^1.5.9", "graphiql": "^1.5.9",
"graphql": "^16.2.0", "graphql": "^16.2.0",
"graphql-request": "^3.7.0", "graphql-request": "^3.7.0",
@ -45,7 +46,8 @@
"react-tabs": "^3.2.3", "react-tabs": "^3.2.3",
"sass": "^1.46.0", "sass": "^1.46.0",
"styled-components": "^5.3.3", "styled-components": "^5.3.3",
"tailwindcss": "^2.2.19" "tailwindcss": "^2.2.19",
"yup": "^0.32.11"
}, },
"devDependencies": { "devDependencies": {
"babel-plugin-styled-components": "^2.0.2", "babel-plugin-styled-components": "^2.0.2",