import React, { useCallback, useContext, useState } from 'react'; import { StyledAutoSizingTextInputContainer, StyledAutoSizingTextInputWrapper, StyledAutoSizingTextInputFiller, StyledCellButton, StyledContainer, StyledLabel, StyledInput, StyledSection, } from './CellStyles'; import { default as StandaloneSwitch } from './Switch'; export { StyledFooter as Footer, StyledFooterBoldText as FooterBoldText, StyledFooterText as FooterText, StyledInputFrame as InputFrame, StyledSectionTitle as SectionTitle, StyledSubText as SubText, StyledTintedIcon as Icon, } from './CellStyles'; export { default as UntintedIcon } from './ImageView'; const CellSectionContext = React.createContext(false); const CellDisabledContext = React.createContext(false); interface IContainerProps extends React.HTMLAttributes { disabled?: boolean; } export function Container({ disabled, ...otherProps }: IContainerProps) { return ( ); } interface ICellButtonProps extends React.ButtonHTMLAttributes { selected?: boolean; } export const CellButton = React.forwardRef(function Button( props: ICellButtonProps, ref: React.Ref, ) { const containedInSection = useContext(CellSectionContext); return ; }); interface ISectionProps { children?: React.ReactNode; className?: string; } export function Section(props: ISectionProps) { return ( {props.children} ); } export function Label(props: React.HTMLAttributes) { const disabled = useContext(CellDisabledContext); return ; } export function Switch(props: StandaloneSwitch['props']) { const disabled = useContext(CellDisabledContext); return ; } interface IInputProps extends React.InputHTMLAttributes { value?: string; validateValue?: (value: string) => boolean; modifyValue?: (value: string) => string; submitOnBlur?: boolean; onSubmitValue?: (value: string) => void; onChangeValue?: (value: string) => void; } interface IInputState { value?: string; focused: boolean; } export class Input extends React.Component { public state = { value: this.props.value ?? '', focused: false, }; public componentDidUpdate(prevProps: IInputProps, _prevState: IInputState) { if ( !this.state.focused && prevProps.value !== this.props.value && this.props.value !== this.state.value ) { this.setState( (_state, props) => ({ value: props.value, }), () => { this.props.onChangeValue?.(this.state.value); }, ); } } public render() { const { type: _type, onChange: _onChange, onFocus: _onFocus, onBlur: _onBlur, onKeyPress: _onKeyPress, value: _value, modifyValue: _modifyValue, submitOnBlur: _submitOnBlur, onChangeValue: _onChangeValue, onSubmitValue: _onSubmitValue, validateValue, ...otherProps } = this.props; return ( {(disabled) => ( )} ); } private onChange = (event: React.ChangeEvent) => { const value = this.props.modifyValue?.(event.target.value) ?? event.target.value; this.setState({ value }); this.props.onChange?.(event); this.props.onChangeValue?.(value); }; private onFocus = (event: React.FocusEvent) => { this.setState({ focused: true }); this.props.onFocus?.(event); }; private onBlur = (event: React.FocusEvent) => { this.setState({ focused: false }); this.props.onBlur?.(event); if (this.props.submitOnBlur) { this.props.onSubmitValue?.(this.state.value); } }; private onKeyPress = (event: React.KeyboardEvent) => { if (event.key === 'Enter') { this.props.onSubmitValue?.(this.state.value); } this.props.onKeyPress?.(event); }; } export function AutoSizingTextInput({ onChangeValue, ...otherProps }: IInputProps) { const [value, setValue] = useState(otherProps.value ?? ''); const onChangeValueWrapper = useCallback( (value: string) => { setValue(value); onChangeValue?.(value); }, [onChangeValue], ); return ( {value === '' ? otherProps.placeholder : value} ); }