From c98ff68b9c2826e6e4f7f91f1821a21d2a0d8707 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Tue, 23 Feb 2021 19:14:06 +0200 Subject: [PATCH] fix: fix typescript errors when trying to use customComponents MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit v3.0.0 didn't work like i intended 😬 BREAKING CHANGE: Modify Webform component props --- src/Webform.tsx | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/Webform.tsx b/src/Webform.tsx index 9967a64..1019d8a 100644 --- a/src/Webform.tsx +++ b/src/Webform.tsx @@ -80,13 +80,6 @@ export class WebformError extends Error { type CustomComponentLibrary = { [name: string]: WebformCustomComponent -} & { - /** - * Button is special component type that accepts looser parameters. - * - * This is to make it compatible with existing button components. - * */ - button: React.ComponentType } /** @@ -121,18 +114,24 @@ interface Props { /** Extra data to POST. */ extraData?: object + /** + * Override button component that the form uses. + * */ + buttonComponent: React.ComponentType + /** Provide custom components that handle specific webform elements. */ customComponents: CustomComponentLibrary } -const DefaultComponents: CustomComponentLibrary = { - button: WebformButton -} - /** * Render single webform element. */ -export function renderWebformElement(customComponents: CustomComponentLibrary, element: WebformElement, error?: string) { +export function renderWebformElement( + customComponents: CustomComponentLibrary, + Button: React.ComponentType, + element: WebformElement, + error?: string +) { const customComponentAPI = { error } @@ -172,9 +171,7 @@ export function renderWebformElement(customComponents: CustomComponentLibrary, e case 'webform_actions': return (
- - {getAttributeValue('#submit__label', element) || DEFAULT_SUBMIT_LABEL} - +
) // Unknown element type -> render as json string @@ -186,7 +183,7 @@ export function renderWebformElement(customComponents: CustomComponentLibrary, e /** * Drupal webform react component. */ -const Webform = ({ webform, customComponents, ...props }: Props) => { +const Webform = ({ webform, customComponents, buttonComponent, ...props }: Props) => { const [errors, setErrors] = useState({}) const submitHandler: React.FormEventHandler = async (event) => { @@ -266,22 +263,23 @@ const Webform = ({ webform, customComponents, ...props }: Props) => { * Webform object should rarely change. */ const elements = useMemo(() => { - const components = { ...DefaultComponents, ...customComponents } + const Button = buttonComponent + const ret = webform.elements.map((element) => ( - {renderWebformElement(components, element, errors[element.name])} + {renderWebformElement(customComponents, Button, element, errors[element.name])} )) // Render default submit button if it is not defined in elements array. if (webform.elements.find((element) => element.type === 'webform_actions') === undefined) { ret.push( - + ) } return ret - }, [webform, customComponents, errors]) + }, [webform, customComponents, buttonComponent, errors]) return (
{ } Webform.defaultProps = { - customComponents: {} + customComponents: {}, + buttonComponent: WebformButton } export default Webform