Skip to content

Commit

Permalink
adding id prop for label and hint tag (#448)
Browse files Browse the repository at this point in the history
* adding id prop for label and hint tag

* updating documentation for new properties added
  • Loading branch information
arpakuma authored Mar 23, 2023
1 parent e0eebf9 commit a511277
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/autocomplete/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ type autocompleteProps = {
* you can style the look and feel of your hint text
*/
hintClass?: string;
/**
* if you want to pass an id to the hint
*/
hintId?: string;
/**
* if you want to pass an id to the result UL list
*/
Expand Down Expand Up @@ -154,6 +158,10 @@ type autocompleteProps = {
* if a label is provided, it will provide the ability to style it
*/
labelClassName?: string;
/**
* allow to customise the label with all the properties needed
**/
labelProps?: React.LabelHTMLAttributes<HTMLLabelElement>;
/**
* it will pass an id to the input or select element(in case of progressive enhancement)
*/
Expand Down Expand Up @@ -231,6 +239,7 @@ export const Autocomplete = ({
defaultValue = '',
hintText,
hintClass,
hintId,
multiSelect = false,
notFoundText,
resultId,
Expand All @@ -253,6 +262,7 @@ export const Autocomplete = ({
containerClassName,
labelText,
labelClassName,
labelProps,
id,
errorPosition,
errorMessageText = '',
Expand Down Expand Up @@ -492,7 +502,7 @@ export const Autocomplete = ({
/>
)}
{labelText && (
<label htmlFor={id} className={labelClassName}>
<label htmlFor={id} className={labelClassName} {...labelProps}>
{labelText}
</label>
)}
Expand All @@ -506,7 +516,7 @@ export const Autocomplete = ({
/>
)}
{hintText && (
<Hint text={hintText} className={hintClass} useLabel={false} />
<Hint text={hintText} className={hintClass} id={hintId} useLabel={false} />
)}
{errorPosition &&
errorPosition === AutoCompleteErrorPosition.AFTER_HINT && (
Expand Down Expand Up @@ -534,7 +544,7 @@ export const Autocomplete = ({
return (
<>
{multiSelect && hintText && (
<Hint text={hintText} className={hintClass} useLabel={false} />
<Hint text={hintText} className={hintClass} id={hintId} useLabel={false} />
)}
<div className={containerClassName} style={{ ...searchContainerStyle }}>
{searchEl}
Expand Down
26 changes: 26 additions & 0 deletions src/autocomplete/__tests__/Autocomplete.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ describe('Autocomplete', () => {
expect(label).toBeInTheDocument();
});

it('should allow to specify a label id', () => {
render(
<Autocomplete
options={['abc', 'xyz']}
labelText="labelText"
labelClassName="labelClass"
labelProps={{id:'labelid'}}
/>
);
const label: any = screen.getByText('labelText');
expect(label.id).toBe('labelid');
});

it('should display an error', () => {
render(
<Autocomplete
Expand Down Expand Up @@ -424,6 +437,19 @@ describe('Autocomplete', () => {
expect(hint).not.toBeNull();
});

it('should display an hint id if specified', () => {
render(
<Autocomplete
options={['abc', 'xyz']}
hintText="search names"
hintClass="hintclass"
hintId="hintid"
/>
);
const hint: any = screen.getByText('search names');
expect(hint.id).toBe('hintid');
});

it('should display the results after typing 2 character', async () => {
const user = userEvent.setup();
const { container } = render(
Expand Down
2 changes: 2 additions & 0 deletions stories/Autocomplete/Documentation.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ An example with all the available properties is (the only mandatory property is
debounceMs={2000}
hintText="search the list of fruits"
hintClass="hintClass"
hintId="hintId"
resultUlClass="resultUlClass"
resultlLiClass="resultlLiClass"
resultNoOptionClass="resultNoOptionClass"
Expand All @@ -52,6 +53,7 @@ An example with all the available properties is (the only mandatory property is
containerClassName=""
labelText=""
labelClassName=""
labelProps={{id:'labelid'}}
id=""
errorPosition={AutoCompleteErrorPosition.TOP}
errorMessageText=""
Expand Down
2 changes: 2 additions & 0 deletions stories/liveEdit/AutocompleteLive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function AutocompleteDemo() {
debounceMs={100}
hintText="search the list of fruits"
hintClass=""
hintId="hintid"
prefix={<></>}
suffix={<></>}
resultUlClass=""
Expand All @@ -48,6 +49,7 @@ function AutocompleteDemo() {
containerClassName=""
labelText=""
labelClassName=""
labelProps={{id:'labelid'}}
id=""
errorPosition='below'
errorMessageText=""
Expand Down

0 comments on commit a511277

Please # to comment.