-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
STCOM-766 Accessibility | Repeatable Field Component (#2215)
- Loading branch information
Showing
7 changed files
with
196 additions
and
134 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
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 @@ | ||
export { default } from './usePrevious'; |
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,13 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
export const usePrevious = (value) => { | ||
const ref = useRef(); | ||
|
||
useEffect(() => { | ||
ref.current = value; | ||
}, [value]); | ||
|
||
return ref.current; | ||
}; | ||
|
||
export default usePrevious; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React, { useCallback } from "react"; | ||
import PropTypes from "prop-types"; | ||
|
||
import { getFirstFocusable } from "../../util/getFocusableElements"; | ||
|
||
import css from "./RepeatableField.css"; | ||
|
||
export const RepeatableFieldContent = ({ children, isFocused }) => { | ||
const callbackRef = useCallback((node) => { | ||
if (node) { | ||
const elem = getFirstFocusable(node, true, true); | ||
|
||
if (isFocused) { | ||
elem?.focus(); | ||
} | ||
} | ||
}, [isFocused]) | ||
|
||
return ( | ||
<div className={css.repeatableFieldItemContent} ref={callbackRef}> | ||
{children} | ||
</div> | ||
); | ||
} | ||
|
||
RepeatableFieldContent.propTypes = { | ||
children: PropTypes.oneOfType([ | ||
PropTypes.node, | ||
PropTypes.func, | ||
]).isRequired, | ||
isFocused: PropTypes.bool.isRequired, | ||
} |
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 @@ | ||
import usePrevious from "../../../hooks/usePrevious"; | ||
import { useEffect, useState } from "react"; | ||
|
||
export const useFocusedIndex = (fieldsLength) => { | ||
const [focusIndex, setFocusIndex] = useState(null); | ||
|
||
const prevFieldsLength = usePrevious(fieldsLength); | ||
|
||
useEffect(() => { | ||
if (fieldsLength > prevFieldsLength) { // added | ||
setFocusIndex(fieldsLength - 1); | ||
} else { // removed | ||
setFocusIndex(null); | ||
} | ||
}, [fieldsLength]) | ||
|
||
return focusIndex; | ||
}; |
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