perf: reduce drastically build size

BREAKING CHANGE: This package is now pure ESM
This commit is contained in:
Divlo
2022-04-06 23:52:50 +02:00
parent ec4929d7d8
commit 686b5643b3
24 changed files with 9454 additions and 13731 deletions

View File

@ -0,0 +1,40 @@
import { render, cleanup, fireEvent } from '@testing-library/react'
import { Form, HandleForm } from '..'
afterEach(cleanup)
describe('<Form />', () => {
it('should get the formData and formElement onSubmit and onChange', () => {
let formData: { [k: string]: any } = {}
let formElement: any = null
const handleSubmitChange: HandleForm = (data, element) => {
formData = data
formElement = element
}
const formComponent = render(
<Form onSubmit={handleSubmitChange} onChange={handleSubmitChange}>
<input data-testid='input-form' type='text' name='inputName' />
<button data-testid='button-submit' type='submit'>
Submit
</button>
</Form>
)
const inputForm = formComponent.getByTestId(
'input-form'
) as HTMLInputElement
const buttonSubmit = formComponent.getByTestId('button-submit')
const text = 'some random text'
fireEvent.change(inputForm, { target: { value: text } })
expect(formData.inputName).toEqual(text)
expect(formElement instanceof HTMLFormElement).toBeTruthy()
formData = {}
formElement = null
fireEvent.click(buttonSubmit)
expect(Object.keys(formData).length).toEqual(1)
expect(formData.inputName).toEqual(text)
expect(formElement instanceof HTMLFormElement).toBeTruthy()
})
})

3
src/__test__/setup.ts Normal file
View File

@ -0,0 +1,3 @@
import React from 'react'
global.React = React

View File

@ -1,4 +1,4 @@
import React, { useRef } from 'react'
import { useRef } from 'react'
export interface FormDataObject {
[key: string]: FormDataEntryValue
@ -23,7 +23,7 @@ export const getFormDataObject = (
return Object.fromEntries<FormDataEntryValue>(new FormData(formElement))
}
export const Form = (props: FormProps): JSX.Element => {
export const Form: React.FC<FormProps> = (props) => {
const { onSubmit, onChange, children, ...rest } = props
const formRef = useRef<HTMLFormElement>(null)