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

Enhance getInputProps to allow passing of non-overridden props #35034

Merged
merged 22 commits into from Oct 21, 2022

Conversation

joshuatf
Copy link
Contributor

@joshuatf joshuatf commented Oct 11, 2022

All Submissions:

Changes proposed in this Pull Request:

Previously, adding an onChange handler or anything else that collided with getInputProps would require recalling getInputProps.someFunction inside the new handler. This PR introduces a second argument to getInputProps which passes props to the control without overriding the original handlers where possible.

Some props will not override the original handler (e.g., if you pass on onChange it will be combined with the Form component's onChange) and some will override (e.g., value can not be combined and the consumer prop will override the original value).

This PR also uncovered numerous TS issues, so a getCheckboxProps was introduced into the Form component to omit invalid props (e.g., value for checkboxes). More specific prop getters are probably in order (getSelectProps), but I think we can iterate on these as we go.

Finally, a sanitize method was added as part of the optional consumer input props. This is a shortcut to sanitizing the input value and handling changes on blur.

TL;DR:

  • getInputProps now takes a second argument to combine/override props where necessary
  • sanitize method can be added to an input to handle sanitization on blur
  • getCheckboxProps was introduced for props specific to checkboxes

Closes #34996 .

How to test the changes in this Pull Request:

  1. Navigate to the new product page Products -> Add new (MVP)
  2. Check that styling for checkboxes is still correct
  3. Add values to the price and dimension fields, using thousand commas and decimal separators (depending your locale)
  4. Make sure they are sanitized on blur and persist correctly on save

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally?
  • Have you created a changelog file for each project being changed, ie pnpm --filter=<project> run changelog add?

FOR PR REVIEWER ONLY:

  • I have reviewed that everything is sanitized/escaped appropriately for any SQL or XSS injection possibilities. I made sure Linting is not ignored or disabled.

@joshuatf joshuatf requested a review from a team October 11, 2022 14:53
@joshuatf joshuatf self-assigned this Oct 11, 2022
@github-actions github-actions bot added focus: react admin package: @woocommerce/components issues related to @woocommerce/components plugin: woocommerce Issues related to the WooCommerce Core plugin. labels Oct 11, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Oct 11, 2022

Test Results Summary

Commit SHA: 6519e81

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests19600201980m 46s
E2E Tests186003018915m 52s

To view the full API test report, click here.
To view the full E2E test report, click here.
To view all test reports, visit the WooCommerce Test Reports Dashboard.

Copy link
Contributor Author

@joshuatf joshuatf left a comment

Choose a reason for hiding this comment

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

Just noting that we have more upcoming work (I also have an open PR) that will benefit from being able to pass in additional handlers without overriding the Form's handlers.

regularPriceProps?.onChange( sanitizedValue );
} }
value={ formatCurrencyDisplayValue( regularPriceProps?.value, currencyConfig, formatAmount ) }
onBlur={ () => setValue( 'regular_price', sanitizePrice( regularPriceProps.value ) ) }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@octaedro I moved the sanitization to onBlur. I think this is a little bit more conventional as I was worried about input being sanitized as it was typed could lead to odd behavior.

But please let me know if I broke anything here or if this needs to be sanitized as we type.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good to me! Good idea!

const currencyPosition = symbolPosition.includes( 'left' )
? 'prefix'
: 'suffix';

return {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

My goal was to unwrap some of these so that we can:

  1. More easily follow where props are being passed
  2. Reduce the knowledge of form handlers in this function
  3. Reduce overall logic in this function

@joshuatf joshuatf force-pushed the update/34996 branch 2 times, most recently from 6dd3ccc to 9298538 Compare October 13, 2022 21:10
@joshuatf
Copy link
Contributor Author

@octaedro Thanks for the ping on rebasing this one in Slack! After rebasing, a lot of TS issues cropped up that I think we were able to errantly bypass before.

I added a getCheckboxProps method to Form and also introduced a sanitize method as a shortcut for inputs. I updated the description above to reflect all the changes.

@mdperez86 since this also affects a lot of your recent additions around dimensions, it would be great to get your 👀 on this as well.

};
}

function getCheckboxProps< Value = Values[ keyof Values ] >(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should this be getCheckboxInputProps or getCheckboxControlProps instead? The reason I ask is we'll probably want to introduce a getSelectProps which sounds odd and could be interpreted the wrong way. Also okay leaving it as is, but wanted to get some feedback.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would lean towards getCheckboxControlProps. But I'm also ok leaving it as it is now.

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 that makes sense too since we're really creating props specifically for our controls 👍

Comment on lines +87 to +101
{ ...getInputProps( 'name', {
onBlur: setSkuIfEmpty,
} ) }
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

Copy link
Contributor

@octaedro octaedro left a comment

Choose a reason for hiding this comment

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

Nice job @joshuatf! This is testing well here and the code looks good.
There is a small visual error with the Feature this product checkbox, the rest looks great!

Screen Shot 2022-10-19 at 09 55 35

@joshuatf
Copy link
Contributor Author

There is a small visual error with the Feature this product checkbox, the rest looks great!

Good catch @octaedro! Updated in 38f80d8

Also gave this a rebase; could you take a look when you have time?

@github-actions github-actions bot added package: @woocommerce/data issues related to @woocommerce/data release: highlight Issues that have a high user impact and need to be discussed/paid attention to. labels Oct 19, 2022
octaedro
octaedro previously approved these changes Oct 20, 2022
Copy link
Contributor

@octaedro octaedro left a comment

Choose a reason for hiding this comment

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

Thank you @joshuatf for addressing the changes! The initialValues issue was a pretty tricky thing to deal with.
Good job! :shipit:

@github-actions github-actions bot removed the release: highlight Issues that have a high user impact and need to be discussed/paid attention to. label Oct 20, 2022
Copy link
Contributor

@octaedro octaedro left a comment

Choose a reason for hiding this comment

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

🚀

@joshuatf joshuatf merged commit 28f8e7f into trunk Oct 21, 2022
@joshuatf joshuatf deleted the update/34996 branch October 21, 2022 14:03
@github-actions github-actions bot added this to the 7.2.0 milestone Oct 21, 2022
@github-actions
Copy link
Contributor

Hi @joshuatf, thanks for merging this pull request. Please take a look at these follow-up tasks you may need to perform:

  • Add the release: add testing instructions label

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: @woocommerce/components issues related to @woocommerce/components package: @woocommerce/data issues related to @woocommerce/data plugin: woocommerce Issues related to the WooCommerce Core plugin.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Enhancement]: Form component input props should allow passing of custom callbacks
2 participants