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 form component #27025

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
593df79
Prepare POC
kowczarz Aug 25, 2023
32beaaf
POC of Form using custom input components
kowczarz Sep 5, 2023
51b1ab3
Cleanup input wrapper
kowczarz Sep 7, 2023
620b48c
Remove redundant testing component
kowczarz Sep 7, 2023
1296a97
Cleanup form wrapper
kowczarz Sep 7, 2023
9a07c15
Merge remote-tracking branch 'expensify/main' into refactor-form-comp…
kowczarz Sep 7, 2023
59652ce
Cleanup DisplayNamePage
kowczarz Sep 7, 2023
0491bc3
Cleanup FormProvider
kowczarz Sep 8, 2023
5a379f0
Merge remote-tracking branch 'expensify/main' into refactor-form-comp…
kowczarz Sep 8, 2023
74f69a5
Fix lint
kowczarz Sep 8, 2023
b6c8a62
Cleanup
kowczarz Sep 11, 2023
43d8080
Apply suggestions from code review
kowczarz Sep 13, 2023
e186a32
Merge remote-tracking branch 'expensify/main' into refactor-form-comp…
kowczarz Sep 13, 2023
01a455f
Code review changes
kowczarz Sep 13, 2023
9f28189
Code review changes
kowczarz Sep 13, 2023
7ee8ee3
Code style fixes
kowczarz Sep 14, 2023
b82c26d
Merge remote-tracking branch 'expensify/main' into refactor-form-comp…
kowczarz Sep 14, 2023
858e8fc
Fix crashing issues
kowczarz Sep 18, 2023
416b0bd
Merge remote-tracking branch 'expensify/main' into refactor-form-comp…
kowczarz Sep 20, 2023
106afb7
Fix PropTypes errors
kowczarz Sep 20, 2023
ce80fef
Form performance optimisation
kowczarz Sep 21, 2023
1592692
Merge remote-tracking branch 'expensify/main' into refactor-form-comp…
kowczarz Sep 21, 2023
6cba7be
Cleanup
kowczarz Sep 21, 2023
ff8653f
Fix wrong default props
kowczarz Sep 21, 2023
073af5d
Fix prettier
kowczarz Sep 21, 2023
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
30 changes: 17 additions & 13 deletions src/hooks/useForm/FormProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as FormActions from '../../libs/actions/FormActions';
import FormContext from './FormContext';
import FormWrapper from './FormWrapper';
import compose from '../../libs/compose';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import {withNetwork} from '../../components/OnyxProvider';
import stylePropTypes from '../../styles/stylePropTypes';
import networkPropTypes from '../../components/networkPropTypes';
Expand Down Expand Up @@ -66,7 +65,9 @@ const propTypes = {
/** Information about the network */
network: networkPropTypes.isRequired,

...withLocalizePropTypes,
shouldValidateOnBlur: PropTypes.bool,

shouldValidateOnChange: PropTypes.bool,
};

