Skip to content

Commit

Permalink
BA: updates utils package v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
anicioalexandre committed Oct 3, 2023
1 parent e24a122 commit f88f789
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
6 changes: 6 additions & 0 deletions packages/utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @baseapp-frontend/utils

## 1.3.0

### Minor Changes

- Update `withController` so it passes a `helperText` and an `error` prop to the component. So it fits better the MUI's error display dynamic.

## 1.2.2

### Patch Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('withController', () => {

it('should pass down the correct props', () => {
const { queryByText } = render(
<WrappedComponent name="test" someProp="prop" control={{}} enableError />,
<WrappedComponent name="test" someProp="prop" control={{}} error />,
)
const testComponent = queryByText('Test')

Expand Down
10 changes: 8 additions & 2 deletions packages/utils/functions/form/withController/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import { Controller } from 'react-hook-form'
import { WithControllerProps } from './types'

function withController<T>(Component: FC<T>) {
return ({ name, control, ...props }: WithControllerProps<T>) => {
return ({ name, control, helperText, ...props }: WithControllerProps<T>) => {
if (control) {
return (
<Controller
name={name}
control={control}
render={({ field, fieldState }) => (
<Component {...field} error={fieldState.error} name={name} {...(props as any)} />
<Component
{...field}
error={!!fieldState.error}
name={name}
helperText={helperText || (!!fieldState.error && fieldState.error?.message)}
{...(props as any)}
/>
)}
/>
)
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@baseapp-frontend/utils",
"description": "Util functions, constants and types.",
"version": "1.2.2",
"version": "1.3.0",
"main": "./dist/index.ts",
"module": "./dist/index.mjs",
"scripts": {
Expand Down
11 changes: 2 additions & 9 deletions packages/utils/types/form.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { Control, ControllerProps, FieldError, FieldValues } from 'react-hook-form'
import { Control, ControllerProps, FieldValues } from 'react-hook-form'

import { RequireAllOrNone } from './typescript'

export interface IFormControl extends Pick<ControllerProps, 'name'> {
control?: Control<FieldValues[string]>
error?: FieldError
enableError?: boolean
}

export type ControlProps = RequireAllOrNone<{
control?: Control<FieldValues[string]>
name?: ControllerProps<FieldValues>['name']
}>

export type FormControl = ControlProps & {
error?: FieldError
enableError?: boolean
helperText?: any
}

0 comments on commit f88f789

Please sign in to comment.