Skip to content

Instantly share code, notes, and snippets.

@Sidnioulz
Created December 14, 2021 16:46
Show Gist options
  • Save Sidnioulz/3e57eef3116c994f4b15aaa264b09238 to your computer and use it in GitHub Desktop.
Save Sidnioulz/3e57eef3116c994f4b15aaa264b09238 to your computer and use it in GitHub Desktop.
LJN Reclame Architecture - Gist 14
import PropTypes from '@ljn/prop-types'
import React, { useCallback, useContext } from 'react'
import { FormContext } from 'components/form/Form'
import { useReclame } from 'core/reclame'
import ButtonBase, { renderRootProps } from './partials'
export const FormButton = (initialProps) => {
const params = useReclame(FormButton, initialProps, [
ButtonBase,
{
id: 'formButton',
hook: ({ props }) => {
const form = useContext(FormContext)
const oldOnClick = props.onClick
props.onClick = useCallback(
(...args) => {
oldOnClick?.(...args)
form[props.type]?.(...args)
},
[form, oldOnClick, props.type]
)
if (props.type === 'submit') {
props.isDisabled ||= form.submitDisabled
}
},
propTypes: {
type: PropTypes.oneOf(['submit', 'reset']).isRequired,
onClick: PropTypes.func,
},
},
])
const { bem, props } = params
return (
<button className={bem.block()} {...renderRootProps(params)}>
{props.children && <span className={bem.element('inner')}>{props.children}</span>}
</button>
)
}
export default FormButton
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment