Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | 7x 91x 91x 91x 91x 46x 90x 2x 45x 46x 46x 46x 46x 46x 46x 91x 91x 7x | // @flow strict
import React, {
useReducer,
useImperativeHandle,
useEffect,
useRef,
} from 'react';
import cx from 'classnames';
import MDCTextFieldHelperTextFoundation from '@material/textfield/helper-text/foundation';
import { mtClassNamesReducer, mtAttrsReducer } from '../reducers';
import type { HTMLTextFieldHelperTextElement } from '../mt-types';
type Props = {
text: string,
persistent?: boolean,
validation?: boolean,
};
const TextFieldHelperText = React.forwardRef<
Props,
HTMLTextFieldHelperTextElement,
>(({ text, persistent = false, validation = false, ...rest }: Props, ref) => {
const rootRef = useRef<?HTMLParagraphElement>();
const [mtClassName, mtClassNameDispatch] = useReducer(
mtClassNamesReducer,
new Set<string>(),
);
const [mtAttrs, mtAttrsDispatch] = useReducer(mtAttrsReducer, {});
useEffect(() => {
const foundation = new MDCTextFieldHelperTextFoundation({
addClass: mClassName =>
mtClassNameDispatch({ type: 'add', className: mClassName }),
removeClass: mClassName =>
mtClassNameDispatch({ type: 'remove', className: mClassName }),
hasClass: className =>
rootRef.current && rootRef.current.classList.contains(className),
setAttr: (attr, value) =>
mtAttrsDispatch({ type: 'set', name: attr, value }),
removeAttr: attr => mtAttrsDispatch({ type: 'unset', name: attr }),
// NOTE.- next is not needed already
// setContent: (content) => {
// this.root_.textContent = content;
// },
});
foundation.init();
// flowlint-next-line unclear-type:off
const root: HTMLTextFieldHelperTextElement = (rootRef.current: any);
Eif (root) {
root.foundation = foundation;
}
return () => {
foundation.destroy();
};
}, []);
// flowlint-next-line unclear-type:off
useImperativeHandle(ref, () => (rootRef.current: any), []);
return (
<p
{...rest}
{...mtAttrs}
className={cx('mdc-text-field-helper-text', ...Array.from(mtClassName), {
'mdc-text-field-helper-text--persistent': persistent,
'mdc-text-field-helper-text--validation-msg': validation,
})}
ref={rootRef}
>
{text}
</p>
);
});
TextFieldHelperText.displayName = 'TextFieldHelperText';
export default TextFieldHelperText;
|