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 | 10x 132x 132x 132x 67x 4x 50x 67x 67x 67x 67x 67x 67x 67x 67x 132x 132x 10x | // @flow strict
import React, {
useImperativeHandle,
useReducer,
useRef,
useEffect,
} from 'react';
import cx from 'classnames';
import MDCFloatingLabelFoundation from '@material/floating-label/foundation';
import { mtClassNamesReducer } from '../reducers';
import type { HTMLFloatinLabelElement } from '../mt-types';
type Props = {
label?: string,
preFilled?: boolean,
};
const FloatingLabel = React.forwardRef<Props, ?HTMLFloatinLabelElement>(
({ label, preFilled = false }: Props, ref) => {
const rootRef = useRef<?HTMLSpanElement>();
const [mtClassName, mtClassNameDispatch] = useReducer(
mtClassNamesReducer,
new Set<string>(preFilled ? ['mdc-floating-label--float-above'] : []),
);
useEffect(() => {
const foundation = new MDCFloatingLabelFoundation({
addClass: mClassName =>
mtClassNameDispatch({ type: 'add', className: mClassName }),
removeClass: mClassName =>
mtClassNameDispatch({ type: 'remove', className: mClassName }),
getWidth: () => rootRef.current && rootRef.current.offsetWidth,
registerInteractionHandler: (evtType, handler) =>
rootRef.current && rootRef.current.addEventListener(evtType, handler),
deregisterInteractionHandler: (evtType, handler) =>
rootRef.current &&
rootRef.current.removeEventListener(evtType, handler),
});
foundation.init();
// flowlint-next-line unclear-type:off
const root: HTMLFloatinLabelElement = (rootRef.current: any);
Eif (root) {
root.foundation = foundation;
}
return () => {
foundation.destroy();
};
}, []);
// flowlint-next-line unclear-type:off
useImperativeHandle(ref, () => (rootRef.current: any), []);
return (
<span
className={cx('mdc-floating-label', ...Array.from(mtClassName))}
ref={rootRef}
>
{label}
</span>
);
},
);
FloatingLabel.displayName = 'FloatingLabel';
export default FloatingLabel;
|