diff --git a/package.json b/package.json index b7c5cab2..5ad6e5e1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dassana-io/web-components", - "version": "0.7.5", + "version": "0.7.6", "publishConfig": { "registry": "https://npm.pkg.github.com/dassana-io" }, diff --git a/src/components/Form/FieldContext.ts b/src/components/Form/FieldContext.ts index 11674868..708e5c8f 100644 --- a/src/components/Form/FieldContext.ts +++ b/src/components/Form/FieldContext.ts @@ -3,7 +3,6 @@ import { FieldValues } from 'react-hook-form/dist/types/form' import { SubmitHandler } from 'react-hook-form' export interface FieldContextProps { - initialValues: FieldValues loading: boolean onSubmit: SubmitHandler } diff --git a/src/components/Form/Form.test.tsx b/src/components/Form/Form.test.tsx index b41451c8..781e682b 100644 --- a/src/components/Form/Form.test.tsx +++ b/src/components/Form/Form.test.tsx @@ -52,37 +52,12 @@ describe('Form', () => { it('passes the correct props to FieldContext provider', () => { expect(wrapper.find(FieldContext.Provider).props().value).toMatchObject( { - initialValues: mockInitialValues, loading: false, onSubmit: mockOnSubmit } ) }) - it('correctly defaults initial values to empty object if none is passed in', () => { - wrapper = shallow( -
- Submit -
- ) - - expect( - wrapper.find(FieldContext.Provider).props().value - ).toMatchObject({ initialValues: {} }) - }) - - it('correctly updates initial values', () => { - const form = mount( -
- Submit -
- ) - - form.setProps({ initialValues: mockInitialValues }) - - expect(mockReset).toHaveBeenCalledWith(mockInitialValues) - }) - it('exposes form methods when a ref is passed', () => { const formRef = createRef() diff --git a/src/components/Form/FormInput/FormInput.test.tsx b/src/components/Form/FormInput/FormInput.test.tsx index 15fa2f09..d4ac8092 100644 --- a/src/components/Form/FormInput/FormInput.test.tsx +++ b/src/components/Form/FormInput/FormInput.test.tsx @@ -40,7 +40,6 @@ const getMountedFormInput = (formInputProps: Partial = {}) => mount( { expect(wrapper).toHaveLength(1) }) - it('correctly passes a default value from initial values if it exists', () => { - expect(wrapper.find(Controller).props().defaultValue).toEqual('bar') - }) - it('should render an Input component', () => { const input = wrapper.find(Controller).invoke('render')!(mockRenderArgs) @@ -76,7 +71,6 @@ describe('FormInput', () => { wrapper = mount( { expect(mockFocus).toHaveBeenCalled() }) + it('prevents default behavior when enter is pressed within the input', () => { + const input = wrapper.find(Controller).invoke('render')!(mockRenderArgs) + const mockPreventDefault = jest.fn() + + input.props.onKeyDown({ + key: 'Enter', + preventDefault: mockPreventDefault + }) + + expect(mockPreventDefault).toHaveBeenCalled() + }) + + it('does not prevent default behavior when other keys are pressed within the input', () => { + const input = wrapper.find(Controller).invoke('render')!(mockRenderArgs) + const mockPreventDefault = jest.fn() + + input.props.onKeyDown({ + key: 'Escape', + preventDefault: mockPreventDefault + }) + + expect(mockPreventDefault).not.toHaveBeenCalled() + }) + it('clears errors on focus if there are any', () => { wrapper = getMountedFormInput() diff --git a/src/components/Form/FormInput/index.tsx b/src/components/Form/FormInput/index.tsx index 2f2bf5e6..6f9b98a7 100644 --- a/src/components/Form/FormInput/index.tsx +++ b/src/components/Form/FormInput/index.tsx @@ -6,7 +6,7 @@ import { getFormFieldDataTag } from '../utils' import { Controller, useFormContext } from 'react-hook-form' import FieldContext, { FieldContextProps } from '../FieldContext' import { Input, InputProps } from 'components/Input' -import React, { FC, useContext, useEffect, useRef } from 'react' +import React, { FC, KeyboardEvent, useContext, useEffect, useRef } from 'react' export interface FormInputProps extends BaseFieldProps, @@ -26,9 +26,7 @@ const FormInput: FC = ({ }: FormInputProps) => { const inputRef = useRef(null) const { clearErrors, control, errors } = useFormContext() - const { initialValues, loading } = useContext( - FieldContext - ) + const { loading } = useContext(FieldContext) const errorMsg = errors[name] ? errors[name].message : '' @@ -36,6 +34,11 @@ const FormInput: FC = ({ if (errors[name]) clearErrors(name) } + const onKeyDown = (e: KeyboardEvent) => { + // This prevents the form from being automatically submitted when the Enter button is pressed + if (e.key === 'Enter') e.preventDefault() + } + useEffect(() => { if (focused && inputRef.current) { inputRef.current.focus() @@ -46,8 +49,6 @@ const FormInput: FC = ({ rules.required = true } - const defaultValue = (initialValues[name] as string) || '' - return (
{label && ( @@ -61,7 +62,6 @@ const FormInput: FC = ({ )} ( = ({ loading={loading} onChange={onChange} onFocus={onInputFocus} + onKeyDown={onKeyDown} value={value} {...rest} /> diff --git a/src/components/Form/FormRadioGroup/FormRadioGroup.test.tsx b/src/components/Form/FormRadioGroup/FormRadioGroup.test.tsx index 19032671..26c5397f 100644 --- a/src/components/Form/FormRadioGroup/FormRadioGroup.test.tsx +++ b/src/components/Form/FormRadioGroup/FormRadioGroup.test.tsx @@ -31,7 +31,6 @@ beforeEach(() => { wrapper = mount( { expect(wrapper).toHaveLength(1) }) - it('correctly passes a default value from initial values if it exists', () => { - expect(wrapper.find(Controller).props().defaultValue).toEqual('bar') - }) - it('should render a Radio Group component', () => { const radioGroup = getRenderedCmp(wrapper) @@ -72,7 +67,6 @@ describe('FormRadioGroup', () => { wrapper = mount( {} const FormRadioGroup: FC = ({ - defaultValue, label, labelSkeletonWidth, name, @@ -20,14 +19,10 @@ const FormRadioGroup: FC = ({ ...rest }: FormRadioGroupProps) => { const { control } = useFormContext() - const { initialValues, loading } = useContext( - FieldContext - ) + const { loading } = useContext(FieldContext) rules.required = true - const initialValue = (initialValues[name] as string) || defaultValue || '' - return (
{label && ( @@ -40,12 +35,10 @@ const FormRadioGroup: FC = ({ )} ( diff --git a/src/components/Form/FormSelect/FormSelect.test.tsx b/src/components/Form/FormSelect/FormSelect.test.tsx index ebf3f3e3..5411104b 100644 --- a/src/components/Form/FormSelect/FormSelect.test.tsx +++ b/src/components/Form/FormSelect/FormSelect.test.tsx @@ -24,7 +24,6 @@ beforeEach(() => { wrapper = mount( { expect(wrapper).toHaveLength(1) }) - it('correctly passes a default value from initial values if it exists', () => { - expect(wrapper.find(Controller).props().defaultValue).toEqual('bar') - }) - it('should render a Select component', () => { const test = { onChange: jest.fn(), @@ -62,7 +57,6 @@ describe('FormSelect', () => { wrapper = mount( = ({ ...rest }: FormSelectProps) => { const { control } = useFormContext() - const { initialValues, loading } = useContext( - FieldContext - ) + const { loading } = useContext(FieldContext) rules.required = true - const defaultValue = (initialValues[name] as string) || '' - return (
{label && ( @@ -39,12 +35,10 @@ const FormSelect: FC = ({ )} (