Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 7872856

Browse files
author
Matt Goo
authored
fix(text-field): allow for input isValid override (#374)
1 parent e903dd5 commit 7872856

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

packages/text-field/Input.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ export default class Input extends React.Component {
142142
}
143143

144144
isBadInput = () => this.inputElement.current.validity.badInput;
145-
isValid = () => this.inputElement.current.validity.valid;
145+
isValid = () => {
146+
if (this.props.isValid !== undefined) {
147+
return this.props.isValid;
148+
}
149+
return this.inputElement.current.validity.valid;
150+
}
146151

147152
render() {
148153
const {
@@ -151,6 +156,7 @@ export default class Input extends React.Component {
151156
/* eslint-disable no-unused-vars */
152157
className,
153158
foundation,
159+
isValid,
154160
value,
155161
handleFocusChange,
156162
handleValueChange,
@@ -186,6 +192,7 @@ Input.propTypes = {
186192
className: PropTypes.string,
187193
inputType: PropTypes.string,
188194
disabled: PropTypes.bool,
195+
isValid: PropTypes.bool,
189196
foundation: PropTypes.shape({
190197
activateFocus: PropTypes.func,
191198
deactivateFocus: PropTypes.func,
@@ -215,6 +222,7 @@ Input.defaultProps = {
215222
className: '',
216223
inputType: 'input',
217224
disabled: false,
225+
isValid: undefined,
218226
foundation: {
219227
activateFocus: () => {},
220228
deactivateFocus: () => {},

packages/text-field/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ className | String | Classes to be applied to the input element.
8383
disabled | Function | Disables the input and the parent text field.
8484
foundation | Function | The text field foundation.
8585
handleValueChange | Function | A callback function to update React Text Field's value.
86+
isValid | Boolean | If set, this value will override the native input's validation.
8687
id | String | The `<input>` id attribute.
8788
onBlur | Function | Blur event handler.
8889
onChange | Function | Change event handler.

test/unit/text-field/Input.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,30 @@ test('#isValid returns false if input is invalid', () => {
4040
assert.isFalse(isValidInput);
4141
});
4242

43+
test('#isValid returns true if prop.isValid is set to true', () => {
44+
const wrapper = mount(<Input value='m' pattern='[a-z]' isValid />);
45+
const isValidInput = wrapper.instance().isValid();
46+
assert.isTrue(isValidInput);
47+
});
48+
49+
test('#isValid returns false if prop.isValid is set to false', () => {
50+
const wrapper = mount(<Input value='m' pattern='[a-z]' isValid={false} />);
51+
const isValidInput = wrapper.instance().isValid();
52+
assert.isFalse(isValidInput);
53+
});
54+
55+
test('#isValid returns false if prop.isValid is set to false and input is invalid', () => {
56+
const wrapper = mount(<Input value='meow' pattern='[a-z]' isValid={false}/>);
57+
const isValidInput = wrapper.instance().isValid();
58+
assert.isFalse(isValidInput);
59+
});
60+
61+
test('#isValid returns true if prop.isValid is set to true and input is invalid', () => {
62+
const wrapper = mount(<Input value='meow' pattern='[a-z]' isValid/>);
63+
const isValidInput = wrapper.instance().isValid();
64+
assert.isTrue(isValidInput);
65+
});
66+
4367
test('#componentDidMount should call props.setInputId if props.id exists', () => {
4468
const setInputId = td.func();
4569
shallow(<Input setInputId={setInputId} id='best-id'/>);
@@ -82,6 +106,14 @@ test('#componentDidMount calls props.handleValueChange when the foundation initi
82106
td.verify(handleValueChange(value, td.matchers.isA(Function)), {times: 1});
83107
});
84108

109+
test('#componentDidMount does not call props.handleValueChange when there is no props.value', () => {
110+
const handleValueChange = td.func();
111+
shallow(<Input
112+
handleValueChange={handleValueChange}
113+
/>);
114+
td.verify(handleValueChange(td.matchers.anything(), td.matchers.isA(Function)), {times: 0});
115+
});
116+
85117
test('change to minLength calls handleValidationAttributeChange', () => {
86118
const handleValidationAttributeChange = td.func();
87119
const wrapper = shallow(<Input foundation={{handleValidationAttributeChange}} />);

0 commit comments

Comments
 (0)