Skip to content

Commit b95db3b

Browse files
committed
feat: Refactored and improved the dimension readngs (moved to a hook). Fixed the can submit logic
1 parent 9889d8b commit b95db3b

4 files changed

Lines changed: 16 additions & 29 deletions

File tree

src/Form.tsx

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { canSubmit } from './canSubmit.js';
99
import { SubmitButton } from './SubmitButton.js';
1010
import { Button } from './Button.js';
1111
import { ScrollArea } from './ScrollArea.js';
12-
import { FullScreen } from './FullScreen.js';
12+
import { FullScreen, useStdoutDimensions } from './FullScreen.js';
1313

1414
export const Form: React.FC<FormProps> = props => {
1515
const isControlled = props.value !== undefined;
@@ -98,30 +98,13 @@ export const Form: React.FC<FormProps> = props => {
9898
setValue([...value]);
9999
}
100100

101-
const [size, setSize] = useState({
102-
columns: process.stdout.columns,
103-
rows: process.stdout.rows,
104-
});
105-
106-
useEffect(() => {
107-
function onResize() {
108-
setSize({
109-
columns: process.stdout.columns,
110-
rows: process.stdout.rows,
111-
});
112-
}
113-
114-
process.stdout.on("resize", onResize);
115-
return () => {
116-
process.stdout.off("resize", onResize);
117-
};
118-
}, []);
101+
const [, fullHeight] = useStdoutDimensions()
119102

120103
return (
121104
<FullScreen>
122105
<Box width="100%" height="90%" flexDirection="column" overflowY="hidden">
123106
<FormHeader {...props} headerRef={headerRef} form={{ ...props.form, sections }} currentTab={currentTab} onChangeTab={onChangeTab} editingField={editingField} />
124-
<ScrollArea height={size.rows - headerHeight} key={currentTab} isStart={focusedElement === 0}>
107+
<ScrollArea height={fullHeight - headerHeight} key={currentTab} isStart={focusedElement === 0}>
125108
{!editingField && sections[currentTab].description && (
126109
<Box marginX={4}>
127110
<DescriptionRenderer description={props.form.sections[currentTab]?.description} />

src/FullScreen.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Box, BoxProps } from 'ink';
22
import React, { PropsWithChildren, useLayoutEffect } from 'react';
33
import { useEffect, useState } from 'react';
44

5-
function useStdoutDimensions(): [number, number] {
5+
export function useStdoutDimensions(): [number, number] {
66
const {columns, rows} = process.stdout;
77
const [size, setSize] = useState({columns, rows});
88
useEffect(() => {

src/ScrollArea.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Box, measureElement, useFocusManager, useInput } from 'ink';
2-
import React, { ReactNode, useEffect, useState } from 'react';
2+
import React, { ReactNode, useEffect, useLayoutEffect, useState } from 'react';
33

44
const reducer = (state, action) => {
55
switch (action.type) {
@@ -46,7 +46,7 @@ export function ScrollArea({height, isStart, children}) {
4646

4747
const innerRef = React.useRef();
4848

49-
useEffect(() => {
49+
useLayoutEffect(() => {
5050
if (isStart || (children.length) * 1 + 3 * 3 < (height - 6)) {
5151
setCanScroll(false);
5252
dispatch({

src/canSubmit.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { FormStructure } from './types.js';
1+
import { FormField, FormStructure } from './types.js';
22

33
export const canSubmit = (form: FormStructure, values: Array<Record<string, unknown>>) => {
4-
return form.sections
5-
.map(section => section.fields)
6-
.reduce((fields1, fields2) => [...fields1, ...fields2], [])
7-
.map(field => !field.required || (values[field.name] !== undefined && values[field.name] !== ''))
8-
.reduce((field1, field2) => field1 && field2, true);
4+
for (const [idx, value] of Object.entries(values)) {
5+
const fields = form.sections[idx].fields as FormField[];
6+
7+
for (const field of fields) {
8+
if (field.required && (value[field.name] === undefined || value[field.name] === '')) {
9+
return false;
10+
}
11+
}
12+
}
913

1014
return true;
1115
};

0 commit comments

Comments
 (0)