Giter Club home page Giter Club logo

input-autogrow's Introduction

input-autogrow

NPM Version NPM Downloads Buy Me a Coffee

Input-Autogrow is a plugin for autogrowing inputs. This plugin is different from others because most usually target textarea tags, this library is instead targeted at input tags.

Requires a DOM library such as jQuery, Zepto, or any that supports $.fn plugins.

Demo

https://jsfiddle.net/xpvt214o/50743/

Install

NPM, Yarn, or Bower

npm install input-autogrow

yarn add input-autogrow

bower install input-autogrow

CDN

<script src="https://cdnjs.cloudflare.com/ajax/libs/input-autogrow/1.1.1/input-autogrow.min.js"></script>

Rails-Assets

# Gemfile
source 'https://rails-assets.org' do
  gem 'rails-assets-input-autogrow'
end


# app/assets/javascripts/application.js
/*
 *= require input-autogrow
 */

Usage

/* Makes input elements autogrow */
$('input.autogrow').inputAutogrow();

/* Manually trigger update */
$('input.autogrow').trigger('autogrow');
/* or */
$('input.autogrow').trigger('change');

/* Custom Options */
$('input.autogrow').inputAutogrow({maxWidth: 500, minWidth: 25, trailingSpace: 10});

/* Remove autogrow from input */
$('input.autogrow').inputAutogrow('destroy');

/* If the parent container width is changed (and maxWidth is not specified)
   OR the options need to be changed, then you must destroy & re-initialize inputAutogrow */
$('input.autogrow').inputAutogrow('destroy');
$('input.autogrow').inputAutogrow();

Options

maxWidth - The max width of the autogrow - Defaults to the inputs parent width

minWidth - The min width of the autogrow - Defaults to the inputs original input start width

trailingSpace - This is extra spacing at the end of the input which is used until the maxWidth is hit - Default: 0

Contributing

  • Only edit js files from src/ and test/ folders.
  • To run tests: npm run test
  • Remember to run gulp to run the dist build after your done adding and testing your changes.

Credits

Created & Maintained by Weston Ganger - @westonganger

Originally based on this Stackoverflow answer by James Padolsey

input-autogrow's People

Contributors

graue avatar westonganger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

input-autogrow's Issues

"change" event is removed and can therefore not be listened to by other elements

Is seems this line

input.off(event + '.autogrow autogrow change').on(event+'.autogrow autogrow change', check);
removes the "change" event and directly re-adds it and assigns the check function to listen to it.

This approach causes that only the (internal) check function will be able to trigger to this event. Is there a way to have other elements informed of a changed value?

Rationale
We got some HTML5 data-attributes which are configured to "listen" to changes of an input element and submitted as values to a backend. However, when the change event can not be listened to, this approach fails.

For now using https://github.com/westonganger/input-autogrow/releases/tag/v0.9.0 which does not remove / involve the change event resolves / circumvents this limitation.

Request: please tag 1.0.3 - installation fails due to version tags on master

In e07468c#diff-b9cfc7f2cdf78a7f4b91a753d10865a2 the package.json got a tag 1.0.3. However that release does not exist (yet) https://github.com/westonganger/input-autogrow/releases

The following dependency in a composer.json file combined with the composer-asset-plugin:

"bower-asset/input-autogrow": "^1.0"

resolves to installing the master version, instead of the latest stable release 1.0.2.

Can you tag the current master branch as 1.0.3 to resolve this?

"ReferenceError: input is not defined" when calling destroy

On line 22, input is not defined because it lives inside the previous each block. I'm guessing this should instead be $(this).data('original-width','');

    if(options && (options === 'destroy' || $(this).data('autogrow-span'))){
      this.each(function() {
        var input = $(this);
        input.off(events);
        var span = input.data("autogrow-span");
        span.remove();
        input.data('autogrow-span','');
      });

      if(options === 'destroy'){
        input.data('original-width',''); // <--- here
        return this;
      }
    }

Was banging my head against the wall for hours trying to figure out why this wasn't working.

Rails and jQuery autocomplete bug

When the gem is installed causes this error stack
Uncaught TypeError: elem.getClientRects is not a function at jQuery.fn.init.offset (http://localhost:3000/assets/jquery/jquery.self-355640bfbbb3239b9bb16d6795e41d526eeffc2eff3253d494fa3f58e2c3177c.js?body=1:9961:14) at Object.getWithinInfo (http://localhost:3000/assets/jquery-ui/position.self-e693ced4ecfa1a276f0b794f32c1d30d792764b08788bf68ecfa3b388c291333.js?body=1:124:26) at jQuery.fn.init.$.fn.position (http://localhost:3000/assets/jquery-ui/position.self-e693ced4ecfa1a276f0b794f32c1d30d792764b08788bf68ecfa3b388c291333.js?body=1:146:23) at $.(anonymous function).(anonymous function)._suggest (http://localhost:3000/assets/jquery-ui/autocomplete.self-3befc48aba87cead090cfd014562b2771a662ac6d2c8197b24c08b2d9f3d9f2d.js?body=1:520:6) at $.(anonymous function).(anonymous function)._suggest (http://localhost:3000/assets/jquery-ui/widget.self-c1602241ddc51216b58391768667068867b8e15b9efc722befcd25771eda6819.js?body=1:116:25) at $.(anonymous function).(anonymous function).__response (http://localhost:3000/assets/jquery-ui/autocomplete.self-3befc48aba87cead090cfd014562b2771a662ac6d2c8197b24c08b2d9f3d9f2d.js?body=1:464:9) at $.(anonymous function).(anonymous function).__response (http://localhost:3000/assets/jquery-ui/widget.self-c1602241ddc51216b58391768667068867b8e15b9efc722befcd25771eda6819.js?body=1:116:25) at $.(anonymous function).(anonymous function)._superApply (http://localhost:3000/assets/jquery-ui/widget.self-c1602241ddc51216b58391768667068867b8e15b9efc722befcd25771eda6819.js?body=1:106:36) at $.(anonymous function).(anonymous function).__response (http://localhost:3000/assets/jquery-ui/autocomplete.self-3befc48aba87cead090cfd014562b2771a662ac6d2c8197b24c08b2d9f3d9f2d.js?body=1:618:8) at $.(anonymous function).(anonymous function).__response (http://localhost:3000/assets/jquery-ui/widget.self-c1602241ddc51216b58391768667068867b8e15b9efc722befcd25771eda6819.js?body=1:116:25)

Without even including *= require input-autogrow into the js file or creating an instance. Also, breaks jQuery UI Autocomplete widget.

Add to CDN for easy deployment on production environments

To use the JavaScript functionality (thanks for creating and maintaining it by the way ;)) on a production environment, hosting on a CDN (with focus on JavaScript packages) would be nice instead of hosting it ourselves.

For the time being, RawGit suits fine, this converts a GitHub URL to a CDN like approach:
https://cdn.rawgit.com/westonganger/input-autogrow/vX.Y.Z/dist/input-autogrow.min.js, for example:

https://cdn.rawgit.com/westonganger/input-autogrow/v1.0.2/dist/input-autogrow.min.js

But maybe you can consider registering it at something like www.cdnjs.com?

Breaks when initial value is single space

When $(...).inputAutogrow() is called on an input whose value is a single space, the code throws because it tries to read e.type in check, which has been called initially with no argument.

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.