-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(form): number input with comma seperator (#53)
* feat(form): number input with comma seperator * feat: add seperator for number input --------- Co-authored-by: mrbadri <mrbadri.dev@gmail.com>
- Loading branch information
1 parent
03b1e98
commit d20e56c
Showing
9 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"CodeGPT.apiKey": "CodeGPT Plus Beta" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/core/src/modules/form/src/components/fields/number/number.loading.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Skeleton } from '@mui/material'; | ||
|
||
import { LoadingProps } from '@mui-builder/types/configs.type'; | ||
|
||
import useNumberFieldLoading from './useNumber.loading'; | ||
|
||
const NumberFieldLoading = (props: LoadingProps) => { | ||
const { getNumberFieldLoadingProps } = useNumberFieldLoading(props); | ||
|
||
return <Skeleton {...getNumberFieldLoadingProps()} />; | ||
}; | ||
|
||
export default NumberFieldLoading; |
17 changes: 17 additions & 0 deletions
17
packages/core/src/modules/form/src/components/fields/number/number.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { FC } from 'react'; | ||
|
||
import { TextField } from '@mui/material'; | ||
|
||
import { NumberFieldProps } from './number.types'; | ||
|
||
import useNumberField from './useNumber'; | ||
|
||
const NumberField: FC<NumberFieldProps> = (props) => { | ||
const { getFieldProps, show } = useNumberField(props); | ||
|
||
if (show) return <TextField {...getFieldProps()} />; | ||
|
||
return null; | ||
}; | ||
|
||
export default NumberField; |
22 changes: 22 additions & 0 deletions
22
packages/core/src/modules/form/src/components/fields/number/number.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { TextFieldProps } from '@mui/material'; | ||
|
||
import { Api } from '@mui-builder/types/api.types'; | ||
import { Script } from '@mui-builder/types/script.types'; | ||
|
||
import { Dependesies, FormId, Id } from '../../../types/public.types'; | ||
import { Rule } from '../../../types/validation.types'; | ||
|
||
export type NumberFieldProps = Omit<TextFieldProps, 'onChange'> & { | ||
value: string; | ||
onChange: (value: string) => void; | ||
} & { | ||
seperator?: string; | ||
id: Id; | ||
formId: FormId; | ||
script?: Script; | ||
dependesies?: Dependesies; | ||
propsController?: Record<string, any>; | ||
api?: Api; | ||
rule?: Rule; | ||
show?: boolean; | ||
}; |
22 changes: 22 additions & 0 deletions
22
packages/core/src/modules/form/src/components/fields/number/useNumber.loading.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { LoadingProps } from '@mui-builder/types/configs.type'; | ||
|
||
const useNumberFieldLoading = (props: LoadingProps) => { | ||
const { animation = 'wave', sx, ...otherProps } = props; | ||
|
||
const getNumberFieldLoadingProps = () => ({ | ||
sx: { | ||
display: 'inline-block', | ||
transform: 'unset', | ||
width: '255px', | ||
height: '56px', | ||
mx: 0.5, | ||
...sx, | ||
}, | ||
animation, | ||
...otherProps, | ||
}); | ||
|
||
return { getNumberFieldLoadingProps }; | ||
}; | ||
|
||
export default useNumberFieldLoading; |
113 changes: 113 additions & 0 deletions
113
packages/core/src/modules/form/src/components/fields/number/useNumber.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
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 { NumberFieldProps } from './number.types'; | ||
|
||
import useForms from '../../../hooks/useForms/useForms'; | ||
import usePropsController from '../../../hooks/usePropsController/usePropsController'; | ||
import useRule from '../../../hooks/useRule/useRule'; | ||
|
||
const UseNumberField = (props: NumberFieldProps) => { | ||
const { | ||
formId, | ||
script, | ||
api, | ||
show = true, | ||
dependesies, | ||
helperText, | ||
defaultValue, | ||
onChange, | ||
seperator, | ||
...numberFieldProps | ||
} = props; | ||
const { configs, queries } = api || {}; | ||
|
||
const { forms } = useForms(); | ||
const formMethod = forms?.[formId]; | ||
const { setProps, propsController } = usePropsController(); | ||
|
||
const newProps = propsController?.[numberFieldProps?.id] || {}; | ||
|
||
// Handle Script | ||
const { scriptResult } = UseScript({ | ||
script, | ||
formMethod, | ||
forms, | ||
formId, | ||
setProps, | ||
}); | ||
|
||
// Function to format the number with thousands separators | ||
const formatNumber = (value: string, seperator: string) => { | ||
return value.replace(/\B(?=(\d{3})+(?!\d))/g, seperator); | ||
}; | ||
|
||
// Handle changes to the input field | ||
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
let rawValue = event.target.value; | ||
|
||
// Remove existing commas from the input value | ||
if (seperator) { | ||
const regex = new RegExp(seperator); | ||
rawValue = event.target.value.replace(regex, ''); | ||
} | ||
|
||
// Check if the value is a valid number before calling onChange | ||
if (!isNaN(Number(rawValue))) { | ||
formMethod.setValue(numberFieldProps.id, rawValue); | ||
} | ||
}; | ||
|
||
// Handle Wtach Fields | ||
useWatch({ | ||
control: formMethod.control, | ||
name: dependesies ?? [], | ||
}); | ||
|
||
// API Call | ||
useQueryBuilder({ | ||
apiInstance: axios, | ||
apiConfigs: configs ?? {}, | ||
apiQuery: queries ?? {}, | ||
formMethod, | ||
formId, | ||
forms, | ||
}); | ||
|
||
// Controller | ||
const { | ||
field, | ||
formState: { errors }, | ||
} = useController({ | ||
name: numberFieldProps.id, | ||
control: formMethod.control, | ||
disabled: numberFieldProps.disabled, | ||
rules: useRule(numberFieldProps?.rule), | ||
defaultValue, | ||
}); | ||
|
||
const error = errors?.[numberFieldProps.id]; | ||
const value = seperator | ||
? formatNumber(field.value ?? '', seperator) | ||
: field.value; | ||
|
||
// Props | ||
const getFieldProps = () => ({ | ||
...field, | ||
...numberFieldProps, | ||
helperText: error?.message ?? helperText, | ||
error: !!error, | ||
...scriptResult, | ||
...newProps, | ||
value, | ||
onChange: (e: React.ChangeEvent<HTMLInputElement>) => handleChange(e), | ||
}); | ||
|
||
return { getFieldProps, show }; | ||
}; | ||
|
||
export default UseNumberField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters