Skip to content

Commit b984fbb

Browse files
MariaAgajeff-phillips-18
authored andcommitted
fix(Login card page): Submit is disabled when input is empty (#1188)
1 parent ffbe741 commit b984fbb

File tree

6 files changed

+39
-20
lines changed

6 files changed

+39
-20
lines changed

packages/patternfly-3/patternfly-react/src/components/LoginPage/LoginPage.stories.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ const createProps = () => {
9494
href: '#',
9595
onClick: e => storyAction(e, 'Forgot password was clicked')
9696
},
97-
disableSubmit: false,
9897
submitText: card.form.submitText,
9998
onSubmit: (e, onError) => {
10099
onError(card.form.error);

packages/patternfly-3/patternfly-react/src/components/LoginPage/LoginPage.test.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ const createProps = () => {
7272
href: '#',
7373
onClick: mockFunction
7474
},
75-
disableSubmit: false,
7675
submitText: card.form.submitText
7776
}
7877
}
@@ -177,6 +176,10 @@ test('Submit while inputs are empty cause specific errors to be shown and onChan
177176
const component = mount(<LoginPage {...createProps()} />);
178177
const usernameElement = component.find('input[type="email"]').at(0);
179178
const passwordElement = component.find('input[type="password"]').at(0);
179+
const submit = component.find('button[type="submit"]').at(0);
180+
181+
expect(submit.props().disabled).toEqual(true);
182+
180183
component
181184
.find('form')
182185
.at(0)
@@ -197,15 +200,6 @@ test('Submit while inputs are empty cause specific errors to be shown and onChan
197200
.props().showError
198201
).toEqual(true);
199202

200-
usernameElement.simulate('change', { target: { value: 'Ron' } });
201-
202-
expect(
203-
component
204-
.find(Input)
205-
.at(0)
206-
.props().showError
207-
).toEqual(false);
208-
209203
// check password field
210204
expect(
211205
component
@@ -221,6 +215,15 @@ test('Submit while inputs are empty cause specific errors to be shown and onChan
221215
.props().showError
222216
).toEqual(true);
223217

218+
usernameElement.simulate('change', { target: { value: 'Ron' } });
219+
220+
expect(
221+
component
222+
.find(Input)
223+
.at(0)
224+
.props().showError
225+
).toEqual(false);
226+
224227
passwordElement.simulate('change', { target: { value: 'Q!w2e3' } });
225228

226229
expect(

packages/patternfly-3/patternfly-react/src/components/LoginPage/SocialLoginPage.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ const createProps = () => {
140140
href: '#',
141141
onClick: mockFunction
142142
},
143-
disableSubmit: false,
144143
submitText: card.form.submitText,
145144
onSubmit: mockFunction
146145
},

packages/patternfly-3/patternfly-react/src/components/LoginPage/__snapshots__/LoginPage.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ exports[`Component matches snapshot 1`] = `
172172
</div>
173173
<button
174174
class="login-pf-submit-button btn btn-lg btn-primary btn-block"
175+
disabled=""
175176
type="submit"
176177
>
177178
Log In

packages/patternfly-3/patternfly-react/src/components/LoginPage/__snapshots__/SocialLoginPage.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ exports[`Component matches snapshot 1`] = `
170170
</div>
171171
<button
172172
class="login-pf-submit-button btn btn-lg btn-primary btn-block"
173+
disabled=""
173174
type="submit"
174175
>
175176
Log In

packages/patternfly-3/patternfly-react/src/components/LoginPage/components/LoginCardComponents/LoginCardWithValidation.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class LoginCardWithValidation extends React.Component {
2222
form: {
2323
showError: this.props.showError,
2424
submitError: this.props.submitError,
25-
disableSubmit: this.props.disableSubmit,
25+
disableSubmit: true,
2626
isSubmitting: this.props.isSubmitting
2727
}
2828
};
@@ -35,15 +35,30 @@ class LoginCardWithValidation extends React.Component {
3535
window.removeEventListener('keyup', this.toggleCapsLock);
3636
}
3737

38+
// eslint-disable-next-line class-methods-use-this
39+
static getDerivedStateFromProps(props, state) {
40+
// disableSubmit prop will only be used in a not validated login card
41+
const { validate, disableSubmit } = props;
42+
if (validate) return null;
43+
if (disableSubmit !== state.form.disableSubmit) {
44+
return {
45+
form: { disableSubmit }
46+
};
47+
}
48+
return null;
49+
}
50+
3851
onInputChange = (e, inputType) => {
3952
this.props[inputType].onChange && this.props[inputType].onChange(e);
40-
this.setState({
41-
[inputType]: {
42-
...this.state[inputType],
43-
value: e.target.value,
44-
showError: false
45-
}
46-
});
53+
const updateState = {
54+
usernameField: { value: inputType === 'usernameField' ? e.target.value : this.state.usernameField.value },
55+
passwordField: { value: inputType === 'passwordField' ? e.target.value : this.state.passwordField.value }
56+
};
57+
updateState[inputType].showError = false;
58+
updateState.form = {
59+
disableSubmit: updateState.usernameField.value.length < 1 || updateState.passwordField.value.length < 1
60+
};
61+
this.setState(updateState);
4762
};
4863

4964
onInputFocus = (e, inputType) => {
@@ -273,6 +288,7 @@ LoginCardWithValidation.propTypes = {
273288
}),
274289
onSubmit: PropTypes.func,
275290
submitError: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
291+
// eslint-disable-next-line react/no-unused-prop-types
276292
disableSubmit: PropTypes.bool,
277293
isSubmitting: PropTypes.bool,
278294
showError: PropTypes.bool

0 commit comments

Comments
 (0)