import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import React from 'react';
import { AriaLiveRegion, changeFocus } from '@workday/canvas-kit-react/common';
import { FormField } from '@workday/canvas-kit-react/form-field';
import { PrimaryButton } from '@workday/canvas-kit-react/button';
import { TextInput } from '@workday/canvas-kit-react/text-input';
import { system } from '@workday/canvas-tokens-web';
import { createStyles } from '@workday/canvas-kit-styling';
const hintStyles = createStyles({
    height: system.space.x6,
});
export const TextInputWithLiveError = () => {
    const errMsg = 'Error: First name is required.';
    const [hasError, setHasError] = React.useState('no');
    const inputRef = React.useRef(null);
    const handleBlur = (e) => setHasError(e.target.value.trim().length === 0 ? 'error' : 'no');
    const handleSubmit = () => hasError && changeFocus(inputRef.current);
    return (_jsxs(_Fragment, { children: [_jsxs(FormField, { error: hasError === 'error' ? 'error' : undefined, isRequired: true, children: [_jsx(FormField.Label, { children: "First Name:" }), _jsx(FormField.Input, { as: TextInput, onBlur: handleBlur, ref: inputRef }), _jsx(FormField.Hint, { cs: hintStyles, children: _jsx(AriaLiveRegion, { children: hasError === 'error' && errMsg }) })] }), _jsx(PrimaryButton, { onClick: handleSubmit, children: "Continue" })] }));
};
