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

fix: onchange for autocomplete in datafilters #2047

Merged
merged 15 commits into from
Feb 1, 2024
Merged

Conversation

benlister-okta
Copy link
Contributor

Fix to add onChange event for Autocomplete in DataFilters for DataTable.

Jira: OKTA-671006

Summary

  • Logic added for OnChange to trigger updates in the Autocomplete filter in DataFilters. This was previously placeholder/stub logic.
  • The filter now works like the Textfield and Number filters where the user makes a selection and click a primary button to add the filter.

Testing & Screenshots

N/A

Fix to add `onChange` event for Autocomplete in DataFilters for DataTable
] as string) ?? ""
}
onChange={(_, value) => {
const label =
Copy link
Contributor

Choose a reason for hiding this comment

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

@benlister-okta Can we pull this logic up into the handleInputChange?

Copy link
Contributor

Choose a reason for hiding this comment

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

Never mind, don't think we can :( How about making another function like... getAutoCompleteLabel or something like that? Generally, I just try to avoid having to much logic inside JSX

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be cleaner if we did for sure. I was exploring this today and the issue I run into is that due to the structure of Autocomplete's data which simulates a Select, the check to see if the data is an object or a string is needed here, but not in the global handleInputChange. I might be missing something, let's chat about this Monday!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bryancunningham-okta I pulled back my last change and went with your idea of moving the logic to a getAutoCompleteLabel function. Looking at the changes I had in handleInputChange, it felt like it was getting too complicated overall. Let me know what you think of this approach or if you think and could be further optimized.

Type checks in onChange for Autocomplete moved into handleInputChange
Created a new function called getAutoCompleteLabel to simplify the Autocomplete logic
@benlister-okta
Copy link
Contributor Author

@bryancunningham-okta @KevinGhadyani-Okta @jordankoschei-okta let me know if you have thoughts on this approach. I think abstracting the checks into the autocomplete-specific function will be cleaner than what I was trying to do previously.

): string => {
if (Array.isArray(value)) {
// Iterating to find the label
for (const valueElement of value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the value array ever more than one entry?

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 didn't explore this case but multi-select is an option for Autocomplete

Copy link
Contributor

Choose a reason for hiding this comment

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

So, it could theoretically be something like this?

[{label: 'some label'}, 'another label']

return valueElement.label;
}
}
} else if (
Copy link
Contributor

Choose a reason for hiding this comment

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

We could probably just do

} else if (value?.label) {...

@benlister-okta
Copy link
Contributor Author

benlister-okta commented Jan 19, 2024

Hey @bryancunningham-okta @jordankoschei-okta, @KevinGhadyani-Okta helped me refactor some of the logic for the Autocomplete filter's onChange and I think this is ready for another review.

Also, I just realized I still haven't gained access to Aplitools so I need help with the failed visual regression test (I believe it timed out).

value: undefined,
});

updateFilters({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Kevin and I also discussed splitting the logic for updating the filter value shown in the DataFilter UI and the actual value in the form components into 2 parts updateInputValue and updateFilters to separate concerns and make the code a little easier to read.

 unnecessary dependency was included
Fix to add `onChange` event for Autocomplete in DataFilters for DataTable
Type checks in onChange for Autocomplete moved into handleInputChange
Created a new function called getAutoCompleteLabel to simplify the Autocomplete logic
removes nested ternary and breaks out handleInputChange into 2 steps
 unnecessary dependency was included
useImperativeHandle(
inputFocusRef,
() => {
const element = ref.current;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why create a temp variable? I think inputRef.current?.focus() is fine. The .current will always exist, it just might be run depending on when it's run.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@KevinGhadyani-Okta the scope of my PR was only to add logic for autocomplete in DataFilters. I am not sure what the reason for using a temp variable here was originally, but because I am unfamiliar with this code, I am unsure how to test it. Do you think we should consider this change in a future PR since it adds scope?


const Radio = ({
inputFocusRef,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the plan that any bi-directional props (like blur() and focus() are gonna have separate ref props?

If not, we should instead make this more generic so consumers only have to worry about creating a single ref to pass.

I would rename this more generically such as inputRef.

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 am also not certain on this one, I believe the updates to Radio in the PR are from a library update, not related to new functionality.

Copy link
Contributor

@KevinGhadyani-Okta KevinGhadyani-Okta left a comment

Choose a reason for hiding this comment

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

Did a rough check. Are all these changes yours? Did you need to merge main?

@benlister-okta benlister-okta changed the base branch from main to ti-textfield-support-inputmode January 29, 2024 19:04
@benlister-okta benlister-okta changed the base branch from ti-textfield-support-inputmode to main January 29, 2024 19:04
Comment on lines 206 to 210
if (typeof valueElement !== "string") {
return valueElement.label;
}

return;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm surprised this doesn't throw a typescript error because we're not returning the same thing. Either we're returning a string or undefined.

I wonder if it would be better to say "if it's a string, then return undefined; otherwise, return valueElement.label". I say that because I prefer the positive over the negative with if statements:

Suggested change
if (typeof valueElement !== "string") {
return valueElement.label;
}
return;
if (typeof valueElement === "string") {
return undefined;
}
return valueElement.label;

Comment on lines 394 to 401
<Box
sx={{
display: "flex",
gap: 2,
alignItems: "flex-end",
}}
>
<Box sx={{ width: "100%" }}>
Copy link
Contributor

Choose a reason for hiding this comment

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

Were we going to add styled components in this PR or a followup?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for the reminder, PR is updated with this now.

value={
inputValues[filterPopoverCurrentFilter.id] ?? ""
}
onChange={(_, value) => {
Copy link
Contributor

@KevinGhadyani-Okta KevinGhadyani-Okta Jan 30, 2024

Choose a reason for hiding this comment

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

It's fine to keep this labeled as event even if we don't use it because if we do use it, the _ isn't very helpful:

Suggested change
onChange={(_, value) => {
onChange={(event, value) => {

If it's an ESLint error, don't worry about 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.

changing to event gives an ESLint error

Comment on lines 428 to 432
options={filterPopoverCurrentFilter.options.map(
(option: { label: string }) => ({
label: option.label,
})
)}
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to memoize this value.

Right now, we're creating options every time a re-render occurs which is anytime you click something in DataFilters, but I don't think the options ever change once this is rendered unless we're removing selected filters?

I thought there was something @jordankoschei-okta did recently about making it so selected items stay in the Autocomplete list.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@KevinGhadyani-Okta Are you open to pairing on this? It looks like we use memo elsewhere in this file but I am not familiar with how the library works.

@benlister-okta benlister-okta merged commit 34e9086 into main Feb 1, 2024
1 check passed
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.

3 participants