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 | 7x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x | // @flow strict
import React, { useRef, useEffect } from 'react';
import cx from 'classnames';
import MDCFormFieldFoundation from '@material/form-field/foundation';
import type { Node } from 'react';
import type { HTMLInputElementWithRipple } from '../mt-types';
type Props = {
children: Node,
label: string,
alignEnd?: boolean,
};
const FormField = ({ children, label, alignEnd = false, ...rest }: Props) => {
const rootRef = useRef();
const labelRef = useRef<?HTMLLabelElement>();
useEffect(() => {
const root = rootRef.current;
const getInput = (): ?HTMLInputElementWithRipple => {
if (root) {
// flowlint-next-line unclear-type:off
return (root.querySelector('input'): any);
}
return undefined;
};
const foundation = new MDCFormFieldFoundation({
registerInteractionHandler: (type, handler) =>
labelRef.current && labelRef.current.addEventListener(type, handler),
deregisterInteractionHandler: (type, handler) =>
labelRef.current && labelRef.current.removeEventListener(type, handler),
activateInputRipple: () => {
const input = getInput();
if (input && input.ripple) {
input.ripple.activate();
}
},
deactivateInputRipple: () => {
const input = getInput();
if (input && input.ripple) {
input.ripple.deactivate();
}
},
});
foundation.init();
return () => {
foundation.destroy();
};
}, []);
return (
<div
ref={rootRef}
className={cx('mdc-form-field', {
'mdc-form-field--align-end': alignEnd,
})}
>
{children}
<label // eslint-disable-line jsx-a11y/label-has-for, jsx-a11y/label-has-associated-control
{...rest}
ref={labelRef}
>
{label}
</label>
</div>
);
};
export default FormField;
|