/* * Copyright 2020 Actyx AG * * 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 { theme } from '../../theme'; import * as classNames from 'classnames'; import * as React from 'react'; import injectSheet, { StyleSheet, WithStyles } from 'react-jss'; import { compose, setDisplayName } from 'recompose'; type CompProps = Readonly<{ className?: string; type: 'number' | 'text'; value?: number | string; maxLength?: number; readOnly?: boolean; placeholder?: string; disabled?: boolean; blurOnEnter?: boolean; error?: boolean; fullWidth?: boolean; autoFocus?: boolean; onClick?: () => void; onFocus?: () => void; onBlur?: () => void; onChange?: (e: React.ChangeEvent) => void; onKeyDown?: (e: React.KeyboardEvent) => void; }>; type Props = WithStyles & CompProps; class InputComp extends React.Component { input: HTMLInputElement | null = null; handleKeyDown = (e: React.KeyboardEvent) => { const { onKeyDown } = this.props; if (e.key.toLowerCase() === 'enter' && this.input) { this.input.blur(); } if (onKeyDown) { onKeyDown(e); } }; setInputRef = (el: HTMLInputElement) => { this.input = el; }; componentDidMount(): void { if (this.props.autoFocus && this.input) { this.input.focus(); } } render(): React.ReactNode { const { className, blurOnEnter, classes, type, value, maxLength, readOnly, placeholder, disabled, error, fullWidth, onClick, onFocus, onBlur, onChange } = this.props; return ( ); } } type ClassKey = 'root' | 'error' | 'fullWidth'; const inputReset = { outline: 'none', outlineWidth: 0, boxShadow: 'none', border: 'none', backgroundColor: 'transparent' }; const { main } = theme.palette.primary; const styles: StyleSheet = { root: { fontSize: 20, color: theme.palette.common.black, ...inputReset, borderBottom: `${2}px solid ${theme.palette.grey[400]}`, '&:focus': { ...inputReset, borderBottom: `${2}px solid ${main}` }, '&::placeholder': { color: theme.palette.grey[400] } }, error: { color: theme.palette.common.black, ...inputReset, borderBottom: `${2}px solid ${theme.palette.error.main}`, '&:focus': { ...inputReset, borderBottom: `${2}px solid ${theme.palette.error.main}` } }, fullWidth: { width: '100%' } }; export const Input = compose( setDisplayName('Input'), injectSheet(styles) )(InputComp);