From d32e6d01dc9c9458f2456ace6ebb8da173a43e88 Mon Sep 17 00:00:00 2001
From: Brian Smith <112954497+brian-smith-tcril@users.noreply.github.com>
Date: Tue, 19 Dec 2023 16:29:42 -0500
Subject: [PATCH] fix: associate labels with autosuggest input (#2755)
---
src/Form/FormAutosuggest.jsx | 16 ++-
src/Form/form-autosuggest.mdx | 152 +++++++++++++-----------
src/Form/tests/FormAutosuggest.test.jsx | 21 ++++
3 files changed, 112 insertions(+), 77 deletions(-)
diff --git a/src/Form/FormAutosuggest.jsx b/src/Form/FormAutosuggest.jsx
index a2ef755281..0e255d5553 100644
--- a/src/Form/FormAutosuggest.jsx
+++ b/src/Form/FormAutosuggest.jsx
@@ -6,7 +6,7 @@ import { v4 as uuidv4 } from 'uuid';
import { useIntl } from 'react-intl';
import { KeyboardArrowUp, KeyboardArrowDown } from '../../icons';
import Icon from '../Icon';
-import FormGroup from './FormGroup';
+import { FormGroupContextProvider, useFormGroupContext } from './FormGroupContext';
import FormControl from './FormControl';
import FormControlFeedback from './FormControlFeedback';
import IconButton from '../IconButton';
@@ -239,12 +239,18 @@ function FormAutosuggest({
setDisplayValue(e.target.value);
};
+ const { getControlProps } = useFormGroupContext();
+ const controlProps = getControlProps(props);
+
return (
{`${state.dropDownItems.length} options found`}
-
+
0).toString()}
@@ -259,7 +265,7 @@ function FormAutosuggest({
onClick={handleClick}
trailingElement={iconToggle}
data-testid="autosuggest-textbox-input"
- {...props}
+ {...controlProps}
/>
{helpMessage && !state.errorMessage && (
@@ -269,11 +275,11 @@ function FormAutosuggest({
)}
{state.errorMessage && (
-
+
{errorMessageText}
)}
-
+
setSelected(value)}
- >
- JavaScript
- Python
- Rube
- alert(e.currentTarget.getAttribute('data-value'))}>
- Option with custom onClick
-
-
- );
-}
+ return (
+
+
+ Programming language
+
+ setSelected(value)}
+ >
+ JavaScript
+ Python
+ Rube
+ alert(e.currentTarget.getAttribute('data-value'))}>
+ Option with custom onClick
+
+
+
+ );
+};
```
## Search Usage
```jsx live
() => {
- const [selected, setSelected] = useState('');
+ const [selected, setSelected] = useState('');
- return (
-
setSelected(value)}
- >
- PHP
- Java
- Turbo Pascal
- Flask
-
- );
-}
+ return (
+
setSelected(value)}
+ >
+ PHP
+ Java
+ Turbo Pascal
+ Flask
+
+ );
+};
```
## Loading state
```jsx live
() => {
- const [data, setData] = useState([]);
- const [showLoading, setShowLoading] = useState(false);
+ const [data, setData] = useState([]);
+ const [showLoading, setShowLoading] = useState(false);
- useEffect(() => {
- setShowLoading(true);
- fetch('https://api.sampleapis.com/coffee/hot')
- .then(data => data.json())
- .then(items => {
- setTimeout(() => {
- setData(items);
- setShowLoading(false);
- }, 1500);
- });
- }, [])
+ useEffect(() => {
+ setShowLoading(true);
+ fetch('https://api.sampleapis.com/coffee/hot')
+ .then(data => data.json())
+ .then(items => {
+ setTimeout(() => {
+ setData(items);
+ setShowLoading(false);
+ }, 1500);
+ });
+ }, []);
- const searchCoffee = (title) => {
- setShowLoading(true);
- fetch('https://api.sampleapis.com/coffee/hot')
- .then(data => data.json())
- .then(items => setTimeout(() => {
- const filteredCoffee = items.filter(res => res.title.toLowerCase().includes(title.toLowerCase()));
- setShowLoading(false);
- if (filteredCoffee) { return filteredCoffee }
- return { ...title, filteredCoffee }
- }, 1500));
- };
+ const searchCoffee = (title) => {
+ setShowLoading(true);
+ fetch('https://api.sampleapis.com/coffee/hot')
+ .then(data => data.json())
+ .then(items => setTimeout(() => {
+ const filteredCoffee = items.filter(res => res.title.toLowerCase().includes(title.toLowerCase()));
+ setShowLoading(false);
+ if (filteredCoffee) { return filteredCoffee; }
+ return { ...title, filteredCoffee };
+ }, 1500));
+ };
- return (
-
- {data.map((item, index) => {item.title})}
-
- );
-}
+ return (
+
+
+ Café API
+
+
+ {data.map((item, index) => {item.title})}
+
+
+ );
+};
```
diff --git a/src/Form/tests/FormAutosuggest.test.jsx b/src/Form/tests/FormAutosuggest.test.jsx
index 9f4714dcfe..dce226f71a 100644
--- a/src/Form/tests/FormAutosuggest.test.jsx
+++ b/src/Form/tests/FormAutosuggest.test.jsx
@@ -6,6 +6,8 @@ import userEvent from '@testing-library/user-event';
import { IntlProvider } from 'react-intl';
import FormAutosuggest from '../FormAutosuggest';
import FormAutosuggestOption from '../FormAutosuggestOption';
+import FormGroup from '../FormGroup';
+import FormLabel from '../FormLabel';
function FormAutosuggestWrapper(props) {
return (
@@ -31,6 +33,19 @@ function FormAutosuggestTestComponent(props) {
);
}
+function FormAutosuggestLabelTestComponent() {
+ return (
+
+
+ Label
+
+
+ Option
+
+
+ );
+}
+
FormAutosuggestTestComponent.defaultProps = {
onSelected: jest.fn(),
onClick: jest.fn(),
@@ -112,6 +127,12 @@ describe('render behavior', () => {
expect(getByText('3 options found')).toBeInTheDocument();
});
+
+ it('associates labels with the input textbox', () => {
+ const { getByTestId } = render(
);
+
+ expect(getByTestId('autosuggest-label').getAttribute('for')).toEqual(getByTestId('autosuggest-textbox-input').getAttribute('id'));
+ });
});
describe('controlled behavior', () => {