Skip to content

Commit

Permalink
feat: added field descriptions & updated tests (#2914)
Browse files Browse the repository at this point in the history
  • Loading branch information
chesterkmr authored Dec 27, 2024
1 parent 49869ff commit 82d8388
Show file tree
Hide file tree
Showing 31 changed files with 406 additions and 302 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const schema: Array<IFormElement<any, any>> = [
params: {
label: 'Text Field',
placeholder: 'Enter text',
description: 'This is a text field for entering any text value',
},
validate: [],
},
Expand All @@ -22,6 +23,7 @@ const schema: Array<IFormElement<any, any>> = [
params: {
label: 'Autocomplete Field',
placeholder: 'Select an option',
description: 'This is an autocomplete field that provides suggestions as you type',
options: [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' },
Expand All @@ -35,6 +37,7 @@ const schema: Array<IFormElement<any, any>> = [
valueDestination: 'checkboxlist',
params: {
label: 'Checkbox List Field',
description: 'Select multiple options from this list of checkboxes',
options: [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' },
Expand All @@ -48,6 +51,7 @@ const schema: Array<IFormElement<any, any>> = [
valueDestination: 'date',
params: {
label: 'Date Field',
description: 'Select a date from the calendar',
},
},
{
Expand All @@ -56,6 +60,7 @@ const schema: Array<IFormElement<any, any>> = [
valueDestination: 'multiselect',
params: {
label: 'Multiselect Field',
description: 'Select multiple options from the dropdown list',
options: [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' },
Expand All @@ -69,6 +74,7 @@ const schema: Array<IFormElement<any, any>> = [
valueDestination: 'select',
params: {
label: 'Select Field',
description: 'Choose a single option from the dropdown list',
options: [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' },
Expand All @@ -82,6 +88,7 @@ const schema: Array<IFormElement<any, any>> = [
valueDestination: 'checkbox',
params: {
label: 'Checkbox Field',
description: 'Toggle this checkbox for a yes/no selection',
},
},
{
Expand All @@ -90,6 +97,7 @@ const schema: Array<IFormElement<any, any>> = [
valueDestination: 'phone',
params: {
label: 'Phone Field',
description: 'Enter a phone number with country code selection',
defaultCountry: 'il',
},
},
Expand All @@ -99,6 +107,7 @@ const schema: Array<IFormElement<any, any>> = [
valueDestination: 'radio',
params: {
label: 'Radio Field',
description: 'Select one option from these radio buttons',
options: [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' },
Expand All @@ -112,6 +121,7 @@ const schema: Array<IFormElement<any, any>> = [
valueDestination: 'tags',
params: {
label: 'Tags Field',
description: 'Add multiple tags by typing and pressing enter',
},
},
{
Expand All @@ -121,6 +131,7 @@ const schema: Array<IFormElement<any, any>> = [
params: {
label: 'File Field',
placeholder: 'Select File',
description: 'Upload a file from your device',
},
},
{
Expand All @@ -129,6 +140,7 @@ const schema: Array<IFormElement<any, any>> = [
valueDestination: 'fieldlist',
params: {
label: 'Field List',
description: 'A list of repeatable form fields that can be added or removed',
},
children: [
{
Expand All @@ -138,6 +150,7 @@ const schema: Array<IFormElement<any, any>> = [
params: {
label: 'Text Field',
placeholder: 'Enter text',
description: 'Enter text for this list item',
},
validate: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useStack } from '../FieldList/providers/StackProvider';

import { useMountEvent } from '../../hooks/internal/useMountEvent';
import { useUnmountEvent } from '../../hooks/internal/useUnmountEvent';
import { FieldDescription } from '../../layouts/FieldDescription';

export interface IAutocompleteFieldOption {
label: string;
Expand Down Expand Up @@ -46,6 +47,7 @@ export const AutocompleteField: TDynamicFormField<IAutocompleteFieldParams> = ({
onBlur={onBlur}
onFocus={onFocus}
/>
<FieldDescription element={element} />
<FieldErrors element={element} />
</FieldLayout>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
import { Checkbox } from '@/components/atoms';
import { Checkbox, Label } from '@/components/atoms';
import { useDynamicForm } from '../../context';
import { useElement, useField } from '../../hooks/external';
import { useRequired } from '../../hooks/external/useRequired';
import { useMountEvent } from '../../hooks/internal/useMountEvent';
import { useUnmountEvent } from '../../hooks/internal/useUnmountEvent';
import { FieldDescription } from '../../layouts/FieldDescription';
import { FieldErrors } from '../../layouts/FieldErrors';
import { FieldLayout } from '../../layouts/FieldLayout';
import { TDynamicFormField } from '../../types';
import { ICommonFieldParams, TDynamicFormField } from '../../types';
import { useStack } from '../FieldList/providers/StackProvider';

export const CheckboxField: TDynamicFormField = ({ element }) => {
export const CheckboxField: TDynamicFormField<ICommonFieldParams> = ({ element }) => {
useMountEvent(element);
useUnmountEvent(element);

const { label } = element.params || {};
const { stack } = useStack();
const { id } = useElement(element, stack);
const { value, onChange, onFocus, onBlur, disabled } = useField<boolean | undefined>(
element,
stack,
);
const { values } = useDynamicForm();
const isRequired = useRequired(element, values);

return (
<FieldLayout element={element} layout="horizontal">
<Checkbox
id={id}
checked={Boolean(value)}
onCheckedChange={onChange}
disabled={disabled}
onFocus={onFocus}
onBlur={onBlur}
/>
<div className="flex flex-col">
<div className="flex flex-row flex-nowrap items-center gap-2">
<Checkbox
id={id}
checked={Boolean(value)}
onCheckedChange={onChange}
disabled={disabled}
onFocus={onFocus}
onBlur={onBlur}
/>
<Label id={`${id}-label`} htmlFor={`${id}`}>
{`${isRequired ? `${label}` : `${label} (optional)`} `}
</Label>
</div>
<FieldDescription element={element} />
<FieldErrors element={element} />
</FieldLayout>
</div>
);
};
Loading

0 comments on commit 82d8388

Please sign in to comment.