Skip to content

Commit c501fd9

Browse files
author
Kubit
committed
Modify and improve aria builder utility
1 parent 5fe5ac7 commit c501fd9

File tree

3 files changed

+14
-60
lines changed

3 files changed

+14
-60
lines changed
Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,22 @@
11
import { InputState } from '../types';
2-
import { buildAriaLabelledBy } from '../utils';
2+
import { buildAriaDescribedBy } from '../utils';
33

4-
const extraAriaLabelledBy = '0000';
5-
const labelId = 'labelId';
64
const helpMessage = 'help';
75
const helpMessageId = '1234';
86
const errorMessage = 'error';
97
const errorMessageId = '56789';
108

119
describe('Utils aria', () => {
1210
it('If help message and error', () => {
13-
const extraAriaLabelledBy = undefined;
1411
const state = InputState.ERROR_EMPTY;
1512
expect(
16-
buildAriaLabelledBy({
17-
extraAriaLabelledBy,
18-
labelId,
13+
buildAriaDescribedBy({
1914
helpMessage,
2015
helpMessageId,
2116
errorMessage,
2217
errorMessageId,
2318
state,
2419
})
25-
).toBe('labelId 56789 1234');
26-
});
27-
it('If help message and extraAriaLabelledBy', () => {
28-
const state = InputState.EMPTY;
29-
expect(
30-
buildAriaLabelledBy({
31-
extraAriaLabelledBy,
32-
labelId,
33-
helpMessage,
34-
helpMessageId,
35-
errorMessage,
36-
errorMessageId,
37-
state,
38-
})
39-
).toBe('labelId 0000 1234');
20+
).toBe(' 1234 56789');
4021
});
4122
});

src/components/input/inputStandAlone.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ import {
3131
LABEL_TYPE,
3232
MultipleRef,
3333
} from './types';
34-
import { buildAriaDescribedBy, buildAriaLabelledBy, hasError, isDisabled } from './utils';
34+
import { buildAriaDescribedBy, hasError, isDisabled } from './utils';
3535

3636
const InputStandAloneComponent = (
3737
{
38-
extraAriaLabelledBy,
3938
styles,
4039
state,
4140
helpMessage,
@@ -76,16 +75,12 @@ const InputStandAloneComponent = (
7675
}
7776
aria-haspopup={props['aria-haspopup']}
7877
aria-invalid={hasError(state)}
79-
aria-labelledby={buildAriaLabelledBy({
80-
labelId,
81-
extraAriaLabelledBy,
82-
helpMessage: helpMessage?.content as string,
83-
helpMessageId,
84-
state: state,
85-
})}
78+
aria-labelledby={props.extraAriaLabelledBy}
8679
{...ariaProps}
8780
aria-describedby={buildAriaDescribedBy({
8881
ariaDescribedBy: props['aria-describedby'],
82+
helpMessage: helpMessage?.content as string,
83+
helpMessageId,
8984
errorMessage: errorMessage?.content as string,
9085
errorMessageId,
9186
state,

src/components/input/utils/aria.utils.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,31 @@
11
import { InputState } from '../types';
22
import { hasError } from './state.utils';
33

4-
export const buildAriaLabelledBy = ({
5-
extraAriaLabelledBy,
6-
labelId,
4+
export const buildAriaDescribedBy = ({
5+
ariaDescribedBy,
76
helpMessage,
87
helpMessageId,
98
errorMessage,
109
errorMessageId,
1110
state,
1211
}: {
13-
labelId: string;
14-
extraAriaLabelledBy?: string;
12+
ariaDescribedBy?: string;
1513
helpMessage?: React.ReactNode;
1614
helpMessageId?: string;
1715
errorMessage?: string;
1816
errorMessageId?: string;
1917
state?: InputState;
20-
}): string => {
21-
let res = labelId;
22-
if (extraAriaLabelledBy) {
23-
res += ` ${extraAriaLabelledBy}`;
24-
}
25-
if (errorMessageId && errorMessage && hasError(state)) {
26-
res += ` ${errorMessageId}`;
27-
}
28-
if (helpMessageId && helpMessage) {
29-
res += ` ${helpMessageId}`;
30-
}
31-
return res;
32-
};
33-
34-
export const buildAriaDescribedBy = ({
35-
ariaDescribedBy,
36-
errorMessage,
37-
errorMessageId,
38-
state,
39-
}: {
40-
ariaDescribedBy?: string;
41-
errorMessage?: string;
42-
errorMessageId?: string;
43-
state?: InputState;
4418
}): string | undefined => {
4519
if (!ariaDescribedBy && !errorMessage) return;
4620

4721
let res: string = '';
4822
if (ariaDescribedBy) {
4923
res += ` ${ariaDescribedBy}`;
5024
}
25+
if (helpMessageId && helpMessage) {
26+
res += ` ${helpMessageId}`;
27+
}
28+
5129
if (errorMessageId && errorMessage && hasError(state)) {
5230
res += ` ${errorMessageId}`;
5331
}

0 commit comments

Comments
 (0)