From 42b3d0a9d808417677c15a93481b59af68fe51d3 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Tue, 26 Mar 2019 21:51:49 -0400 Subject: [PATCH] Updating Form to use hooks --- src/components/App/__snapshots__/test.js.snap | 18 +-- src/components/Form/index.js | 131 ++++++++---------- src/components/Form/test.js | 5 +- 3 files changed, 70 insertions(+), 84 deletions(-) diff --git a/src/components/App/__snapshots__/test.js.snap b/src/components/App/__snapshots__/test.js.snap index 6f8f900..5e067f6 100644 --- a/src/components/App/__snapshots__/test.js.snap +++ b/src/components/App/__snapshots__/test.js.snap @@ -3,7 +3,7 @@ exports[`App removing rendered expression 1`] = ` { + const { t } = useTranslation(); + const [ expr, exprUpdate ] = useState(props.expr); + const [ syntax, syntaxUpdate ] = useState(props.syntax); - state = { - expr: this.props.expr, - syntax: this.props.syntax - } - - handleSubmit = event => { + const handleExprChange = useCallback(event => { + exprUpdate(event.target.value); + }, [exprUpdate]); + const handleSyntaxChange = useCallback(event => { + syntaxUpdate(event.target.value); + }, [syntaxUpdate]); + const handleSubmit = useCallback(event => { event.preventDefault(); - const { expr, syntax } = this.state; - - this.props.onSubmit({ expr, syntax }); - } - - handleKeyPress = event => { + onSubmit({ expr, syntax }); + }, [expr, syntax, onSubmit]); + const handleKeyPress = useCallback(event => { if (event.charCode === 13 && event.shiftKey) { - this.handleSubmit(event); + handleSubmit(event); } - } + }, [handleSubmit]); - handleChange = event => this.setState({ - [event.target.name]: event.target.value - }) + return
+
+ + +
+ + +
+ { children } +
+
; +}; - render() { - const { - syntaxList, - children, - t - } = this.props; - const { expr, syntax } = this.state; +Form.propTypes = { + expr: PropTypes.string, + syntax: PropTypes.string, + syntaxList: PropTypes.arrayOf(PropTypes.shape({ + id: PropTypes.string, + label: PropTypes.string + })), + onSubmit: PropTypes.func.isRequired, + children: PropTypes.oneOfType([ + PropTypes.arrayOf(PropTypes.node), + PropTypes.node + ]) +}; - return
-
- - -
- - -
- { children } -
-
; - } -} - -export { Form }; -export default withTranslation()(Form); +export default Form; diff --git a/src/components/Form/test.js b/src/components/Form/test.js index 6f480dd..ca64cb2 100644 --- a/src/components/Form/test.js +++ b/src/components/Form/test.js @@ -5,14 +5,13 @@ jest.mock('react-feather/dist/icons/chevrons-down', () => import React from 'react'; import { render, fireEvent } from 'react-testing-library'; -import { mockT } from 'i18n'; -import { Form } from 'components/Form'; +import Form from 'components/Form'; const syntaxList = [ { id: 'testJS', label: 'Testing JS' }, { id: 'other', label: 'Other' } ]; -const commonProps = { syntaxList, t: mockT }; +const commonProps = { syntaxList }; describe('Form', () => { test('rendering', () => {