Skip to content

Commit c18d2a8

Browse files
committed
chore: focus on rte on multirow-multicol add row
1 parent 9e64613 commit c18d2a8

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

packages/frontend/src/components/InputCreator/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type InputCreatorProps = {
1818
stepId?: string
1919
disabled?: boolean
2020
parentType?: string
21+
autoFocus?: boolean
2122
}
2223

2324
type RawOption = {
@@ -29,7 +30,7 @@ const optionGenerator = (options: RawOption[]): IFieldDropdownOption[] =>
2930
options?.map(({ name, value }) => ({ label: name as string, value: value }))
3031

3132
export default function InputCreator(props: InputCreatorProps): JSX.Element {
32-
const { schema, namePrefix, stepId, disabled, parentType } = props
33+
const { schema, namePrefix, stepId, disabled, parentType, autoFocus } = props
3334

3435
const {
3536
key: name,
@@ -138,6 +139,7 @@ export default function InputCreator(props: InputCreatorProps): JSX.Element {
138139
tooltipText={tooltipText}
139140
variableTypes={schema.variableTypes}
140141
parentType={parentType}
142+
autoFocus={autoFocus}
141143
/>
142144
)
143145
}

packages/frontend/src/components/MultiCol.tsx/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export default function MultiCol(props: MultiColProps) {
2929
const isMobile = useBreakpointValue({ base: true, sm: false })
3030
return (
3131
<Flex flexDir={isMobile ? 'column' : 'row'} gap={2} alignItems="center">
32-
{subFields.map((subF) => {
32+
{subFields.map((subF, subFIndex) => {
33+
const { type, variables } = subF
3334
return (
3435
<div
3536
key={`${name}.${subF.key}`}
@@ -39,6 +40,7 @@ export default function MultiCol(props: MultiColProps) {
3940
schema={subF}
4041
namePrefix={name}
4142
parentType="multicol"
43+
autoFocus={subFIndex === 0 && type === 'string' && variables}
4244
{...forwardedInputCreatorProps}
4345
/>
4446
</div>

packages/frontend/src/components/MultiRow/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,16 @@ function MultiRow(props: MultiRowProps): JSX.Element {
6363
})
6464

6565
const handleAddRow = useCallback(() => {
66-
append(newRowDefaultValue)
67-
}, [append, newRowDefaultValue])
66+
// NOTE: only need to use this flag to focus on the rte
67+
// if the first column is a variable-enabled string field
68+
const firstColIsRte =
69+
subFields?.[0]?.type === 'string' && subFields?.[0]?.variables
70+
if (firstColIsRte) {
71+
append({ ...newRowDefaultValue, isNew: true })
72+
} else {
73+
append(newRowDefaultValue)
74+
}
75+
}, [append, newRowDefaultValue, subFields])
6876

6977
return (
7078
// Use Controller's defaultValue to introduce 1 blank row by default. We

packages/frontend/src/components/RichTextEditor/index.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import ImageResize from './ResizableImageExtension'
4343
import { StepVariable } from './StepVariablePlugin'
4444
import Suggestions from './Suggestions'
4545
import {
46+
checkAutoFocus,
4647
genVariableInfoMap,
4748
getPopoverPlacement,
4849
substituteOldTemplates,
@@ -92,6 +93,7 @@ interface EditorProps {
9293
isSingleLine?: boolean
9394
variableTypes?: TDataOutMetadatumType[]
9495
parentType?: string
96+
autoFocus?: boolean
9597
}
9698
const Editor = ({
9799
onChange,
@@ -103,6 +105,7 @@ const Editor = ({
103105
isSingleLine,
104106
variableTypes,
105107
parentType,
108+
autoFocus = false,
106109
}: EditorProps) => {
107110
const { priorExecutionSteps } = useContext(StepExecutionsContext)
108111
const isMobile = useIsMobile()
@@ -171,6 +174,7 @@ const Editor = ({
171174
const editor = useEditor({
172175
extensions,
173176
content,
177+
autofocus: autoFocus,
174178
onUpdate: ({ editor }) => {
175179
if (editor.isEmpty) {
176180
// this is when content of the editor is empty
@@ -292,6 +296,7 @@ interface RichTextEditorProps {
292296
tooltipText?: string
293297
variableTypes?: TDataOutMetadatumType[]
294298
parentType?: string
299+
autoFocus?: boolean
295300
}
296301
const RichTextEditor = ({
297302
required,
@@ -307,8 +312,21 @@ const RichTextEditor = ({
307312
tooltipText,
308313
variableTypes,
309314
parentType,
315+
autoFocus,
310316
}: RichTextEditorProps) => {
311-
const { control } = useFormContext()
317+
const { control, getValues } = useFormContext()
318+
const { shouldAutoFocus, isNewRow, rowData } = checkAutoFocus(
319+
name,
320+
getValues,
321+
autoFocus,
322+
)
323+
324+
// Clear the isNew flag after focusing
325+
useEffect(() => {
326+
if (isNewRow && rowData) {
327+
delete rowData.isNew
328+
}
329+
}, [isNewRow, rowData])
312330

313331
return (
314332
<FormControl flex={1} data-test="text-input-group">
@@ -339,6 +357,7 @@ const RichTextEditor = ({
339357
isSingleLine={isSingleLine}
340358
variableTypes={variableTypes}
341359
parentType={parentType}
360+
autoFocus={shouldAutoFocus}
342361
/>
343362
)}
344363
/>

packages/frontend/src/components/RichTextEditor/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { FieldValues, UseFormGetValues } from 'react-hook-form'
12
import { PlacementWithLogical } from '@chakra-ui/react'
23
import { Editor } from '@tiptap/react'
34
import { HTMLElement, Node, parse, TextNode } from 'node-html-parser'
@@ -150,3 +151,15 @@ export function getPopoverPlacement(
150151
// If there's more space above than below, show the popover above
151152
return spaceAbove > spaceBelow ? 'top-start' : 'bottom-start'
152153
}
154+
155+
export const checkAutoFocus = (
156+
name: string,
157+
getValues: UseFormGetValues<FieldValues>,
158+
autoFocusProp?: boolean,
159+
) => {
160+
const pathParts = name.split('.')
161+
const fieldParentPath = pathParts.slice(0, -1).join('.')
162+
const rowData = getValues(fieldParentPath)
163+
const isNewRow = rowData?.isNew
164+
return { shouldAutoFocus: isNewRow && autoFocusProp, isNewRow, rowData }
165+
}

0 commit comments

Comments
 (0)