Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove random key from comment field #4177

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 7 additions & 8 deletions app/components/CommentForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,28 @@ const CommentForm = ({
<Card>
<LegoFinalForm
validateOnSubmitOnly
initialValues={{
commentKey: Math.random(),
}}
validate={validate}
onSubmit={({ text }) =>
onSubmit={({ text }, form) => {
// Clear the form value
form.change('text', undefined);
Comment on lines +53 to +54
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever!


dispatch(
addComment({
contentTarget,
text,
parent,
})
)
}
);
}}
>
{({ handleSubmit, values }) => {
{({ handleSubmit }) => {
return (
<form onSubmit={handleSubmit}>
<Flex alignItems="center" gap="1rem">
<ProfilePicture size={40} user={user} />

<div className={styles.field}>
<Field
key={values.commentKey}
autoFocus={autoFocus}
name="text"
placeholder={placeholder}
Expand Down
16 changes: 9 additions & 7 deletions app/components/Form/TextInput.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import cx from 'classnames';
import { useRef } from 'react';
import { useMemo, useRef } from 'react';
import Icon from 'app/components/Icon';
import Flex from 'app/components/Layout/Flex';
import { createField } from './Field';
import styles from './TextInput.css';
import type { ReactNode, InputHTMLAttributes } from 'react';
import type { ReactNode, InputHTMLAttributes, RefObject } from 'react';

type Props = {
type?: string;
prefix?: ReactNode;
suffix?: string;
className?: string;
inputRef?: HTMLInputElement;
inputRef?: RefObject<HTMLInputElement>;
disabled?: boolean;
readOnly?: boolean;
placeholder?: string;
Expand All @@ -32,8 +32,10 @@ const TextInput = ({
centered = false,
...props
}: Props) => {
/* New ref is made because text inputs that are not Fields are not given a ref */
const newInputRef = useRef<HTMLInputElement>(inputRef);
// New ref is made because text inputs that are not Fields are not given a ref
const newInputRef = useRef<HTMLInputElement>(null);
// Force use of inputRef from props if set, as newInputRef does not update the inputRef and parent components thus loose access
const ref = useMemo(() => inputRef ?? newInputRef, [inputRef, newInputRef]);

return (
<Flex
Expand All @@ -53,12 +55,12 @@ const TextInput = ({
<Icon
name={prefix}
size={16}
onClick={() => newInputRef.current.focus()}
onClick={() => ref.current && ref.current.focus()}
className={styles.prefix}
/>
)}
<input
ref={newInputRef}
ref={ref}
type={type}
placeholder={placeholder}
disabled={disabled}
Expand Down
2 changes: 0 additions & 2 deletions cypress/e2e/event_visit_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ describe('View event', () => {
it('Should be possible to comment', () => {
cy.visit('/events/20');

cy.wait(500);

cy.contains('button', 'Kommenter').should('not.be.visible');
cy.get(c('CommentForm')).find('input').first().click();
cy.focused().type('This event will be awesome');
Expand Down