GithubHelp home page GithubHelp logo

ampersandjs / ampersand-input-view Goto Github PK

View Code? Open in Web Editor NEW
16.0 16.0 19.0 130 KB

A view module for intelligently rendering and validating input. Works well with ampersand-form-view.

License: MIT License

JavaScript 100.00%

ampersand-input-view's People

Contributors

bear avatar cdaringe avatar dhritzkiv avatar henrikjoreteg avatar imorrison avatar kamilogorek avatar kasaimagi avatar klaemo avatar kole avatar latentflip avatar legastero avatar lukekarrys avatar mhamann avatar mmacaula avatar nickryall avatar pgilad avatar rickbutler avatar simme avatar taketwo avatar tnguyen14 avatar wesleysmyth avatar wraithgar avatar zearin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ampersand-input-view's Issues

Textarea template cannot be provided

It looks like if you set type to textarea, handleTypeChange simply replaces your <input> with a <textarea>. In my case, I'm using a CSS framework that requires class="textarea" be set on <textarea> elements for proper rendering.

It's not clear what the best solution here is. The only idea I've thought of so far is to define both an <input> and a <textarea> in the template and have handleTypeChange hide or remove the unnecessary one.

An alternative (and "for now" workaround) is to extend ampersand-input-view with a textarea-view class that has its own template.

Mark as invalid with message from server?

Is there an easy way to mark an ampersand-input-view as invalid with a serverside error message?

e.g. i have an registration-view with email and password fields and get serverside validation information for one or both.

getValue to mirror setValue

It would be nice to have a getValue that mirrors setValue to allow the form-view to aggregate field values.

setting `type` breaks Edge/IE

for example:

new InputView({
        name:        'assignmentDate',
        label:       'date',
        placeholder: 'mm/dd/yyyy',
        value:       this.model.assignmentDate,
        type:        'date',
        required:    true,
        parent:      this
      })

causes:

image

image

rootElementClass cannot track valid

Because rootElementClass is a prop and not derived, one can't override it to be something that can track the input's state like validityClass currently does. A lot of UI layouts have the entire label styled differently for error/valid state.

Tests run multiple times on input change

