Skip to content

Commit 7384bd8

Browse files
committed
fix: Fixed calculations for scrolling and focus position
1 parent ea46036 commit 7384bd8

5 files changed

Lines changed: 33 additions & 32 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@codify/ink-form",
2+
"name": "@codify-cli/ink-form",
33
"type": "module",
44
"version": "0.0.0",
55
"description": "Complex user-friendly form component for Codify",
@@ -18,7 +18,7 @@
1818
"commandline",
1919
"cmd"
2020
],
21-
"repository": "https://github.com/kevinwang5658/@codify/ink-form",
21+
"repository": "https://github.com/kevinwang5658/codify-ink-form",
2222
"author": "Lukas Bach <lbach@outlook.de> modified by Kevin Wang",
2323
"devDependencies": {
2424
"@types/node": "20.14.8",

src/Form.tsx

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ export const Form: React.FC<FormProps> = props => {
7676
} else if (key.downArrow) {
7777
// This calculates the maximum amount of children there is. We don't want to scroll past the last item
7878
// Fields.length is number of json fields. 2 is the add and remove buttons. Submit button is sometimes focusable
79-
if (focusedElement + 1 >= sections[currentTab].fields.length + 2 + (canSubmitForm ? 1 : 0)) {
79+
const totalFocusableElements = sections[currentTab].fields.length + 2 + (canSubmitForm ? 1 : 0) + ((sections.length <= 1) ? -1 : 0)
80+
if (focusedElement + 1 >= totalFocusableElements) {
8081
return;
8182
}
8283

@@ -119,12 +120,7 @@ export const Form: React.FC<FormProps> = props => {
119120
<FullScreen>
120121
<Box width="100%" height="90%" flexDirection="column" overflowY="hidden">
121122
<FormHeader {...props} headerRef={headerRef} form={form} currentTab={currentTab} onChangeTab={onChangeTab} editingField={editingField} />
122-
<ScrollArea height={fullHeight - headerHeight} key={currentTab} isStart={focusedElement === -1} editingMode={!!editingField}>
123-
{!editingField && sections[currentTab].description && (
124-
<Box marginX={4}>
125-
<DescriptionRenderer description={sections[currentTab]?.description} />
126-
</Box>
127-
)}
123+
<ScrollArea height={fullHeight - headerHeight} key={currentTab} isStart={focusedElement === -1} numFields={sections[currentTab].fields.length} editingMode={!!editingField}>
128124
{!editingField && (
129125
<Box flexDirection='column'>
130126
<Box marginLeft={1} marginTop={1}>
@@ -153,19 +149,31 @@ export const Form: React.FC<FormProps> = props => {
153149
{!editingField && (
154150
<Text>{' }'}</Text>
155151
)}
156-
<Box flexDirection="row-reverse">
157-
<Button label="Add (duplicate)" id={'addButton'} onClicked={() => duplicateCurrentSection()}/>
158-
</Box>
159-
<Box flexDirection="row-reverse">
160-
<Button label="Remove" id={'removeButton'} isEnabled={sections.length > 1} onClicked={() => removeCurrentSection()}/>
161-
</Box>
162152
</Box>
163153
{!editingField && (
164-
<Box flexDirection="row-reverse">
165-
<SubmitButton canSubmit={canSubmitForm} onSubmit={() => {
166-
props.onSubmit?.(value)
167-
setIsSubmitted(true);
168-
}}/>
154+
<Box flexDirection="row">
155+
<Box flexDirection="column" flexGrow={1} width='40%'>
156+
{!editingField && sections[currentTab].description && (
157+
<Box marginX={1} marginTop={1} flexDirection='column'>
158+
<Text underline>Description:</Text>
159+
<DescriptionRenderer description={sections[currentTab]?.description} />
160+
</Box>
161+
)}
162+
</Box>
163+
<Box flexDirection="column" flexGrow={1} width='40%'>
164+
<Box flexDirection="row-reverse">
165+
<Button label="Add (duplicate)" id={'addButton'} onClicked={() => duplicateCurrentSection()}/>
166+
</Box>
167+
<Box flexDirection="row-reverse">
168+
<Button label="Remove" id={'removeButton'} isEnabled={sections.length > 1} onClicked={() => removeCurrentSection()}/>
169+
</Box>
170+
<Box flexDirection="row-reverse">
171+
<SubmitButton canSubmit={canSubmitForm} onSubmit={() => {
172+
props.onSubmit?.(value)
173+
setIsSubmitted(true);
174+
}}/>
175+
</Box>
176+
</Box>
169177
</Box>
170178
)}
171179
</ScrollArea>

src/ScrollArea.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const reducer = (state, action) => {
3434
}
3535
};
3636

37-
export function ScrollArea({height, isStart, children, editingMode}) {
37+
export function ScrollArea({height, isStart, children, numFields, editingMode}) {
3838
const [state, dispatch] = React.useReducer(reducer, {
3939
height: 10,
4040
scrollTop: 0,
@@ -48,7 +48,8 @@ export function ScrollArea({height, isStart, children, editingMode}) {
4848
// Couple of custom logic baked into here to improve the user experience
4949
// isStart ensures that the first down event will not scroll. This is because initially nothing has focus so the first down gives focus to the first element
5050
// The children.length calculation calculates a rough estimate of the total height. The 3 is the height of the buttons at the bottom.
51-
if (isStart || (children.length) * 1 + 3 * 3 < (height - 6)) {
51+
// The 4 is the fixed length of the brackets in the display.
52+
if (isStart || ((numFields + (3 * 3) + 4) < height)) {
5253
setCanScroll(false);
5354
dispatch({
5455
type: 'RESET'
@@ -58,7 +59,7 @@ export function ScrollArea({height, isStart, children, editingMode}) {
5859
}
5960

6061
focusManager.enableFocus();
61-
}, [height, isStart, children.length]);
62+
}, [height, isStart, numFields]);
6263

6364
// Scroll to the top when entering or exiting edit mode
6465
if (!isEditing && editingMode) {

src/SubmitButton.tsx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@ export const SubmitButton: React.FC<{
1414

1515
return (
1616
<Box marginRight={2}>
17-
<Box marginRight={2} paddingY={1}>
18-
<Text>
19-
{!props.canSubmit
20-
? 'There are still required inputs you have not competed yet.'
21-
: isFocused
22-
? 'Press Enter to submit'
23-
: 'Use the arrow keys to navigate to the submit button.'}
24-
</Text>
25-
</Box>
2617
<Box borderStyle={'round'} borderColor={!props.canSubmit ? 'gray' : isFocused ? 'blue' : 'green'} paddingX={2}>
2718
<Text color={!props.canSubmit ? 'gray' : isFocused ? 'blue' : 'green'} bold={true} underline={isFocused}>
2819
{props.canSubmit ? 'Submit' : 'Cannot submit yet'}

src/demo/Test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export function Test() {
4949
},
5050
{
5151
title: "asdf-install",
52+
description: 'Asdf install is responsible for installing an asdf resource',
5253
fields: [
5354
{ type: 'string', name: 'directory', label: 'directory', required: true },
5455
]

0 commit comments

Comments
 (0)