Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions src/components/ChangePasswordForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,7 @@ export default class ChangePasswordForm extends React.Component {
}
};

if (typeof element.type === 'function' && utils.containsWord(element.type.name, ['input', 'field', 'text'])) {
if (element.props && element.props.name) {
tryMapFormField(element.props.name);
}
} else if (element.type === 'input' || element.type === 'textarea') {
if (element.props.type !== 'submit') {
tryMapFormField(element.props.name);
}
}
utils.mapFormField(element, tryMapFormField);
}

_spIfHandler(action, element) {
Expand Down
10 changes: 1 addition & 9 deletions src/components/LoginForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,7 @@ export default class LoginForm extends React.Component {
}
};

if (typeof element.type === 'function' && utils.containsWord(element.type.name, ['input', 'field', 'text'])) {
if (element.props && element.props.name) {
tryMapFormField(element.props.name);
}
} else if (['input', 'textarea'].indexOf(element.type) > -1) {
if (element.props.type !== 'submit') {
tryMapFormField(element.props.name);
}
}
utils.mapFormField(element, tryMapFormField);
}

_spIfHandler(action, element) {
Expand Down
10 changes: 1 addition & 9 deletions src/components/RegistrationForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,7 @@ export default class RegistrationForm extends React.Component {
}
};

if (typeof element.type === 'function' && utils.containsWord(element.type.name, ['input', 'field', 'text'])) {
if (element.props && element.props.name) {
tryMapFormField(element.props.name);
}
} else if (['input', 'textarea'].indexOf(element.type) > -1) {
if (element.props.type !== 'submit') {
tryMapFormField(element.props.name);
}
}
utils.mapFormField(element, tryMapFormField);
}

_spIfHandler(action, element) {
Expand Down
10 changes: 1 addition & 9 deletions src/components/ResetPasswordForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,7 @@ export default class ResetPasswordForm extends React.Component {
}
};

if (typeof element.type === 'function' && utils.containsWord(element.type.name, ['input', 'field', 'text'])) {
if (element.props && element.props.name) {
tryMapFormField(element.props.name);
}
} else if (['input', 'textarea'].indexOf(element.type) > -1) {
if (element.props.type !== 'submit') {
tryMapFormField(element.props.name);
}
}
utils.mapFormField(element, tryMapFormField);
}

_spIfHandler(action, element) {
Expand Down
12 changes: 1 addition & 11 deletions src/components/UserProfileForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,7 @@ export default class UserProfileForm extends React.Component {
utils.getFieldValue(this.state.defaultFields, element.props.name) :
undefined;

if (typeof element.type === 'function' && utils.containsWord(element.type.name, ['input', 'field', 'text'])) {
if (element.props && element.props.name) {
tryMapField(element.props.name, defaultValue);
}
} else if (element.type === 'input') {
if (element.props.type === 'submit') {
return;
}

tryMapField(element.props.name, defaultValue);
}
utils.mapFormField(element, tryMapField, defaultValue);
}

_spIfHandler(action, element) {
Expand Down
39 changes: 39 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,29 @@ class Utils {
s4() + '-' + s4() + s4() + s4();
}

functionName(f) {
if (typeof f !== 'function') {
return '';
}

if (f.name) {
return f.name;
}

const parts = f.toString().match(/^function\s*([^\s(]+)/);

if (parts) {
return parts[1];
}

return '';
}

containsWord(testWord, words) {
if (typeof testWord !== 'string') {
return false;
}

testWord = testWord.toLowerCase();

for (let i = 0; i < words.length; i++) {
Expand All @@ -25,6 +47,11 @@ class Utils {
return false;
}

isInputLikeComponent(element, inputNames = ['input', 'field', 'text']) {
return typeof element.type === 'function'
&& this.containsWord(this.functionName(element.type), inputNames);
}

takeProp(source, ...fields) {
for (let i = 0; i < fields.length; i++) {
let fieldName = fields[i];
Expand Down Expand Up @@ -116,6 +143,18 @@ class Utils {
return React.cloneElement(newElement, newOptions, newChildren);
}

mapFormField(element, mappingFn, defaultValue) {
if (this.isInputLikeComponent(element)) {
if (element.props && element.props.name) {
mappingFn(element.props.name, defaultValue);
}
} else if (['input', 'textarea'].indexOf(element.type) > -1) {
if (element.props.type !== 'submit') {
mappingFn(element.props.name, defaultValue);
}
}
}

getFormFieldMap(root, handler) {
var fields = {};

Expand Down