Skip to content

Commit

Permalink
Make a WithFont extension of <li> for Autocomplete renderOptions (#2620)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Sep 22, 2023
1 parent cecfb5c commit 1228d0a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { ReactElement, useContext, useEffect } from "react";
import { Key } from "ts-key-enum";

import { WritingSystem } from "api/models";
import { TextFieldWithFont } from "utilities/fontComponents";
import { LiWithFont, TextFieldWithFont } from "utilities/fontComponents";
import SpellCheckerContext from "utilities/spellCheckerContext";

interface GlossWithSuggestionsProps {
Expand Down Expand Up @@ -76,6 +76,16 @@ export default function GlossWithSuggestions(
variant={props.isNew ? "outlined" : "standard"}
/>
)}
renderOption={(liProps, option, { selected }) => (
<LiWithFont
{...liProps}
analysis
aria-selected={selected}
lang={props.analysisLang.bcp47}
>
{option}
</LiWithFont>
)}
onKeyPress={(e: React.KeyboardEvent) => {
if (e.key === Key.Enter) {
props.handleEnter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { ReactElement, useEffect } from "react";
import { Key } from "ts-key-enum";

import { WritingSystem } from "api/models";
import { TextFieldWithFont } from "utilities/fontComponents";
import { LiWithFont, TextFieldWithFont } from "utilities/fontComponents";

interface VernWithSuggestionsProps {
isNew?: boolean;
Expand Down Expand Up @@ -65,6 +65,11 @@ export default function VernWithSuggestions(
vernacular
/>
)}
renderOption={(liProps, option, { selected }) => (
<LiWithFont {...liProps} aria-selected={selected} vernacular>
{option}
</LiWithFont>
)}
/>
);
}
48 changes: 38 additions & 10 deletions src/utilities/fontComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import FontContext, { WithFontProps } from "utilities/fontContext";
type TextFieldWithFontProps = TextFieldProps & WithFontProps;

/**
* TextField modified for use within a FontContext.
* Input props are extended with 3 optional props:
* analysis: bool? (used to apply the default analysis font);
* lang: string? (bcp47 lang-tag for applying the appropriate analysis font);
* vernacular: bool? (used to apply the vernacular font).
* `TextField` modified for use within a `FontContext`.
* The props are extended with 3 optional props:
* `analysis: bool?` (used to apply the default analysis font);
* `lang: string?` (bcp47 lang-tag for applying the appropriate analysis font);
* `vernacular: bool?` (used to apply the vernacular font).
*/
export function TextFieldWithFont(props: TextFieldWithFontProps): ReactElement {
const fontContext = useContext(FontContext);
Expand All @@ -42,11 +42,11 @@ export function TextFieldWithFont(props: TextFieldWithFontProps): ReactElement {
type TypographyWithFontProps = TypographyProps & WithFontProps;

/**
* Typography modified for use within a FontContext.
* Input props are extended with 3 optional props:
* analysis: bool? (used to apply the default analysis font);
* lang: string? (bcp47 lang-tag for applying the appropriate analysis font);
* vernacular: bool? (used to apply the vernacular font).
* `Typography` modified for use within a `FontContext`.
* The props are extended with 3 optional props:
* `analysis: bool?` (used to apply the default analysis font);
* `lang: string?` (bcp47 lang-tag for applying the appropriate analysis font);
* `vernacular: bool?` (used to apply the vernacular font).
*/
export function TypographyWithFont(
props: TypographyWithFontProps
Expand All @@ -64,3 +64,31 @@ export function TypographyWithFont(
/>
);
}

type LiWithFontProps = React.DetailedHTMLProps<
React.LiHTMLAttributes<HTMLLIElement>,
HTMLLIElement
> &
WithFontProps;

/**
* `li` modified for use within a `FontContext`.
* The props are extended with 3 optional props:
* `analysis: bool?` (used to apply the default analysis font);
* `lang: string?` (bcp47 lang-tag for applying the appropriate analysis font);
* `vernacular: bool?` (used to apply the vernacular font).
*/
export function LiWithFont(props: LiWithFontProps) {
const fontContext = useContext(FontContext);
// Use spread to remove the custom props from what is passed into li.
const { analysis, lang, vernacular, ...liProps } = props;
return (
<li
{...liProps}
style={fontContext.addFontToStyle(
{ analysis, lang, vernacular },
liProps.style
)}
/>
);
}

0 comments on commit 1228d0a

Please sign in to comment.