Skip to content

Commit 4a87a37

Browse files
committed
fixed varous bugs relating to the enable/disable of the commit button
1 parent 0bacc7f commit 4a87a37

File tree

2 files changed

+18
-29
lines changed

2 files changed

+18
-29
lines changed

src/components/CommitBox.tsx

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@ import {
1010
} from '../style/BranchHeaderStyle';
1111

1212
export interface ICommitBoxProps {
13-
hasStagedFiles: boolean;
14-
commitAllStagedFiles: (message: string) => Promise<void>;
13+
hasFiles: boolean;
14+
commitFunc: (message: string) => Promise<void>;
1515
}
1616

1717
export interface ICommitBoxState {
1818
/**
1919
* Commit message
2020
*/
2121
value: string;
22-
disableSubmit: boolean;
2322
}
2423

2524
export class CommitBox extends React.Component<
@@ -29,8 +28,7 @@ export class CommitBox extends React.Component<
2928
constructor(props: ICommitBoxProps) {
3029
super(props);
3130
this.state = {
32-
value: '',
33-
disableSubmit: true
31+
value: ''
3432
};
3533
}
3634

@@ -45,24 +43,15 @@ export class CommitBox extends React.Component<
4543
/** Initalize commit message input box */
4644
initializeInput = (): void => {
4745
this.setState({
48-
value: '',
49-
disableSubmit: true
46+
value: ''
5047
});
5148
};
5249

5350
/** Handle input inside commit message box */
5451
handleChange = (event: any): void => {
55-
if (event.target.value && event.target.value !== '') {
56-
this.setState({
57-
value: event.target.value,
58-
disableSubmit: false
59-
});
60-
} else {
61-
this.setState({
62-
value: event.target.value,
63-
disableSubmit: true
64-
});
65-
}
52+
this.setState({
53+
value: event.target.value
54+
});
6655
};
6756

6857
/** Update state of commit message input box */
@@ -86,22 +75,22 @@ export class CommitBox extends React.Component<
8675
>
8776
<textarea
8877
className={classes(textInputStyle, stagedCommitMessageStyle)}
89-
disabled={!this.props.hasStagedFiles}
78+
disabled={!this.props.hasFiles}
9079
placeholder={
91-
this.props.hasStagedFiles
80+
this.props.hasFiles
9281
? 'Input message to commit staged changes'
9382
: 'Stage your changes before commit'
9483
}
9584
value={this.state.value}
9685
onChange={this.handleChange}
9786
/>
9887
<input
99-
className={this.checkReadyForSubmit(this.props.hasStagedFiles)}
88+
className={this.checkReadyForSubmit(this.props.hasFiles)}
10089
type="button"
10190
title="Commit"
102-
disabled={this.state.disableSubmit}
91+
disabled={!(this.props.hasFiles && this.state.value)}
10392
onClick={() => {
104-
this.props.commitAllStagedFiles(this.state.value);
93+
this.props.commitFunc(this.state.value);
10594
this.initializeInput();
10695
}}
10796
/>

tests/test-components/CommitBox.spec.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ describe('CommitBox', () => {
1111
describe('#checkReadyForSubmit()', () => {
1212
it('should update commit box state to be ready when changes are staged', () => {
1313
const box = new CommitBox({
14-
commitAllStagedFiles: async () => {},
15-
hasStagedFiles: true
14+
commitFunc: async () => {},
15+
hasFiles: true
1616
});
1717

1818
let actual = box.checkReadyForSubmit(true);
@@ -26,8 +26,8 @@ describe('CommitBox', () => {
2626

2727
it('should update commit box state to be disabled when no changes are staged', () => {
2828
const box = new CommitBox({
29-
commitAllStagedFiles: async () => {},
30-
hasStagedFiles: true
29+
commitFunc: async () => {},
30+
hasFiles: true
3131
});
3232

3333
let actual = box.checkReadyForSubmit(false);
@@ -40,8 +40,8 @@ describe('CommitBox', () => {
4040

4141
it('should be ready to commit with a message set.', () => {
4242
const box = new CommitBox({
43-
commitAllStagedFiles: async () => {},
44-
hasStagedFiles: true
43+
commitFunc: async () => {},
44+
hasFiles: true
4545
});
4646
box.setState(
4747
{

0 commit comments

Comments
 (0)