Skip to content

Commit

Permalink
MBMS-68435 change military history section flow pattern for pre-need …
Browse files Browse the repository at this point in the history
…integration form
  • Loading branch information
adam-antonioli committed Nov 27, 2024
1 parent 6eb9f21 commit 22c7138
Showing 1 changed file with 45 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect, useState, useMemo } from 'react';

Check warning on line 1 in src/applications/pre-need-integration/components/MilitaryAutoSuggestField.jsx

View workflow job for this annotation

GitHub Actions / App Isolation Annotations

Staged Continuous Deployment App Isolation Conflict

*WARNING* This PR contains changes related to an application that is currently not isolated. As of Feb 3, 2025 deployment may no longer be possible for apps that are not isolated. Please isolate this app from other directories in 'src/applications' to prevent future deployment issues. More information on your app's status can be seen here: https://department-of-veterans-affairs.github.io/veteran-facing-services-tools/frontend-support-dashboard/cross-app-import-report Please reach out to Frontend Platform Support with any questions.
import Downshift from 'downshift';

function MilitaryAutoSuggest({
Expand All @@ -12,20 +12,21 @@ function MilitaryAutoSuggest({

useEffect(
() => {
if (value) {
if (value && value !== inputValue) {
setInputValue(value);
setValue(value);
}
},
[value],
);

const itemToString = item => (item ? item.value || item.key : inputValue);
const itemToString = item => (item ? `${item.key} - ${item.value}` : '');

const getMatchScore = (input, labelValue) => {
if (!input || !labelValue) return 0;
const normalizedInput = input.toLowerCase();
const label = `${labelValue.key} - ${labelValue.value}`;
const normalizedLabel = label ? label.toLowerCase() : '';
const normalizedLabel = label.toLowerCase();
let score = normalizedLabel.startsWith(normalizedInput) ? 10000 : 0;

normalizedInput.split(/\s+/).forEach(inputWord => {
Expand All @@ -37,60 +38,67 @@ function MilitaryAutoSuggest({
return score;
};

const filteredLabels = useMemo(
() => {
return labels
.map(label => ({
label,
score: getMatchScore(inputValue, label),
}))
.filter(item => item.score > 0)
.sort((a, b) => b.score - a.score)
.slice(0, maxItems);
},
[labels, inputValue, maxItems],
);

return (
<Downshift
onChange={selection => {
setValue(`${selection.key.toUpperCase()} - ${selection.value}`);
onSelectionChange(selection);
if (selection) {
const formattedValue = `${selection.key.toUpperCase()} - ${
selection.value
}`;
setInputValue(formattedValue);
setValue(formattedValue);
onSelectionChange(selection);
}
}}
itemToString={itemToString}
selectedItem={value}
inputValue={inputValue}
onInputValueChange={setInputValue}
>
{({ getInputProps, getItemProps, isOpen, highlightedIndex }) => (
<div className="relative">
{/* Add label with htmlFor */}
{/* <label htmlFor="military-auto-suggest"> */}
{/* Select a military rank: */}
<input
id="military-auto-suggest"
type="text"
{...getInputProps({
value,
onChange: e => {
setInputValue(e.target.value);
onSelectionChange(e.target.value);
},
onBlur: () => {
setValue(inputValue);
},
})}
/>
{/* </label> */}
{isOpen && (
<div className="autosuggest-list">
{labels
.map((label, index) => ({
label,
index,
score: getMatchScore(inputValue, label),
}))
.filter(item => item.score > 0)
.sort((a, b) => b.score - a.score)
.slice(0, maxItems)
.map((item, index) => (
<div
key={item.index}
{...getItemProps({
item: item.label,
index,
className:
highlightedIndex === index
? 'autosuggest-item-highlighted'
: 'autosuggest-item',
})}
>
{`${item.label.key} - ${item.label.value}`}
</div>
))}
{filteredLabels.map((item, index) => (
<div
key={index}
{...getItemProps({
item: item.label,
index,
className:
highlightedIndex === index
? 'autosuggest-item-highlighted'
: 'autosuggest-item',
})}
>
{`${item.label.key} - ${item.label.value}`}
</div>
))}
</div>
)}
</div>
Expand Down

0 comments on commit 22c7138

Please sign in to comment.