Skip to content

Commit

Permalink
Move text generation to InputDropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
nmanu1 committed Oct 28, 2021
1 parent 0c39c04 commit 0f5cecd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
16 changes: 12 additions & 4 deletions sample-app/src/components/InputDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useReducer, KeyboardEvent, useRef, useEffect, useState } from "react"
import Dropdown, { Option } from './Dropdown';
import ScreenReader from "./ScreenReader";
import { processTranslation } from './utils/processTranslation';

interface Props {
inputValue?: string,
placeholder?: string,
screenReaderInstructions?: string,
screenReaderInstructionsId?: string,
hasAutocompleteCount: boolean,
shouldAutocompleteCount: boolean,
options: Option[],
optionIdPrefix: string,
onSubmit?: (value: string) => void,
Expand Down Expand Up @@ -54,7 +55,7 @@ export default function InputDropdown({
placeholder,
screenReaderInstructions,
screenReaderInstructionsId,
hasAutocompleteCount,
shouldAutocompleteCount,
options,
optionIdPrefix,
onSubmit = () => {},
Expand Down Expand Up @@ -148,15 +149,22 @@ export default function InputDropdown({
/>
{renderButtons()}
</div>
{(screenReaderInstructionsId || hasAutocompleteCount) &&
{(screenReaderInstructionsId || shouldAutocompleteCount) &&
<ScreenReader
instructionsId={screenReaderInstructionsId}
instructions={screenReaderInstructions}
hasCount={hasAutocompleteCount}
shouldCount={shouldAutocompleteCount}
hasInput={!!inputRef.current.value}
optionsLength={options.length}
shouldDisplayOptions={shouldDisplayDropdown}
countKey={countKey}
generateCountText={(numOptions: number) => {
return processTranslation({
phrase: `${numOptions} autocomplete option found.`,
pluralForm: `${numOptions} autocomplete options found.`,
count: numOptions
});
}}
cssClasses={cssClasses}
/>
}
Expand Down
23 changes: 10 additions & 13 deletions sample-app/src/components/ScreenReader.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { useRef } from "react";
import { processTranslation } from './utils/processTranslation';
import '../sass/ScreenReader.scss';

interface Props {
instructionsId?: string,
instructions?: string,
hasCount: boolean,
shouldCount: boolean,
shouldDisplayOptions?: boolean,
hasInput?: boolean,
optionsLength?: number,
shouldDisplayOptions?: boolean,
countKey?: number,
generateCountText?: (numOptions: number) => string,
cssClasses: {
screenReaderInstructions?: string,
screenReaderCount?: string
Expand All @@ -19,11 +19,12 @@ interface Props {
export default function ScreenReader({
instructionsId,
instructions,
hasCount,
hasInput,
optionsLength,
shouldCount,
shouldDisplayOptions,
hasInput,
optionsLength = 0,
countKey,
generateCountText = () => '',
cssClasses
} : Props): JSX.Element | null {

Expand All @@ -35,7 +36,7 @@ export default function ScreenReader({
}

function removeAutocompleteCountText() {
if (hasCount && countRef.current.innerText !== '') {
if (shouldCount && countRef.current.innerText !== '') {
countRef.current.innerText = '';
}
}
Expand All @@ -50,18 +51,14 @@ export default function ScreenReader({
{instructions}
</div>
}
{hasCount &&
{shouldCount &&
<div
className={cssClasses.screenReaderCount}
key={countKey}
aria-live='assertive'
ref={countRef}
>
{processTranslation({
phrase: `${optionsLength} autocomplete option found.`,
pluralForm: `${optionsLength} autocomplete options found.`,
count: optionsLength
})}
{generateCountText(optionsLength)}
</div>
}
</>
Expand Down
2 changes: 1 addition & 1 deletion sample-app/src/components/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default function SearchBar({ placeholder, isVertical, instructions }: Pro
placeholder={placeholder}
screenReaderInstructions={screenReaderInstructions}
screenReaderInstructionsId='SearchBar__srInstructions'
hasAutocompleteCount={true}
shouldAutocompleteCount={true}
options={autocompleteResults.map(result => {
return {
value: result.value,
Expand Down

0 comments on commit 0f5cecd

Please sign in to comment.