Skip to content

Commit fd2a453

Browse files
committed
Add max_rows_when_not_focused support to Textarea with focused state-based row limit and apply it to edit format instructions textareas
1 parent ecd8a7e commit fd2a453

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

‎packages/ui/src/components/editor/common/Textarea/Textarea.stories.tsx‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,20 @@ export const WithValue = () => {
3232
/>
3333
)
3434
}
35+
36+
export const WithMaxRowsWhenNotFocused = () => {
37+
const [value, set_value] = useState(
38+
'This is a long text that should be truncated when not focused.\nLine 2\nLine 3\nLine 4\nLine 5'
39+
)
40+
41+
return (
42+
<Textarea
43+
value={value}
44+
on_change={set_value}
45+
placeholder="Will be ignored because value is set"
46+
min_rows={1}
47+
max_rows={10}
48+
max_rows_when_not_focused={2}
49+
/>
50+
)
51+
}

‎packages/ui/src/components/editor/common/Textarea/Textarea.tsx‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { useState } from 'react'
22
import TextareaAutosize from 'react-textarea-autosize'
33
import styles from './Textarea.module.scss'
44

@@ -9,6 +9,7 @@ type Props = {
99
autofocus?: boolean
1010
min_rows?: number
1111
max_rows?: number
12+
max_rows_when_not_focused?: number
1213
on_change: (value: string) => void
1314
on_blur?: () => void
1415
on_key_down?: (e: React.KeyboardEvent<HTMLTextAreaElement>) => void
@@ -17,6 +18,7 @@ type Props = {
1718

1819
export const Textarea = React.forwardRef<HTMLTextAreaElement, Props>(
1920
(props, ref) => {
21+
const [is_focused, set_is_focused] = useState(!!props.autofocus)
2022
const internal_ref = React.useRef<HTMLTextAreaElement | null>(null)
2123

2224
const set_ref = React.useCallback(
@@ -49,18 +51,32 @@ export const Textarea = React.forwardRef<HTMLTextAreaElement, Props>(
4951
props.on_key_down?.(e)
5052
}
5153

54+
const handle_focus = (e: React.FocusEvent<HTMLTextAreaElement>) => {
55+
set_is_focused(true)
56+
props.onFocus?.(e)
57+
}
58+
59+
const handle_blur = (e: React.FocusEvent<HTMLTextAreaElement>) => {
60+
set_is_focused(false)
61+
props.on_blur?.()
62+
}
63+
64+
const max_rows = is_focused
65+
? props.max_rows
66+
: (props.max_rows_when_not_focused ?? props.max_rows)
67+
5268
return (
5369
<TextareaAutosize
5470
id={props.id}
5571
value={props.value}
5672
onChange={(e) => props.on_change(e.target.value)}
57-
onBlur={props.on_blur}
73+
onBlur={handle_blur}
5874
onKeyDown={handle_key_down}
5975
className={styles.textarea}
6076
placeholder={props.placeholder}
6177
minRows={props.min_rows}
62-
maxRows={props.max_rows}
63-
onFocus={props.onFocus}
78+
maxRows={max_rows}
79+
onFocus={handle_focus}
6480
ref={set_ref}
6581
/>
6682
)

‎packages/vscode/src/views/settings/frontend/Home/sections/GeneralSection.tsx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
158158
slot={
159159
<Textarea
160160
value={instructions.whole}
161+
min_rows={3}
162+
max_rows_when_not_focused={3}
161163
on_change={(value) =>
162164
set_instructions((prev) => ({ ...prev, whole: value }))
163165
}
@@ -172,6 +174,8 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
172174
slot={
173175
<Textarea
174176
value={instructions.truncated}
177+
min_rows={3}
178+
max_rows_when_not_focused={3}
175179
on_change={(value) =>
176180
set_instructions((prev) => ({ ...prev, truncated: value }))
177181
}
@@ -186,6 +190,8 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
186190
slot={
187191
<Textarea
188192
value={instructions.diff}
193+
min_rows={3}
194+
max_rows_when_not_focused={3}
189195
on_change={(value) =>
190196
set_instructions((prev) => ({ ...prev, diff: value }))
191197
}

0 commit comments

Comments
 (0)