/** * See the NOTICE file distributed with this work for additional information * regarding copyright ownership. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import React from 'react'; import classNames from 'classnames'; import noop from 'lodash/noop'; import styles from './Textarea.scss'; type PropsForRespondingWithEvents = { onChange: (e: React.ChangeEvent) => void; onFocus: (e: React.SyntheticEvent) => void; onBlur: (e: React.SyntheticEvent) => void; callbackWithEvent: true; }; type PropsForRespondingWithData = { onChange: (value: string) => void; onFocus: (value?: string) => void; onBlur: (value?: string) => void; callbackWithEvent: false; }; type OnChangeProps = PropsForRespondingWithEvents | PropsForRespondingWithData; export type Props = { value: string | number; id?: string; name?: string; autoFocus?: boolean; placeholder?: string; resizable: boolean; className?: string; // to customize textarea styling when using CSS modules onKeyUp: (e: React.KeyboardEvent) => void; onKeyDown: (e: React.KeyboardEvent) => void; onKeyPress: (e: React.KeyboardEvent) => void; } & OnChangeProps; const Textarea = (props: Props) => { const eventHandler = (eventName: string) => ( e: | React.ChangeEvent | React.FocusEvent ) => { const value = e.target.value; if (eventName === 'change') { props.callbackWithEvent ? props.onChange(e) : props.onChange(value); } else if (eventName === 'focus') { props.callbackWithEvent ? props.onFocus(e) : props.onFocus(value); } else if (eventName === 'blur') { props.callbackWithEvent ? props.onBlur(e) : props.onBlur(value); } }; const className = classNames(styles.textarea, props.className, { [styles.disableResize]: !props.resizable }); return (