feat: basic ui styling of new input

including improved error messaging and removing the label
pull/3056/head
William Grant 1 year ago
parent ccd127de44
commit fd32fb5fbd

@ -234,9 +234,16 @@ export const OverlayRightPanelSettings = () => {
const handleInputChanged = (name: string) => { const handleInputChanged = (name: string) => {
sanitizeDisplayNameOrToast(name, setInputValue, setInputError); sanitizeDisplayNameOrToast(name, setInputValue, setInputError);
if (name.length > MAX_USERNAME_BYTES) {
setInputError(window.i18n('displayNameTooLong'));
}
}; };
const handleEnterPressed = () => { const handleEnterPressed = (name?: string) => {
if (name && name.length >= MAX_USERNAME_BYTES) {
setInputError(window.i18n('displayNameTooLong'));
return;
}
ToastUtils.pushToastSuccess('success', window.i18n('done')); ToastUtils.pushToastSuccess('success', window.i18n('done'));
}; };
@ -335,10 +342,9 @@ export const OverlayRightPanelSettings = () => {
/> />
<SpacerLG /> <SpacerLG />
<SessionInput2 <SessionInput2
label={window.i18n('displayName')}
type="text"
placeholder={window.i18n('enterDisplayName')} placeholder={window.i18n('enterDisplayName')}
value={inputValue} value={inputValue}
error={inputError}
maxLength={MAX_USERNAME_BYTES} maxLength={MAX_USERNAME_BYTES}
onValueChanged={handleInputChanged} onValueChanged={handleInputChanged}
onEnterPressed={handleEnterPressed} onEnterPressed={handleEnterPressed}

@ -1,96 +1,69 @@
import { ChangeEvent, useState } from 'react'; import { ChangeEvent, useState } from 'react';
import classNames from 'classnames'; import { isEmpty } from 'lodash';
import styled from 'styled-components'; import styled from 'styled-components';
import { Noop } from '../../types/Util'; import { Noop } from '../../types/Util';
import { useHTMLDirection } from '../../util/i18n'; import { useHTMLDirection } from '../../util/i18n';
import { Flex } from '../basic/Flex';
import { SpacerMD } from '../basic/Text';
import { SessionIconButton } from '../icon'; import { SessionIconButton } from '../icon';
type Props = { const StyledInputContainer = styled(Flex)<{ error: boolean }>`
label?: string;
error?: string;
type?: string;
value?: string;
placeholder: string;
maxLength?: number;
enableShowHide?: boolean;
onValueChanged?: (value: string) => any;
onEnterPressed?: any;
autoFocus?: boolean;
ref?: any;
inputDataTestId?: string;
};
const StyledInputWithLabelContainer = styled.label`
height: 46.5px;
width: 280px; width: 280px;
font-family: var(--font-default);
color: var(--text-primary-color);
padding: 2px 0 2px 0;
transition: opacity var(--default-duration);
opacity: 1;
position: relative;
label { label {
line-height: 14px;
opacity: 0;
color: var(--text-primary-color); color: var(--text-primary-color);
opacity: 0;
transition: opacity var(--default-duration);
font-size: 10px; &.filled {
line-height: 11px; opacity: 1;
position: absolute; }
top: 0px;
}
&.filled { &.error {
opacity: 1; color: var(--danger-color);
font-weight: 700;
}
} }
&.error { ${props =>
color: var(--danger-color); props.error &&
} `
input {
color: var(--danger-color);
border-color: var(--danger-color);
&::placeholder {
color: var(--danger-color);
opacity: 1;
}
}
`}
`;
input { const StyledInput = styled.input`
border: none; border: 1px solid var(--input-border-color);
outline: 0; border-radius: 13px;
height: 14px; outline: 0;
width: 280px; width: 280px;
background: transparent; background: transparent;
color: var(--input-text-color); color: var(--input-text-color);
font-family: var(--font-default); font-family: var(--font-default);
font-size: 12px; font-size: 12px;
line-height: 14px; line-height: 14px;
position: absolute; padding: var(--margins-lg);
top: 50%;
transform: translateY(-50%); &::placeholder {
color: var(--input-text-placeholder-color);
&::placeholder {
color: var(--input-text-placeholder-color);
}
} }
`; `;
const LabelItem = (props: { inputValue: string; label?: string }) => { const ErrorItem = (props: { id: string; error: string }) => {
return (
<StyledInputWithLabelContainer
htmlFor="session-input-floating-label"
className={classNames(props.inputValue !== '' ? 'filled' : '')}
>
{props.label}
</StyledInputWithLabelContainer>
);
};
const ErrorItem = (props: { error: string | undefined }) => {
return ( return (
<StyledInputWithLabelContainer <label htmlFor={props.id} className={'filled error'}>
htmlFor="session-input-floating-label"
className={classNames('filled error')}
>
{props.error} {props.error}
</StyledInputWithLabelContainer> </label>
); );
}; };
@ -108,18 +81,33 @@ const ShowHideButton = (props: { toggleForceShow: Noop }) => {
); );
}; };
type Props = {
error?: string;
type?: string;
value?: string;
placeholder: string;
maxLength?: number;
enableShowHide?: boolean;
onValueChanged?: (value: string) => any;
onEnterPressed?: (value?: string) => any;
autoFocus?: boolean;
ref?: any;
inputDataTestId?: string;
id?: string;
};
export const SessionInput2 = (props: Props) => { export const SessionInput2 = (props: Props) => {
const { const {
autoFocus, autoFocus,
placeholder, placeholder,
type, type = 'text',
value, value,
maxLength, maxLength,
enableShowHide, enableShowHide,
error, error,
label,
onValueChanged, onValueChanged,
inputDataTestId, inputDataTestId,
id = 'session-input-floating-label',
} = props; } = props;
const [inputValue, setInputValue] = useState(''); const [inputValue, setInputValue] = useState('');
const [forceShow, setForceShow] = useState(false); const [forceShow, setForceShow] = useState(false);
@ -136,14 +124,15 @@ export const SessionInput2 = (props: Props) => {
}; };
return ( return (
<StyledInputWithLabelContainer> <StyledInputContainer
{error ? ( container={true}
<ErrorItem error={props.error} /> flexDirection="column"
) : ( justifyContent="center"
<LabelItem inputValue={inputValue} label={label} /> alignItems="center"
)} error={Boolean(error)}
<input >
id="session-input-floating-label" <StyledInput
id={id}
type={correctType} type={correctType}
placeholder={placeholder} placeholder={placeholder}
value={value} value={value}
@ -156,7 +145,7 @@ export const SessionInput2 = (props: Props) => {
onBlur={updateInputValue} onBlur={updateInputValue}
onKeyDown={event => { onKeyDown={event => {
if (event.key === 'Enter' && props.onEnterPressed) { if (event.key === 'Enter' && props.onEnterPressed) {
props.onEnterPressed(); props.onEnterPressed(inputValue !== '' ? inputValue : undefined);
} }
}} }}
/> />
@ -168,6 +157,12 @@ export const SessionInput2 = (props: Props) => {
}} }}
/> />
)} )}
</StyledInputWithLabelContainer> {!isEmpty(error) ? (
<>
<SpacerMD />
<ErrorItem id={id} error={error!} />
</>
) : null}
</StyledInputContainer>
); );
}; };

Loading…
Cancel
Save