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): radio input with radio group component #45

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
41 changes: 16 additions & 25 deletions apps/docs/src/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,21 @@ export function App() {
},
},
},
{
id: 'form-radio-1',
groupType: 'form',
type: 'radio',
props: {
formId: '20',
id: 'radio-1',
name: 'fol',
defaultValue: '2',
options: [
{ name: 'radio-num-1', value: 'value 1', id: '1' },
{ name: 'radio-num-2', value: 'value 2', id: '2' },
],
},
},
{
id: 'form-checkbox-1',
groupType: 'form',
Expand All @@ -193,30 +208,6 @@ export function App() {
formId: '21',
label: 'label 1',
checkboxProps: {},
children: [
{
id: 'form-checkbox-1-nested-1',
groupType: 'form',
type: 'checkbox',
props: {
id: 'checkbox-1-nested-1',
formId: '18',
label: 'label 2',
checkboxProps: {},
},
},
{
id: 'form-checkbox-1-nested-13',
groupType: 'form',
type: 'checkbox',
props: {
id: 'checkbox-1-nested-13',
formId: '18',
label: 'label 23',
checkboxProps: {},
},
},
],
},
},
{
Expand Down Expand Up @@ -254,7 +245,7 @@ export function App() {
// multiple: true,
defaultValue: '',
inputLabelProps: {
children: 'select label',
children: 'input label',
},
options: [
{ name: 'first', id: '1' },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { FC } from 'react';

import { FormControlLabel, Radio as MuiRadio } from '@mui/material';

import { RadioProps } from './radio.types';

import useRadio from './useRadio';

const Radio: FC<RadioProps> = (props) => {
const { getFormControlLabelProps, getRadioInputProps } = useRadio(props);

return (
<FormControlLabel
{...getFormControlLabelProps()}
control={<MuiRadio {...getRadioInputProps()} />}
/>
);
};

export default Radio;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
FormControlLabelProps,
RadioGroupProps as MuiRadioGroupProps,
RadioProps as MuiRadioProps,
} from '@mui/material';

import { Api } from '@mui-builder/types/api.types';
import { Script } from '@mui-builder/types/script.types';

import { Dependencies, FormId, Id, Option } from '../../../types/public.types';
import { Rule } from '../../../types/validation.types';

export type RadioProps = Omit<FormControlLabelProps, 'control' | 'label'> &
Option & {
radioInputProps?: MuiRadioProps;
};

export type RadioGroupProps = MuiRadioGroupProps & {
id: Id;
formId: FormId;
script?: Script;
dependesies?: Dependencies;
propsController?: Record<string, any>;
api?: Api;
rule?: Rule;
show?: boolean;
options: RadioProps[];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { FC } from 'react';

import { RadioGroup as MuiRadioGroup } from '@mui/material';

import { RadioGroupProps } from './radio.types';

import Radio from './radio';
import useRadioGroup from './useRadioGroup';

const RadioGroup: FC<RadioGroupProps> = (props) => {
const { getRadioGroupProps, options } = useRadioGroup(props);

return (
<MuiRadioGroup {...getRadioGroupProps()}>
{options.map((radio) => (
<Radio key={radio.id} {...radio} />
))}
</MuiRadioGroup>
);
};

export default RadioGroup;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { RadioProps } from './radio.types';

const useRadio = (props: RadioProps) => {
const { radioInputProps, ...restRadioProps } = props;

const getRadioInputProps = () => ({ ...radioInputProps });

const getFormControlLabelProps = () => ({ ...restRadioProps, label: restRadioProps.name });

return { getRadioInputProps, getFormControlLabelProps };
};

export default useRadio;
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { useController, useWatch } from 'react-hook-form';

import useQueryBuilder from '@mui-builder/utils/useQueryBuilder/useQueryBuilder';
import UseScript from '@mui-builder/utils/useScript/useScript';

import axios from 'axios';

import { RadioGroupProps } from './radio.types';

import useForms from '../../../hooks/useForms/useForms';
import usePropsController from '../../../hooks/usePropsController/usePropsController';
import useRule from '../../../hooks/useRule/useRule';

const useRadioGroup = (props: RadioGroupProps) => {
const {
id,
api,
script,
formId,
show = true,
dependesies,
defaultValue,
options,
...restRadioGroupProps
} = props;

const { configs, queries } = api || {};

const { forms } = useForms();
const formMethod = forms?.[formId];

const { setProps, propsController } = usePropsController();
const newProps = propsController?.[id] || {};

useQueryBuilder({
apiInstance: axios,
apiConfigs: configs ?? {},
apiQuery: queries ?? {},
formMethod,
formId,
forms,
});

// Handle Wtach Fields
useWatch({
control: formMethod.control,
name: dependesies ?? [],
});

// Controller
const {
field,
formState: { errors },
} = useController({
name: id,
control: formMethod.control,
// disabled: restRadioGroupProps.disabled,
rules: useRule(restRadioGroupProps?.rule),
defaultValue,
});

const error = errors?.[id];

// Handle Script
const { scriptResult } = UseScript({
script,
formMethod,
forms,
formId,
setProps,
});

const getRadioGroupProps = () => ({
...field,
...restRadioGroupProps,
error,
...scriptResult,
...newProps,
});

return { getRadioGroupProps, options };
};

export default useRadioGroup;
9 changes: 6 additions & 3 deletions packages/core/src/modules/form/src/types/public.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { SubmitFieldProps } from '../components/actions/submit/submit.types';
import { AutoCompleteProps } from '../components/fields/autoComplete/autoComplete.types';
import { CheckboxProps } from '../components/fields/checkbox/checkbox.types';
import { NumberFieldProps } from '../components/fields/number/number.types';
import { SelectProps } from '../components/fields/select/select.types';
import { RadioGroupProps } from '../components/fields/radio/radio.types';
import { TextProps } from '../components/fields/text/text.types';
import { Form } from '../hooks/useForms/useForms.types';
import { SelectProps } from '../components/fields/select/select.types';

export type FormId = string;

Expand All @@ -21,7 +22,8 @@ export type FormTypes =
| 'checkbox'
| 'number'
| 'password'
| 'select';
| 'select'
| 'radio';

export type Option = {
name: string;
Expand All @@ -31,8 +33,9 @@ export type Option = {
export type FieldProps =
| TextProps
| SubmitFieldProps
| AutoCompleteProps<Option>
| CheckboxProps
| RadioGroupProps
| AutoCompleteProps<Option>
| NumberFieldProps
| SelectProps;

Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/modules/form/src/utils/selector/formSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AutoCompleteProps } from '../../components/fields/autoComplete/autoComp
import { CheckboxProps } from '../../components/fields/checkbox/checkbox.types';
import { NumberFieldProps } from '../../components/fields/number/number.types';
import { PasswordProps } from '../../components/fields/password/password.types';
import { RadioGroupProps } from '../../components/fields/radio/radio.types';
import { SelectProps } from '../../components/fields/select/select.types';
import { TextProps } from '../../components/fields/text/text.types';
import { Option } from '../../types/public.types';
Expand Down Expand Up @@ -68,6 +69,17 @@ const FormSelector: FC<FormSelectorProps> = ({
</Suspense>
);

case 'radio':
SelectedComponent = lazy(
() => import('../../components/fields/radio/radioGroup')
);

return (
<Suspense key={fieldProps.id} fallback={<SubmitLoading {...loading} />}>
<SelectedComponent {...(fieldProps as RadioGroupProps)} />
</Suspense>
);
Comment on lines +72 to +81
Copy link

Choose a reason for hiding this comment

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

The implementation for the 'radio' case in the switch statement follows the established pattern and correctly uses lazy loading and suspense. Consider using a more specific loading component for radio fields, similar to other field types, to enhance user experience.

- <Suspense key={fieldProps.id} fallback={<SubmitLoading {...loading} />}>
+ <Suspense key={fieldProps.id} fallback={<RadioLoading {...loading} />}>

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
case 'radio':
SelectedComponent = lazy(
() => import('../../components/fields/radio/radioGroup')
);
return (
<Suspense key={fieldProps.id} fallback={<SubmitLoading {...loading} />}>
<SelectedComponent {...(fieldProps as RadioGroupProps)} />
</Suspense>
);
case 'radio':
SelectedComponent = lazy(
() => import('../../components/fields/radio/radioGroup')
);
return (
<Suspense key={fieldProps.id} fallback={<RadioLoading {...loading} />}>
<SelectedComponent {...(fieldProps as RadioGroupProps)} />
</Suspense>
);


case 'number':
SelectedComponent = lazy(
() => import('../../components/fields/number/number')
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
"path": "./tsconfig.lib.json"
}
],
"extends": "../../tsconfig.base.json"
Expand Down
Loading