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(form): actions (submit & base button) in form (#5) #6

Merged
merged 1 commit into from
Mar 11, 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
108 changes: 52 additions & 56 deletions apps/docs/src/app/app.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,58 @@
import { Route, Routes, Link } from 'react-router-dom';

// import { Form } from '@mui-builder/form';
import { Builder, GROUP_TYPE } from '@mui-builder/core';
import { FIELD_TYPE } from 'packages/components/form/src/types/public.types';
import { Link, Route, Routes } from 'react-router-dom';
import { Builder, IFormTextBuilderProps } from '@mui-builder/core';

export function App() {
const groupList: IFormTextBuilderProps[] = [
{
id: '1',
groupType: 'form',
type: 'field-text',
props: {
id: 'type-1',
formId: '20',
},
},
{
id: '2',
groupType: 'form',
type: 'field-text',
props: {
id: 'type-2',
formId: '20',
helperText: 'mmd',
},
},
{
id: '3',
groupType: 'form',
type: 'field-text',
props: {
id: 'type-2',
formId: '21',
helperText: 'mmd',
},
},
{
id: '4',
groupType: 'form',
type: 'action-submit',
props: {
formId: '21',
children: 'submit 21',
},
},
{
id: '4',
groupType: 'form',
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate id value for the entry with id: '4'. Each entry in the groupList should have a unique id to avoid potential rendering issues or unintended behavior.

type: 'action-submit',
props: {
formId: '20',
children: 'submit 20',
},
},
];
return (
<div>
<br />
<hr />
<br />
<div role="navigation">
<ul>
<li>
Expand All @@ -33,55 +76,8 @@ export function App() {
</div>
}
/>
{/* <Route path="/form" element={<Form />} /> */}
<Route
path="/"
element={
<Builder
groupList={[
{
id: '1',
groupType: GROUP_TYPE.FORM,
type: FIELD_TYPE.TEXT,
props: {
id: 'type-1',
formId: '20',
},
},
{
id: '2',
groupType: GROUP_TYPE.FORM,
type: FIELD_TYPE.TEXT,
props: {
id: 'type-2',
formId: '20',
helperText: 'mmd',
},
},
{
id: '3',
groupType: GROUP_TYPE.FORM,
type: FIELD_TYPE.TEXT,
props: {
id: 'type-2',
formId: '21',
helperText: 'mmd',
},
},
]}
/>
}
/>
<Route
path="/page-2"
element={
<div>
<Link to="/">Click here to go back to root page.</Link>
</div>
}
/>
<Route path="/" element={<Builder groupList={groupList} />} />
</Routes>
{/* END: routes */}
</div>
);
}
Expand Down
96 changes: 84 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@changesets/cli": "^2.27.1",
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.0",
"@mui/lab": "^5.0.0-alpha.167",
"@mui/material": "^5.15.11",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { LoadingButton } from '@mui/lab';
import { FC } from 'react';
import { ActionBaseProps } from './actionBase.types';

const ActionBase: FC<ActionBaseProps> = ({ children, ...actionBaseProps }) => {
return <LoadingButton {...actionBaseProps}>{children}</LoadingButton>;
};

export default ActionBase;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { LoadingButtonProps } from '@mui/lab';
import { ReactNode } from 'react';

export type ActionBaseProps = LoadingButtonProps & {
children: ReactNode;
};
25 changes: 25 additions & 0 deletions packages/components/form/src/components/actions/submit/submit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Button } from '@mui/material';
import { FC } from 'react';
import { ActionSubmitFieldProps } from './submit.types';
import useForms from '../../../hooks/useForms/useForms';
import { FieldValues } from 'react-hook-form';

const ActionSubmit: FC<ActionSubmitFieldProps> = ({
formId,
children,
...submitFieldProps
}) => {
const forms = useForms((state) => state.forms);
const { handleSubmit } = forms[formId];
const onSubmit = (values: FieldValues) => {
console.log(values);
};

return (
<Button {...submitFieldProps} onClick={handleSubmit(onSubmit)}>
{children}
</Button>
);
Comment on lines +7 to +22
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ActionSubmit component is well-implemented, utilizing the Button component from Material-UI and the useForms hook for form management. The use of handleSubmit from the form's state to wrap the onSubmit function ensures that form submission is handled correctly. However, consider handling the submission result or errors, as logging values to the console might not be sufficient for production use.

Consider enhancing the onSubmit function to handle submission results or errors, providing feedback to the user or further processing the form data.

};

export default ActionSubmit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { FormId } from '../../../types/public.types';
import { ActionBaseProps } from '../base/actionBase.types';

export type ActionSubmitFieldProps = ActionBaseProps & FormId;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FIELD_TYPE, FieldProps } from '../../types/public.types';
import { FormTypes, FieldProps } from '../../types/public.types';

export interface BuilderProps {
fieldType: FIELD_TYPE;
fieldType: FormTypes;
fieldProps: FieldProps;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TextField } from '@mui/material';
import { FC } from 'react';
import { useController } from 'react-hook-form';
import { TextProps } from './text.types';
import useForms from '../../hooks/useForms/useForms';
import useForms from '../../../hooks/useForms/useForms';

const Text: FC<TextProps> = ({ formId, ...textFieldProps }) => {
const { forms } = useForms();
Expand All @@ -12,7 +12,7 @@ const Text: FC<TextProps> = ({ formId, ...textFieldProps }) => {
control: formMethod.control,
rules: { required: true },
});

return <TextField {...field} {...textFieldProps} value={field.value ?? ''} />;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TextFieldProps } from "@mui/material";
import { FormId, Id } from "../../types/public.types";
import { FormId, Id } from "../../../types/public.types";

export type TextProps = TextFieldProps & Id & FormId;
9 changes: 4 additions & 5 deletions packages/components/form/src/types/public.types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { TextProps } from "../components/text/text.types";
import { ActionSubmitFieldProps } from "../components/actions/submit/submit.types";
import { TextProps } from "../components/fields/text/text.types";

export type FormId = { formId: string };

export type Id = {
id: string;
};

export enum FIELD_TYPE {
TEXT = 'text',
}
export type FormTypes = 'field-text' | "action-submit";

export type FieldProps = TextProps;
export type FieldProps = TextProps | ActionSubmitFieldProps;
Loading
Loading