const defaultProps = {
Expand All @@ -79,7 +80,9 @@ const defaultProps = {
scrollContextEnabled: false,
footerContent: null,
style: [],
validate: () => ({}),
validate: () => {},
shouldValidateOnBlur: false,
shouldValidateOnChange: false,
};

function getInitialValueByType(valueType) {
Expand All @@ -99,7 +102,7 @@ function FormProvider({validate, shouldValidateOnBlur, shouldValidateOnChange, c
const inputRefs = useRef({});
const touchedInputs = useRef({});
const [inputValues, setInputValues] = useState({});
const [errors, setErrors] = useState([]);
const [errors, setErrors] = useState({});

const onValidate = useCallback(
(values) => {
Expand Down Expand Up @@ -137,7 +140,7 @@ function FormProvider({validate, shouldValidateOnBlur, shouldValidateOnChange, c
if (network.isOffline && !enabledWhenOffline) {
return;
}
// Call submit handler

onSubmit(inputValues);
}, [enabledWhenOffline, formState.isLoading, inputValues, network.isOffline, onSubmit, onValidate]);

Expand All @@ -146,18 +149,18 @@ function FormProvider({validate, shouldValidateOnBlur, shouldValidateOnChange, c
const newRef = propsToParse.ref || createRef();
inputRefs[inputID] = newRef;

// We want to initialize the input value if it's undefined
if (_.isUndefined(inputValues[inputID])) {
inputValues[inputID] = propsToParse.defaultValue || getInitialValueByType(propsToParse.valueType);
if (!_.isUndefined(propsToParse.value)) {
inputValues[inputID] = propsToParse.value;
}

// We force the form to set the input value from the defaultValue props if there is a saved valid value
if (propsToParse.shouldUseDefaultValue) {
else if (propsToParse.shouldUseDefaultValue) {
kowczarz marked this conversation as resolved.
Show resolved Hide resolved
inputValues[inputID] = propsToParse.defaultValue;
}

if (!_.isUndefined(propsToParse.value)) {
inputValues[inputID] = propsToParse.value;
// We want to initialize the input value if it's undefined
else if (_.isUndefined(inputValues[inputID])) {
kowczarz marked this conversation as resolved.
Show resolved Hide resolved
inputValues[inputID] = _.isUndefined(propsToParse.defaultValue) ? getInitialValueByType(propsToParse.valueType) : propsToParse.defaultValue;
}

const errorFields = lodashGet(formState, 'errorFields', {});
Expand Down Expand Up @@ -190,7 +193,7 @@ function FormProvider({validate, shouldValidateOnBlur, shouldValidateOnChange, c
// Only run validation when user proactively blurs the input.
if (Visibility.isVisible() && Visibility.hasFocus()) {
// We delay the validation in order to prevent Checkbox loss of focus when
// the user are focusing a TextInput and proceeds to toggle a CheckBox in
// the user is focusing a TextInput and proceeds to toggle a CheckBox in
// web and mobile web platforms.
setTimeout(() => {
setTouchedInput(inputID);
Expand Down Expand Up @@ -237,6 +240,8 @@ function FormProvider({validate, shouldValidateOnBlur, shouldValidateOnChange, c
<FormWrapper
{...rest}
onSubmit={submit}
inputRefs={inputRefs}
errors={errors}
>
{children}
</FormWrapper>
Expand All @@ -249,7 +254,6 @@ FormProvider.propTypes = propTypes;
FormProvider.defaultProps = defaultProps;

export default compose(
withLocalize,
withNetwork(),
withOnyx({
formState: {
Expand Down
19 changes: 8 additions & 11 deletions src/hooks/useForm/FormWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import SafeAreaConsumer from '../../components/SafeAreaConsumer';
import ScrollViewWithContext from '../../components/ScrollViewWithContext';

import stylePropTypes from '../../styles/stylePropTypes';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import compose from '../../libs/compose';

const propTypes = {
/** A unique Onyx key identifying the form */
Expand Down Expand Up @@ -61,7 +59,9 @@ const propTypes = {
/** Custom content to display in the footer after submit button */
footerContent: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),

...withLocalizePropTypes,
errors: PropTypes.objectOf(PropTypes.string).isRequired,

inputRefs: PropTypes.objectOf(PropTypes.oneOfType([PropTypes.func, PropTypes.shape({current: PropTypes.instanceOf(Element)})])).isRequired,
};

const defaultProps = {
Expand Down Expand Up @@ -187,11 +187,8 @@ FormWrapper.displayName = 'FormWrapper';
FormWrapper.propTypes = propTypes;
FormWrapper.defaultProps = defaultProps;

export default compose(
withLocalize,
withOnyx({
formState: {
key: (props) => props.formID,
},
}),
)(FormWrapper);
export default withOnyx({
formState: {
key: (props) => props.formID,
},
})(FormWrapper);
2 changes: 1 addition & 1 deletion src/hooks/useForm/InputWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import FormContext from './FormContext';

const propTypes = {
RenderInput: PropTypes.node.isRequired,
RenderInput: PropTypes.oneOfType([PropTypes.func, PropTypes.node]).isRequired,
inputID: PropTypes.string.isRequired,
valueType: PropTypes.string,
forwardedRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({current: PropTypes.instanceOf(React.Component)})]),
Expand Down
Loading