-
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
- Loading branch information
1 parent
c547b91
commit c2a905a
Showing
9 changed files
with
207 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; |
21 changes: 21 additions & 0 deletions
21
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,21 @@ | ||
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; | ||
} & { | ||
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; |
103 changes: 103 additions & 0 deletions
103
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,103 @@ | ||
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, | ||
...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) => { | ||
return value.replace(/\B(?=(\d{3})+(?!\d))/g, ','); | ||
}; | ||
|
||
// Handle changes to the input field | ||
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
// Remove existing commas from the input value | ||
const rawValue = event.target.value.replace(/,/g, ''); | ||
// 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]; | ||
|
||
// Props | ||
const getFieldProps = () => ({ | ||
...field, | ||
...numberFieldProps, | ||
helperText: error?.message ?? helperText, | ||
error: !!error, | ||
...scriptResult, | ||
...newProps, | ||
value: formatNumber(field.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