All files / src/components/profile profile-title-editable.tsx

0% Statements 0/20
0% Branches 0/12
0% Functions 0/3
0% Lines 0/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70                                                                                                                                           
import React, { useState } from "react";
 
import TinyButton from "src/components/tiny-button";
 
import { styleFor as titleStyleFor } from "src/components/title";
import { useUserName } from "src/store/selectors";
import { createStyleFor } from "src/helpers/styles";
 
/** @private */
const styleFor = createStyleFor({
	input: {
		...titleStyleFor.title,
		fontWeight: 300,
	},
});
 
/** @private */
interface Props {
	onSubmit(name: string): void;
	onCancel?(): void;
}
 
export default function ProfileTitleEditable({
	onSubmit,
	onCancel,
}: Props): JSX.Element {
	const [ name, setName ] = useState(useUserName());
 
	const tooShort = !name;
	const tooLong = name.length >= 32;
	const isValid = !tooShort && !tooLong;
 
	return (
		<form
			onSubmit={(event): void => {
				event.preventDefault();
				if (isValid) onSubmit(name);
			}}
		>
			<input
				name="username"
				type="text"
				defaultValue={name}
				style={styleFor.input}
				className="form-control form-control-lg lead text-center border-top-0 border-right-0 border-left-0"
				onInput={(event): void => setName(event.currentTarget.value)}
			/>
			<div className="form-text text-muted text-center">
				<TinyButton className="text-secondary ml-1" onClick={onCancel}>
					Cancel
				</TinyButton>
 
				<TinyButton
					type="submit"
					className={"ml-1 " + (!isValid ? "text-danger" : "")}
					icon={!isValid ? "error" : null}
					iconPosition="right"
					title={tooLong ? (
						"This name is 32+ characters long, which is too long"
					) : tooShort ? (
						"Please, specify a name that is at least 1 character long"
					) : ""}
				>
					Submit
				</TinyButton>
			</div>
		</form>
	);
}