Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor AddressSearch to be compatible with Form #7701

Merged
merged 14 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions src/components/AddressSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,34 @@ import styles from '../styles/styles';
import TextInput from './TextInput';
import Log from '../libs/Log';
import * as GooglePlacesUtils from '../libs/GooglePlacesUtils';
import * as FormUtils from '../libs/FormUtils';

// The error that's being thrown below will be ignored until we fork the
// react-native-google-places-autocomplete repo and replace the
// VirtualizedList component with a VirtualizedList-backed instead
LogBox.ignoreLogs(['VirtualizedLists should never be nested']);

const propTypes = {
/** Indicates that the input is being used with the Form component */
isFormInput: PropTypes.bool,

/**
* The ID used to uniquely identify the input
*
* @param {Object} props - props passed to the input
* @returns {Object} - returns an Error object if isFormInput is supplied but inputID is falsey or not a string
*/
inputID: props => FormUtils.getInputIDPropTypes(props),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We renamed this util function to validateInputIDProps. Please update it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


/** Saves a draft of the input value when used in a form */
shouldSaveDraft: PropTypes.bool,

/** Callback that is called when the text input is blurred */
onBlur: PropTypes.func,

/** Error text to display */
errorText: PropTypes.string,

/** The label to display for the field */
label: PropTypes.string.isRequired,

Expand All @@ -32,6 +53,12 @@ const propTypes = {
};

const defaultProps = {
// ...defaultFieldPropTypes,
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
isFormInput: false,
inputID: undefined,
shouldSaveDraft: false,
onBlur: () => {},
errorText: '',
value: '',
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
containerStyles: [],
};
Expand Down Expand Up @@ -111,11 +138,28 @@ const AddressSearch = (props) => {
}}
textInputProps={{
InputComp: TextInput,
ref: (node) => {
if (!props.innerRef) {
return;
}

luacmartins marked this conversation as resolved.
Show resolved Hide resolved
if (_.isFunction(props.innerRef)) {
props.innerRef(node);
return;
}

// eslint-disable-next-line no-param-reassign
props.innerRef.current = node;
},
label: props.label,
containerStyles: props.containerStyles,
errorText: props.errorText,
value: props.value,
onChangeText: (text) => {
isFormInput: props.isFormInput,
inputID: props.inputID,
shouldSaveDraft: props.shouldSaveDraft,
onBlur: props.onBlur,
onChange: (text) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm wrong, but I believe that onChanteText passes the text value as argument vs onChange passes an event. We should make sure that we are passing the text value here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it returns only text value, considering AddressSearch is using TextInput as the base component.

onChangeText={this.setValue}

setValue(value) {
if (this.props.onChange) {
this.props.onChange(value);
}
this.value = value;
Str.result(this.props.onChangeText, value);
this.activateLabel();
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm that is onChangeText though, not onChange. I tried logging the text value here and I see the following:

Screen Shot 2022-02-15 at 1 57 32 PM

Is that what we expect?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so.
Should I push this diff?

diff --git a/src/components/AddressSearch.js b/src/components/AddressSearch.js
index 005438f75..bee2e40cb 100644
--- a/src/components/AddressSearch.js
+++ b/src/components/AddressSearch.js
@@ -158,7 +158,7 @@ const AddressSearch = (props) => {
                 inputID: props.inputID,
                 shouldSaveDraft: props.shouldSaveDraft,
                 onBlur: props.onBlur,
-                onChange: (text) => {
+                onChangeText: (text) => {
                     if (skippedFirstOnChangeTextRef.current) {
                         props.onChange({street: text});
                     } else {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kakajann Sorry for the late reply, I was ooo for most of last week. I think it's fine to keep onChangeText as we had before. The onChange handler will be passed by the parent Form component.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@luacmartins, done

if (skippedFirstOnChangeTextRef.current) {
props.onChange({street: text});
} else {
Expand Down Expand Up @@ -160,4 +204,7 @@ const AddressSearch = (props) => {
AddressSearch.propTypes = propTypes;
AddressSearch.defaultProps = defaultProps;

export default withLocalize(AddressSearch);
export default withLocalize(React.forwardRef((props, ref) => (
// eslint-disable-next-line react/jsx-props-no-spreading
<AddressSearch {...props} innerRef={ref} />
)));
33 changes: 33 additions & 0 deletions src/stories/AddressSearch.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import AddressSearch from '../components/AddressSearch';

/**
* We use the Component Story Format for writing stories. Follow the docs here:
*
* https://storybook.js.org/docs/react/writing-stories/introduction#component-story-format
*/
export default {
title: 'Components/AddressSearch',
component: AddressSearch,
args: {
label: 'Enter street',
errorText: '',
},
};

// eslint-disable-next-line react/jsx-props-no-spreading
const Template = args => <AddressSearch {...args} />;

// Arguments can be passed to the component by binding
// See: https://storybook.js.org/docs/react/writing-stories/introduction#using-args
const Default = Template.bind({});

const ErrorStory = Template.bind({});
ErrorStory.args = {
errorText: 'The street you looking for is not exist',
kakajann marked this conversation as resolved.
Show resolved Hide resolved
};

export {
Default,
ErrorStory,
};
9 changes: 8 additions & 1 deletion src/stories/Form.stories.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import {View} from 'react-native';
import TextInput from '../components/TextInput';
import AddressSearch from '../components/AddressSearch';
import Form from '../components/Form';
import * as FormActions from '../libs/actions/FormActions';
import styles from '../styles/styles';
Expand All @@ -13,7 +14,7 @@ import styles from '../styles/styles';
const story = {
title: 'Components/Form',
component: Form,
subcomponents: {TextInput},
subcomponents: {TextInput, AddressSearch},
};

const Template = (args) => {
Expand All @@ -39,6 +40,12 @@ const Template = (args) => {
containerStyles={[styles.mt4]}
isFormInput
/>
<AddressSearch
label="Street"
inputID="street"
containerStyles={[styles.mt4]}
isFormInput
/>
</Form>
);
};
Expand Down