-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(web-react): Implement new HelperText component #DS-886
- Changed utility class helper text to a HelperText components - Connect HelperText with its input using aria-describedby - Implement deprecation of not required id prop for FieldGroup, Checkbox, Radio, Select and TextArea
- Loading branch information
1 parent
f978b11
commit 7302bfd
Showing
14 changed files
with
247 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/web-react/src/components/Field/__tests__/useAriaDescribedBy.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import useAriaDescribedBy from '../useAriaDescribedBy'; | ||
|
||
describe('useAriaDescribedBy', () => { | ||
it('should return joinedAriaDescribedBy with validationTextId and helperTextId', () => { | ||
const props = { | ||
id: 'test', | ||
helperText: 'Helper text', | ||
validationText: 'Validation text', | ||
ariaDescribedBy: 'aria-describedby', | ||
}; | ||
|
||
const result = useAriaDescribedBy(props); | ||
|
||
expect(result.validationTextId).toBe('test__validationText'); | ||
expect(result.helperTextId).toBe('test__helperText'); | ||
expect(result.joinedAriaDescribedBy).toBe('aria-describedby test__helperText test__validationText'); | ||
}); | ||
|
||
it('should return joinedAriaDescribedBy without validationTextId and helperTextId if id is missing', () => { | ||
const props = { | ||
helperText: 'Helper text', | ||
validationText: 'Validation text', | ||
ariaDescribedBy: 'aria-describedby', | ||
}; | ||
|
||
const result = useAriaDescribedBy(props); | ||
|
||
expect(result.validationTextId).toBeUndefined(); | ||
expect(result.helperTextId).toBeUndefined(); | ||
expect(result.joinedAriaDescribedBy).toBe('aria-describedby'); | ||
}); | ||
|
||
it('should return joinedAriaDescribedBy without validationTextId and helperTextId if they are missing', () => { | ||
const props = { | ||
id: 'test', | ||
ariaDescribedBy: 'aria-describedby', | ||
}; | ||
|
||
const result = useAriaDescribedBy(props); | ||
|
||
expect(result.validationTextId).toBeUndefined(); | ||
expect(result.helperTextId).toBeUndefined(); | ||
expect(result.joinedAriaDescribedBy).toBe('aria-describedby'); | ||
}); | ||
|
||
it('should return joinedAriaDescribedBy without validationTextId and helperTextId if they are empty', () => { | ||
const props = { | ||
id: 'test', | ||
helperText: '', | ||
validationText: '', | ||
ariaDescribedBy: 'aria-describedby', | ||
}; | ||
|
||
const result = useAriaDescribedBy(props); | ||
|
||
expect(result.validationTextId).toBeUndefined(); | ||
expect(result.helperTextId).toBeUndefined(); | ||
expect(result.joinedAriaDescribedBy).toBe('aria-describedby'); | ||
}); | ||
|
||
it('should return joinedAriaDescribedBy and helperTextId without validationTextId', () => { | ||
const props = { | ||
id: 'test', | ||
helperText: 'Helper Text', | ||
validationText: undefined, | ||
ariaDescribedBy: 'aria-describedby', | ||
}; | ||
|
||
const result = useAriaDescribedBy(props); | ||
|
||
expect(result.validationTextId).toBeUndefined(); | ||
expect(result.helperTextId).toBe('test__helperText'); | ||
expect(result.joinedAriaDescribedBy).toBe('aria-describedby test__helperText'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './ValidationText'; | ||
export * from './HelperText'; | ||
export * from './useValidationText'; | ||
export * from './useAriaDescribedBy'; | ||
export { default as ValidationText } from './ValidationText'; | ||
export { default as HelperText } from './HelperText'; |
18 changes: 18 additions & 0 deletions
18
packages/web-react/src/components/Field/useAriaDescribedBy.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
type UseAriaDescribedByProps = { | ||
id?: string; | ||
helperText?: string; | ||
validationText?: string | string[]; | ||
ariaDescribedBy?: string; | ||
}; | ||
|
||
const useAriaDescribedBy = ({ id, helperText, validationText, ariaDescribedBy }: UseAriaDescribedByProps) => { | ||
if (!id) return { validationTextId: undefined, helperTextId: undefined, joinedAriaDescribedBy: ariaDescribedBy }; | ||
|
||
const validationTextId = id && validationText ? `${id}__validationText` : undefined; | ||
const helperTextId = id && helperText ? `${id}__helperText` : undefined; | ||
const joinedAriaDescribedBy = [ariaDescribedBy, helperTextId, validationTextId].join(' ').trim(); | ||
|
||
return { validationTextId, helperTextId, joinedAriaDescribedBy }; | ||
}; | ||
|
||
export default useAriaDescribedBy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.