Skip to content

Commit 7ff8bee

Browse files
authored
BA - add text input with formik option (#15)
* Add-TextInput-with-Formik-option * Release TextField
1 parent d33108a commit 7ff8bee

File tree

13 files changed

+94
-50
lines changed

13 files changed

+94
-50
lines changed

apps/docs/pages/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ export default function Docs() {
2929

3030
<div style={{ width: theme.spacing(48) }}>
3131
<h2>{`<PasswordField />`}</h2>
32-
<PasswordField />
32+
<PasswordField name="password" />
3333

34-
<PasswordField helperText="Type your password." />
34+
<PasswordField name="password" helperText="Type your password." />
3535

36-
<PasswordField error helperText="Incorrect entry." />
36+
<PasswordField name="password" error helperText="Incorrect entry." />
3737
</div>
3838

3939
<Divider style={{ margin: theme.spacing(2, 0) }} />

packages/design-system-mui/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# @baseapp-frontend/design-system-mui
22

3+
## 1.1.0
4+
5+
### Minor Changes
6+
7+
- Added TextField
8+
- Added support to Formik (These will be changed in the future, just added to the sake of keep current baseapp-nextjs-mui-template working)
9+
310
## 1.0.1
411

512
### Patch Changes

packages/design-system-mui/components/ButtonWithLoading/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ function ButtonWithLoading({
66
loading = false,
77
children,
88
loadingChildren = <CircularProgress size="20px" />,
9+
formik,
910
...props
1011
}: IButtonWitthLoadingProps) {
12+
const _isLoading = formik ? formik.isSubmitting : loading
1113
return (
1214
<Button disabled={loading} {...props}>
1315
{children}
14-
{loading && loadingChildren}
16+
{_isLoading && loadingChildren}
1517
</Button>
1618
)
1719
}

packages/design-system-mui/components/ButtonWithLoading/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export interface IButtonWitthLoadingProps extends ButtonProps {
44
children?: React.FC<any> | React.Element | string
55
loading?: boolean
66
loadingChildren?: React.FC<any> | React.Element
7+
formik?: any // eslint-disable-line @typescript-eslint/no-explicit-any
78
}

packages/design-system-mui/components/ImageUploader/index.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { ReactChild, ReactFragment, ReactPortal, Key } from 'react'
77
function ImageUploader({
88
images,
99
setImages,
10-
buttonLabel,
11-
buttonRemoveLabel,
10+
buttonLabel = 'Upload Image',
11+
buttonRemoveLabel = 'Remove Image',
1212
name,
1313
error,
1414
helperText,
@@ -18,6 +18,8 @@ function ImageUploader({
1818
UploaderButtonProps,
1919
...props
2020
}: IImageUploadInput) {
21+
// @TODO: Add support for multiple images
22+
// @TODO Add formik or other form context to support form validation
2123
const deleteImage = (index: number) => {
2224
const newImages = [...images]
2325
newImages.splice(index, 1)
@@ -47,7 +49,7 @@ function ImageUploader({
4749
{...UploaderButtonProps}
4850
>
4951
<UploadFileOutlinedIcon sx={{ marginRight: 1 }} />
50-
{buttonLabel || 'Upload Photo'}
52+
{buttonLabel}
5153
</UploaderButton>
5254
<input
5355
name={name}
@@ -59,7 +61,7 @@ function ImageUploader({
5961
{...props}
6062
/>
6163
{error && <FormHelperText>{helperText}</FormHelperText>}
62-
{images.map((img: ImageFile, index: number) => (
64+
{images?.map((img: ImageFile, index: number) => (
6365
<ImageGroup key={index}>
6466
<Image src={img.imagePreviewUrl as string} alt="preview" {...ImageProps} />
6567
<LabelGroup>
@@ -72,7 +74,7 @@ function ImageUploader({
7274
color="primary"
7375
{...DeleteButtonProps}
7476
>
75-
{buttonRemoveLabel || 'Remove Photo'}
77+
{buttonRemoveLabel}
7678
</DeleteButton>
7779
</LabelGroup>
7880
</ImageGroup>
Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
1-
export interface IInputBaseComponentProps
2-
extends React.HTMLAttributes<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement> {
3-
// Accommodate arbitrary additional props coming from the `IInputProps` prop
4-
[arbitrary: string]: any
5-
}
6-
7-
export interface IInputProps extends IInputBaseComponentProps {
8-
component?: React.ElementType<IInputBaseComponentProps> | React.FC<any>
9-
templateComponent?: React.FC<any>
10-
name: string
11-
label?: string
12-
helperText?: string
13-
}
14-
15-
interface IImageUploadInput extends IInputProps {
1+
export interface IImageUploadInput extends IInputProps {
162
ImageProps?: IImageProps
173
ImageLabelProps?: IImageLabelProps
184
DeleteButtonProps?: IImageDeleteButtonProps
19-
image?: ImageFile[]
5+
images: ImageFile[]
6+
setImages: Dispatch<SetStateAction<never[]>>
7+
buttonLabel?: string
8+
buttonRemoveLabel?: string
9+
name?: string
10+
UploaderButtonProps?: IImageUploaderButtonProps
11+
error?: boolean
12+
helperText?: string
2013
}
2114

22-
interface ImageFile {
15+
export interface ImageFile {
2316
file: File
2417
imagePreviewUrl: string | ArrayBuffer | null
2518
}

packages/design-system-mui/components/PasswordField/index.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@ import IconButton from '@mui/material/IconButton'
33
import InputAdornment from '@mui/material/InputAdornment'
44
import Visibility from '@mui/icons-material/Visibility'
55
import VisibilityOff from '@mui/icons-material/VisibilityOff'
6-
import { TextField } from './styled'
6+
import { StyledPasswordTextField } from './styled'
77
import { useTheme } from '@mui/material'
8-
import { IPasswordFieldProps } from './types'
8+
import { ITextField } from '../TextField/types'
99

1010
function PasswordField({
1111
name = 'password',
1212
visibilityIconColor,
1313
InputLabelProps,
1414
InputProps,
15+
formik,
1516
...props
16-
}: IPasswordFieldProps): ReactElement {
17+
}: ITextField): ReactElement {
1718
const [viewPassword, setViewPassword] = useState(false)
1819
const theme = useTheme()
1920
return (
20-
<TextField
21+
<StyledPasswordTextField
2122
label="Password"
2223
placeholder="Password"
2324
type={viewPassword ? 'text' : 'password'}
2425
name={name}
26+
formik={formik}
2527
InputLabelProps={{ ...InputLabelProps }}
2628
InputProps={{
2729
endAdornment: (
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { styled, makeStyles } from '@mui/material/styles'
2-
import { TextField as MuiITextField, TextFieldProps } from '@mui/material'
3-
import { FC } from 'react'
1+
import { styled } from '@mui/material/styles'
2+
import TextField from '../TextField'
43

5-
const TextField = styled(MuiITextField)(({ theme }) => ({
4+
const StyledPasswordTextField = styled(TextField)(({ theme }) => ({
65
width: '100%',
76
marginBottom: theme.spacing(2),
8-
})) as unknown as FC<TextFieldProps>
7+
}))
98

10-
export { TextField }
9+
export { StyledPasswordTextField }

packages/design-system-mui/components/PasswordField/types.d.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import MuiTextField from '@mui/material/TextField'
2+
import { ITextField } from './types'
3+
4+
function TextField({ name, formik, helperText, value, handleChange, ...props }: ITextField) {
5+
const showError = (formik?.errors?.[name] && formik?.touched?.[name]) as boolean
6+
return (
7+
<MuiTextField
8+
name={name}
9+
onChange={formik?.handleChange || handleChange}
10+
value={formik?.values?.[name] || value}
11+
error={showError}
12+
helperText={showError ? formik.errors?.[name] : helperText}
13+
{...props}
14+
/>
15+
)
16+
}
17+
18+
export default TextField

0 commit comments

Comments
 (0)