Skip to content

Conversation

@tompsota
Copy link
Contributor

@tompsota tompsota commented Jun 1, 2022

chore(FormSelect): convert examples to TypeScript

Signed-off-by: Tomas Psota tpsota@redhat.com

What: Closes #7475

@tompsota tompsota force-pushed the formSelect_to_typescript branch from 88eb5a5 to 8e92525 Compare June 1, 2022 09:14
@patternfly-build
Copy link
Collaborator

patternfly-build commented Jun 1, 2022

Copy link
Contributor

@jenny-s51 jenny-s51 left a comment

Choose a reason for hiding this comment

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

Awesome work on this @tompsota ! The examples are looking great. Left some comments below.

In a lot of cases, TS can infer the type of your hooks itself e.g. when you set the value to a string. You don't need the extra <string> type declaration for those!

Just a few easy fixes and otherwise LGTM 👍

@tompsota tompsota force-pushed the formSelect_to_typescript branch from 8e92525 to 1a10452 Compare June 2, 2022 08:03
Copy link
Contributor

@jenny-s51 jenny-s51 left a comment

Choose a reason for hiding this comment

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

Looks 👍👍!

Copy link
Contributor

@thatblindgeye thatblindgeye left a comment

Choose a reason for hiding this comment

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

Great job on the conversions! I also had a couple change requests below, let me know if you have any questions! 🙂

Comment on lines 8 to 11
const isEmpty = () => formSelectValue !== '';
const onChange = (value: string) => {
const validated = isEmpty() && ValidatedOptions.error;
setFormSelectValue(value);
setValidated(validated);
};
Copy link
Contributor

Choose a reason for hiding this comment

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

It doesn't look like this part of the example is working how it may have been intended when originally added.

Rather than trying to set both the validated and value states in the onChange function, it might work better to have the onChange function only call setFormSelectValue like other examples have, and moving the setValidated and checking if formSelectValue is an empty string to a useEffect hook that only runs when the formSelectValue updates. The isEmpty function would just also have to be updated to check that formSelectValue === ''

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this works better. Thanks for pointing it out.

I also renamed const validated variable so it doesn't collide with state variable.

@tompsota tompsota force-pushed the formSelect_to_typescript branch from 1a10452 to 9becd3a Compare June 3, 2022 10:20
};

React.useEffect(() => {
const validationState = !isEmpty() && ValidatedOptions.error;
Copy link
Contributor

Choose a reason for hiding this comment

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

This line just needs to be updated so that it sets the validated state correctly; right now it's having the opposite effect of a valid selection rendering the error icon in the example.

Suggested change
const validationState = !isEmpty() && ValidatedOptions.error;
const validationState = isEmpty() && ValidatedOptions.error;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay so iiuc, only Select a number option should be invalid? If this is the case, won't it be better to use e.g. 3 as the only valid option and add to corresponding FormSelectOptions label that this is the only valid option? Similarly as in Validated example.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay so iiuc, only Select a number option should be invalid?

That's what it seemed like the intention originally was, at least based on the variable being named isEmpty.

If this is the case, won't it be better to use e.g. 3 as the only valid option and add to corresponding FormSelectOptions label that this is the only valid option? Similarly as in Validated example.

I think either way could work really. While both Validated and Invalid examples are very similar, the main difference just seems to be that the Validate example shows various states of validation instead of only an error like the Invalid example does. As long as that remains true, reusing the onChange from the Validate example and tweaking it to not use helper text or simulate a network call could work (this would also make the useEffect unnecessary)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, so I kept the original example, it does what we need it to do so it's probably not necessary to make it more complicated.

@tompsota tompsota force-pushed the formSelect_to_typescript branch from 9becd3a to cd90249 Compare June 8, 2022 13:31
Copy link
Contributor

@thatblindgeye thatblindgeye left a comment

Choose a reason for hiding this comment

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

Looks good, thank you for working on this! 💪🏼

Copy link
Contributor

@tlabaj tlabaj left a comment

Choose a reason for hiding this comment

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

@mcarrano I think we can actually remove the "invalid" example. It is from before we hd the different "validated" states. I don't remember if we kept it for a reason.

@mcarrano
Copy link
Member

mcarrano commented Jun 9, 2022

@mcarrano I think we can actually remove the "invalid" example. It is from before we hd the different "validated" states. I don't remember if we kept it for a reason.

Sounds good to me.

Signed-off-by: Tomas Psota <tpsota@redhat.com>
@tompsota tompsota force-pushed the formSelect_to_typescript branch from cd90249 to a00495f Compare June 13, 2022 07:13
Copy link
Contributor

@tlabaj tlabaj left a comment

Choose a reason for hiding this comment

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

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Form select: convert examples to TypeScript

6 participants