From 9d88293a1f8647336db92b768d0ba0477c0bfe26 Mon Sep 17 00:00:00 2001 From: Tim Brayshaw Date: Wed, 20 Jul 2016 10:36:40 +0100 Subject: [PATCH] Add getInputIndex method This gives a formsy-react component the ability to get the index in which it is registered within the form. This index is unique to that component, and is useful for use in generating id attributes for elements within the component. --- src/Decorator.js | 1 + src/HOC.js | 1 + src/Mixin.js | 3 +++ src/main.js | 6 ++++++ 4 files changed, 11 insertions(+) diff --git a/src/Decorator.js b/src/Decorator.js index f9a4b35d..0fe64f50 100644 --- a/src/Decorator.js +++ b/src/Decorator.js @@ -13,6 +13,7 @@ module.exports = function () { hasValue: this.hasValue, getErrorMessage: this.getErrorMessage, getErrorMessages: this.getErrorMessages, + getInputIndex: this.getInputIndex, isFormDisabled: this.isFormDisabled, isValid: this.isValid, isPristine: this.isPristine, diff --git a/src/HOC.js b/src/HOC.js index e8717a6d..54139a3f 100644 --- a/src/HOC.js +++ b/src/HOC.js @@ -13,6 +13,7 @@ module.exports = function (Component) { hasValue: this.hasValue, getErrorMessage: this.getErrorMessage, getErrorMessages: this.getErrorMessages, + getInputIndex: this.getInputIndex, isFormDisabled: this.isFormDisabled, isValid: this.isValid, isPristine: this.isPristine, diff --git a/src/Mixin.js b/src/Mixin.js index d615fc7a..e118ece0 100644 --- a/src/Mixin.js +++ b/src/Mixin.js @@ -171,5 +171,8 @@ module.exports = { isValidValue: function (value) { return this.context.formsy.isValidValue.call(null, this, value); //return this.props._isValidValue.call(null, this, value); + }, + getInputIndex: function () { + return this.context.formsy.getInputIndex(this); } }; diff --git a/src/main.js b/src/main.js index f7b5e8eb..23297a16 100644 --- a/src/main.js +++ b/src/main.js @@ -53,6 +53,7 @@ Formsy.Form = React.createClass({ formsy: { attachToForm: this.attachToForm, detachFromForm: this.detachFromForm, + getInputIndex: this.getInputIndex, validate: this.validate, isFormDisabled: this.isFormDisabled, isValidValue: (component, value) => { @@ -422,6 +423,11 @@ Formsy.Form = React.createClass({ this.validateForm(); }, + + getInputIndex: function (component) { + return this.inputs.indexOf(component); + }, + render: function () { var { mapping,