Skip to content

Commit bddf98b

Browse files
committed
fix: Fixed bug with removing the first element. Also disabled remove button if there is only 1 element left
1 parent d9c0012 commit bddf98b

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

src/Button.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@ export function Button(props: {
55
label: string;
66
onClicked?: () => void;
77
id?: string;
8+
isEnabled?: boolean;
89
}) {
9-
const { isFocused } = useFocus(props.id ? { id: props.id } : {});
10+
const isEnabled = props.isEnabled ?? true;
11+
const { isFocused } = useFocus({ ...(props.id ? { id: props.id } : {}), isActive: props.isEnabled });
12+
1013
useInput((input, key) => {
14+
if (!isEnabled) {
15+
return;
16+
}
17+
1118
if (key.return && isFocused) {
1219
props.onClicked?.();
1320
}
1421
});
1522

16-
return <Box marginX={2} paddingX={1} borderStyle="round" borderColor={isFocused ? 'blue' : 'magenta'}>
23+
return <Box marginX={2} paddingX={1} borderStyle="round" borderColor={!isEnabled ? 'gray' : isFocused ? 'blue' : 'magenta'}>
1724
<Box flexGrow={1}>
1825
<Text underline={isFocused} color={isFocused ? 'blue' : undefined}>
1926
{props.label}

src/Form.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export const Form: React.FC<FormProps> = props => {
9494
{ isActive: !editingField }
9595
);
9696

97-
const duplicateCurrentItem = () => {
97+
const duplicateCurrentSection = () => {
9898
const newTabs = [...sections]
9999
newTabs.splice(currentTab + 1, 0, sections[currentTab])
100100

@@ -105,14 +105,14 @@ export const Form: React.FC<FormProps> = props => {
105105
setValue([...value]);
106106
}
107107

108-
const removeCurrentItem = () => {
108+
const removeCurrentSection = () => {
109109
const newTabs = [...sections]
110110
newTabs.splice(currentTab, 1);
111111
value.splice(currentTab, 1)
112112

113113
setSections(newTabs);
114114
setValue([...value]);
115-
onChangeTab(currentTab - 1)
115+
onChangeTab(Math.max(currentTab - 1, 0))
116116
}
117117

118118
return (
@@ -154,10 +154,10 @@ export const Form: React.FC<FormProps> = props => {
154154
<Text>{' }'}</Text>
155155
)}
156156
<Box flexDirection="row-reverse">
157-
<Button label="Add (duplicate)" id={'addButton'} onClicked={() => duplicateCurrentItem()}/>
157+
<Button label="Add (duplicate)" id={'addButton'} onClicked={() => duplicateCurrentSection()}/>
158158
</Box>
159159
<Box flexDirection="row-reverse">
160-
<Button label="Remove" id={'removeButton'} onClicked={() => removeCurrentItem()}/>
160+
<Button label="Remove" id={'removeButton'} isEnabled={sections.length > 1} onClicked={() => removeCurrentSection()}/>
161161
</Box>
162162
</Box>
163163
{!editingField && (

0 commit comments

Comments
 (0)