generated from atls-lab/template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add use-custom-element hook, extend form context, edit interfaces
- Loading branch information
1 parent
84addff
commit f8d6d3c
Showing
19 changed files
with
282 additions
and
158 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
77 changes: 77 additions & 0 deletions
77
packages/payment-widget/src/hooks/use-custom-elements.hook.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,77 @@ | ||
import { Children } from 'react' | ||
import { ReactNode } from 'react' | ||
import { JSXElementConstructor } from 'react' | ||
import { isValidElement } from 'react' | ||
|
||
import { AdditionalFieldsType } from '../enums' | ||
import { RequiredFieldsType } from '../enums' | ||
import { CustomElements } from '../interfaces' | ||
import { Field } from '../interfaces' | ||
|
||
export const useCustomElements = ( | ||
existAmount: boolean, | ||
existReceipt: boolean, | ||
existAdditionalFields: boolean, | ||
nodes: ReactNode | ||
): CustomElements => { | ||
const nodeArray = Children.toArray(nodes) | ||
|
||
const isCastomElement = (nameNode: string, node: ReactNode): boolean => | ||
isValidElement(node) | ||
? (node.type as JSXElementConstructor<any>).name === nameNode && | ||
typeof node.props.children === 'function' | ||
: false | ||
const isAdditionalField = (node: ReactNode): boolean => | ||
isValidElement(node) ? Object.values(AdditionalFieldsType).includes(node.props.name) : false | ||
const isRequiredField = (node: ReactNode): boolean => | ||
isValidElement(node) ? node.props.name === RequiredFieldsType.Amount && !existAmount : false | ||
|
||
const customFields = nodeArray.filter( | ||
(node) => | ||
isCastomElement('InputWrapper', node) && (isAdditionalField(node) || isRequiredField(node)) | ||
) | ||
const nameFields = customFields.reduce<Field[]>((acc, field) => { | ||
if (isValidElement(field)) | ||
acc.push({ | ||
name: AdditionalFieldsType[field.props.name] || RequiredFieldsType[field.props.name], | ||
}) | ||
return acc | ||
}, []) | ||
|
||
const customButton = nodeArray.find((node) => isCastomElement('ButtonWrapper', node)) | ||
|
||
const isGenerateReceiptField = existReceipt && !customFields.length | ||
|
||
const isGenerateRequiredField = !existAmount && existAdditionalFields | ||
|
||
const existCustomAmountField = customFields.some( | ||
(field) => isValidElement(field) && field.props.name === RequiredFieldsType.Amount | ||
) | ||
const existCustomReceiptField = customFields.some( | ||
(field) => | ||
isValidElement(field) && | ||
(field.props.name === AdditionalFieldsType.Email || | ||
field.props.name === AdditionalFieldsType.Phone) | ||
) | ||
|
||
if (existAdditionalFields && customFields.length) | ||
throw new Error('Don`t use additionalFields property with InputWrapper component') | ||
|
||
if (customFields.length && !existAmount && !existCustomAmountField) | ||
throw new Error( | ||
'If you use InputWrapper component and don`t set amount property, you mast use InputWrapper componnet with property name equal RequiredFieldsType.Amount' | ||
) | ||
|
||
if (customFields.length && existReceipt && !existCustomReceiptField) | ||
throw new Error( | ||
'If you set receipt property whith InputWrapper component, you mast use InputWrapper componnet with property name equal AdditionalFieldsType.Phone or AdditionalFieldsType.Email' | ||
) | ||
|
||
return { | ||
customFields, | ||
customButton, | ||
isGenerateReceiptField, | ||
isGenerateRequiredField, | ||
nameFields, | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
packages/payment-widget/src/interfaces/custom-elements.iterfaces.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,13 @@ | ||
import { ReactNode } from 'react' | ||
|
||
import { Field } from './fields.interfaces' | ||
|
||
export type NameField = Pick<Field, 'name'> | ||
|
||
export interface CustomElements { | ||
customFields: ReactNode[] | ||
customButton: ReactNode | ||
isGenerateReceiptField: boolean | ||
isGenerateRequiredField: boolean | ||
nameFields: Field[] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,27 @@ | ||
import { ReactNode } from 'react' | ||
|
||
import { NameField } from './custom-elements.iterfaces' | ||
import { Field } from './fields.interfaces' | ||
import { FieldsState } from './fields.interfaces' | ||
import { FieldsValidator } from './fields.interfaces' | ||
import { WidgetProps } from './widget.interfaces' | ||
|
||
export interface FormProps extends WidgetProps { | ||
useCustomFields: boolean | ||
useCustomButton: boolean | ||
isGenerateReceipt: boolean | ||
children: ReactNode | ReactNode[] | ||
} | ||
|
||
export interface FormProviderProps { | ||
additionalFields?: Field[] | ||
nameFields: Field[] | ||
isGenerateReceipt: boolean | ||
isGenerateRequiredField: boolean | ||
disabled: boolean | ||
children: ReactNode | ReactNode[] | ||
children: ReactNode | ||
} | ||
|
||
export interface FormContext extends FieldsValidator { | ||
export interface FormContext extends FieldsValidator, FieldsState { | ||
fields: Field[] | NameField[] | ||
disabled: boolean | ||
isLoaded: boolean | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import { ReactNode } from 'react' | ||
|
||
export interface ThemeProps { | ||
useCustomTheme: boolean | ||
children: ReactNode | ||
} |
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
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
Oops, something went wrong.