Files
TallyNote/web-next/src/components/AccessibleInput.tsx
T
Qiufeng 12495fb6a4
TallyNote release / linux-x64 (push) Successful in 6m24s
release: 1.1.0
2026-08-31 20:11:34 +08:00

29 lines
1.2 KiB
TypeScript

import { useEffect, useRef } from "react";
import { Input, type InputProps, type InputRef } from "tdesign-react";
export interface AccessibleInputProps extends InputProps {
inputAriaLabel?: string;
inputAriaInvalid?: boolean;
inputAriaDescribedby?: string;
}
/**
* TDesign places unknown aria props on its wrapper. This adapter mirrors the
* important field attributes onto the native input so labels, errors and
* keyboard tooling target the actual editable control.
*/
export default function AccessibleInput({ inputAriaLabel, inputAriaInvalid, inputAriaDescribedby, ...props }: AccessibleInputProps) {
const ref = useRef<InputRef>(null);
useEffect(() => {
const input = ref.current?.inputElement;
if (!input) return;
if (inputAriaLabel) input.setAttribute("aria-label", inputAriaLabel);
else input.removeAttribute("aria-label");
if (inputAriaInvalid) input.setAttribute("aria-invalid", "true");
else input.removeAttribute("aria-invalid");
if (inputAriaDescribedby) input.setAttribute("aria-describedby", inputAriaDescribedby);
else input.removeAttribute("aria-describedby");
}, [inputAriaDescribedby, inputAriaInvalid, inputAriaLabel]);
return <Input {...props} ref={ref} />;
}