// Libs import * as React from 'react'; // Misc import InputGroup from '../inputGroup/InputGroup'; import { BaseInputProps, BaseInputState, BaseInput, } from '../base/input/BaseInput'; import { withFormContext } from '../form/withFormContext'; import { withThemeContext } from '../themeProvider/withThemeContext'; import Button from '../button'; import * as PlusIcon from 'material-design-icons/content/svg/production/ic_add_24px.svg'; import classNames from 'classnames'; export interface TextProps extends BaseInputProps { mask?: string; reverse?: boolean; placeholder?: string; stopClickPropagation?: boolean; inputRef?: React.Ref; readOnly?: boolean; type?: 'number' | 'text'; maxLength?: number; showClearButton?: boolean; after?: React.ReactNode; before?: React.ReactNode; sizeFromValue?: boolean; onNumberChange?: (number: number | undefined, isValid: boolean) => void; number?: number | null | undefined; children?: React.ReactNode; } export interface TextState extends BaseInputState { } export class TextRaw extends BaseInput { public static defaultProps = Object.assign({}, BaseInput.defaultProps, { type: 'text', stopClickPropagation: true, readOnly: false, }) as TextProps; constructor(props: TextProps) { super(props); if (props.number !== undefined || props.onNumberChange) { const value = props.number?.toString() ?? ''; const res = this.handleValueChange(value, true, [], props, true); this.state = { ...this.state, isValid: res.isValid, errors: res.errors, value, }; if (!this.props.ignoreContext) { this.props.formContext && this.props.formContext.updateCallback(this.componentId, { isValid: res.isValid, errors: res.errors, }); } } } public componentDidUpdate(prevProps: TextProps, prevState: TextState) { if ( this.props.onNumberChange && (this.state.isValid !== prevState.isValid || this.state.value !== prevState.value) && this.props.number !== Number(this.state.value) ) { const num = Number(this.state.value); if ( this.state.value === '' && this.props.number !== undefined && this.props.number !== null ) { this.props.onNumberChange(undefined, this.state.isValid); } else if (!isNaN(num) && this.state.value !== '') { this.props.onNumberChange(num, this.state.isValid); } } if ( (this.props.onNumberChange && (!Number.isNaN(prevProps.number) || !Number.isNaN(this.props.number)) && (prevProps.number !== undefined || this.props.number !== undefined) && this.props.number !== prevProps.number) || prevProps.validators !== this.props.validators || prevProps.customValidators !== this.props.customValidators || prevProps.required !== this.props.required ) { this.handleValueChange(this.props.number?.toString() ?? ''); } if ( !this.props.onNumberChange && (prevProps.value !== this.props.value || prevProps.validators !== this.props.validators || prevProps.customValidators !== this.props.customValidators || prevProps.required !== this.props.required) ) { this.handleValueChange(this.props.value, true, [], this.props); } } public render() { const clearButtonHidden = !this.state.value?.length || this.props.disabled; return (
{this.props.children}
{this.props.before} {this.props.after} {!this.props.readOnly && this.props.showClearButton && ( )} {this.renderDefaultValidation()} {this.props.label && ( )}
); } private clearClick = () => { if (this.inputRef.current) { var nativeTextAreaValueSetter = Object.getOwnPropertyDescriptor( window.HTMLInputElement.prototype, 'value' ).set; if (nativeTextAreaValueSetter) { nativeTextAreaValueSetter.call(this.inputRef.current, ''); const event = new Event('input', { bubbles: true }); this.inputRef.current.dispatchEvent(event); } } }; private containerClick = (e: React.MouseEvent) => this.props.stopClickPropagation && e.stopPropagation(); private onKeyDown = (e: React.KeyboardEvent) => this.props.onKeyDown && this.props.onKeyDown(e); } export const Text = withThemeContext>( withFormContext(TextRaw), 'text' ); export default Text;