When validating an input field, the tests run multiple times per input change. runTests() is called 3 times per input change while that input is invalid, and then 7 times once a valid value starts (I'm just checking length).

This gist just logs a console message as a simple test: http://requirebin.com/?gist=a728e6caf6f7a0eff022, but if you start typing you'll see the tests run multiple times.

Removing cache: false on the valid derived property stops this from happening.

I feel like it should be the case where validation only happens one once per input change, but maybe there is something else requiring cache to be false.

Render should return `this`?

It's not an official view convention but I was wondering if the render method should follow ampersand-view and return this after it's done rendering. Seems confusing to not do so. I'd be happy to submit a PR with tests if you agree

`template` option is being ignored

In the ampersand example app I tried to pass this template:

<div class="pure-control-group">
  <div role="message-container">
    <p role="message-text"></p>
  </div>
  <label for="name" role="label"></label>
  <input id="name" type="text">
</div>

without any effect.

Allow labels to be explicitly associated with inputs (using for attribute)

Currently the labels supplied in the default template are implicitly associated with the inputs (using nesting).

<label>Name<input type="text" /></label>

It would be awesome to be able to associate them explicitly with a "for" attribute that references the id of the input through an option or by default. They are a little more robust than using nesting (and easier to style - when doing rtl layouts, needing to hide one without the other etc) and preferred in terms of accessibility.

<label for="InputId">Name</label> <input type="text" id="InputId" />

I know this should be easy enough to achieve although I wasn't sure whether there would be a preference on how the id property should be generated for this or whether there would ever be a need to pass them in? I'm wary of them having to be unique.

Change event doesn't fire on autofill

When filling out an input field, sometimes the browser presents me with past values. On accepting these values, the input does not have events fire, leaving the last value that was there.

Browser: Safari 7.0.4

Screenshot of value drop down:
screen shot 2014-07-10 at 6 16 56 pm

Screenshot of returned value via console (no event fired, either);
screen shot 2014-07-10 at 6 17 30 pm

Note: the field with the label Address 2 is bound to the property street2

Remote/async validation

Hey,
first of all great work with ampersand, really enjoying what I'm seeing so far.
I have one question though, is there a way to do validation calls that are async in nature, i.e. by doing a server request?

Input defined as `type: 'number'` doesn't accept the value 0

In a form view created by the CLI, I have added type: 'number' (ExtendedInput is the same as from the demo app):

            new ExtendedInput({
                label: "Order",
                name: "order",
                value: this.model && this.model.order,
                type: 'number',
                required: true,
                placeholder: "Order",
                parent: this
            })

The model's property is defined as such:

        order: ['number', true, 100]

The form displays nice little up and down arrow, and using the keyboard arrows changes the values appropriately.
Positive and negative values work fine, however when passing from -1 to 0, or from 1 to 0 the error message This field is required. is displayed.

It should be possible to enter a number with a value of 0.

Best way to modify InputView?

I'm trying out Ampersand by creating a simple chat page by modifying the sample app.

For this chat page, I have a message input field that I want to behave like InputView, but with two changes:

  • I don't want the field's validity to be checked on blur.
  • I want reset to set shouldValidate to false.

Right now I'm simply doing:

var InputView = require('ampersand-input-view');
// modify InputView to behave as we want it to
InputView.prototype.handleBlur = function() {};
InputView.prototype.reset = function() {
    this.setValue('');
    this.shouldValidate = false;
}

Is there a better way? I tried this with no success:

ExtendedInputView = InputView.extend({
    handleBlur: ...
    reset: ...
});

I'd also like to give the input an "autofocus: true" attribute, but the only ways I know how to do that would be to completely override the template, or wait until the input has been rendered, then do something like input_view.get('.form-input').setAttribute('autofocus', true);

One thing I was able to find out was how to override a derived property:

ExtendedInputView = InputView.extend({
    derived: {
        validityClass: {
            deps: ['valid', 'validClass', 'invalidClass', 'shouldValidate'],
            fn: function () {
                if (!this.shouldValidate) {
                    return '';
                } else {
                    return this.valid ? this.validClass : this.invalidClass;
                }
            }
        }
    }
});

At some point, it makes more sense to simply create my own version of InputView; I'd just like to know how much of what I want is possible without doing so.

number type is missing step argument

It is not possible to set step argument without overriding template. This could be handled same way that type change is handled now.

Step argument in input allows decimal number input.

Buggy type conversion

This is an input of type 'number' straight from the demo app.

new InputView({
  label: "Coolness Factor",
  name: "coolnessFactor",
  value: this.model && this.model.coolnessFactor,
  type: "number", 
})

If I focus input, enter some number and submit form it sends value as Number

{"id":11, "firstName": "Ivan", "lastName": "Kleshnin", "coolnessFactor": 1}

Lets then rerender page. If I now submit form (omitting manual input action) it sends value as String!

{"id": 11, "firstName": "Ivan", "lastName": "Kleshnin", "coolnessFactor": "1"}

It looks like, Ampersand-Input somehow binds it type conversion with input action which is obviously wrong but may be it's just a side-effect of something else. Documentation mentions there is a possibility to make value derived:

 derived: {
  value: {
    deps: ['...'],
    fn: function () {
      ...
    }
},

but I want to get basic conventions on this. I'm used to think that types should be set in declarative way and I don't want to define those deps for every field.

Initialize does not check for beforeSubmit

ampersand-form-view documentation states that the field views can optionally specify a beforeSubmit callback - however there is no room for this in the initialization options for ampersand-input-view. It would be nice to have this be available in ampersand-input-view's initialize method so that a user doesn't have to extend for each different type of input they have on a form.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.