From ad5c2818c025695529a71540057986d99c6c438b Mon Sep 17 00:00:00 2001 From: SachaG Date: Wed, 7 Jun 2017 17:59:02 -0700 Subject: [PATCH] Add new limit custom property to show character count in form fields --- .../lib/stylesheets/_forms.scss | 16 ++++++++++++ packages/vulcan-forms/lib/Form.jsx | 5 ++++ packages/vulcan-forms/lib/FormComponent.jsx | 26 +++++++++++++++---- .../vulcan-lib/lib/modules/collections.js | 14 +--------- packages/vulcan-lib/lib/modules/config.js | 5 ++++ 5 files changed, 48 insertions(+), 18 deletions(-) diff --git a/packages/vulcan-base-styles/lib/stylesheets/_forms.scss b/packages/vulcan-base-styles/lib/stylesheets/_forms.scss index b0c919e33..5f784eea7 100644 --- a/packages/vulcan-base-styles/lib/stylesheets/_forms.scss +++ b/packages/vulcan-base-styles/lib/stylesheets/_forms.scss @@ -111,4 +111,20 @@ div.ReactTags__suggestions mark{ right: 5px; color: $light-grey; } +} + +.form-input{ + position: relative; +} +.form-control-limit{ + position: absolute; + background: white; + padding: 5px; + bottom: 5px; + right: 5px; + color: $light-grey; + font-size: 80%; + &.danger{ + color: #EF1642; + } } \ No newline at end of file diff --git a/packages/vulcan-forms/lib/Form.jsx b/packages/vulcan-forms/lib/Form.jsx index cad720ed7..d553c8da2 100644 --- a/packages/vulcan-forms/lib/Form.jsx +++ b/packages/vulcan-forms/lib/Form.jsx @@ -164,6 +164,11 @@ class Form extends Component { field.help = typeof fieldSchema.form.help === "function" ? fieldSchema.form.help.call(fieldSchema) : fieldSchema.form.help; } + // add limit + if (fieldSchema.limit) { + field.limit = fieldSchema.limit; + } + // add placeholder if (fieldSchema.placeholder) { field.placeholder = fieldSchema.placeholder; diff --git a/packages/vulcan-forms/lib/FormComponent.jsx b/packages/vulcan-forms/lib/FormComponent.jsx index a2c54b4ee..8a5817712 100644 --- a/packages/vulcan-forms/lib/FormComponent.jsx +++ b/packages/vulcan-forms/lib/FormComponent.jsx @@ -1,9 +1,9 @@ import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; -import Formsy from 'formsy-react'; import FRC from 'formsy-react-components'; import { intlShape } from 'meteor/vulcan:i18n'; import DateTime from './DateTime.jsx'; +import classNames from 'classnames'; // import Utils from './utils.js'; @@ -19,6 +19,13 @@ class FormComponent extends PureComponent { constructor(props) { super(props); this.handleBlur = this.handleBlur.bind(this); + this.updateCharacterCount = this.updateCharacterCount.bind(this); + + if (props.limit) { + this.state = { + limit: props.value ? props.limit - props.value.length : props.limit + } + } } handleBlur() { @@ -28,10 +35,18 @@ class FormComponent extends PureComponent { } } + updateCharacterCount(name, value) { + if (this.props.limit) { + this.setState({ + limit: this.props.limit - value.length + }); + } + } + renderComponent() { // see https://facebook.github.io/react/warnings/unknown-prop.html - const { control, group, updateCurrentValues, document, beforeComponent, afterComponent, ...rest } = this.props; // eslint-disable-line + const { control, group, updateCurrentValues, document, beforeComponent, afterComponent, limit, ...rest } = this.props; // eslint-disable-line // const base = typeof this.props.control === "function" ? this.props : rest; @@ -50,9 +65,9 @@ class FormComponent extends PureComponent { switch (this.props.control) { case "text": - return ; + return ; case "textarea": - return