149 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			149 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<!--
 | 
						|
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
 | 
						|
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE
 | 
						|
The complete set of authors may be found at http://polymer.github.io/AUTHORS
 | 
						|
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS
 | 
						|
Code distributed by Google as part of the polymer project is also
 | 
						|
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS
 | 
						|
-->
 | 
						|
 | 
						|
<!--
 | 
						|
 | 
						|
`core-input` is an unstyled single-line input field. It extends the native
 | 
						|
`input` element.
 | 
						|
 | 
						|
Example:
 | 
						|
 | 
						|
    <input is="core-input">
 | 
						|
 | 
						|
The input's value is considered "committed" if the user hits the "enter" key
 | 
						|
or blurs the input after changing the value. The committed value is stored in
 | 
						|
the `committedValue` property.
 | 
						|
 | 
						|
If the input has `type = number`, this element will also prevent non-numeric characters
 | 
						|
from being typed into the text field.
 | 
						|
 | 
						|
Accessibility
 | 
						|
-------------
 | 
						|
 | 
						|
The following ARIA attributes are set automatically:
 | 
						|
 | 
						|
- `aria-label`: set to the `placeholder` attribute
 | 
						|
- `aria-disabled`: set if `disabled` is true
 | 
						|
 | 
						|
@group Polymer Core Elements
 | 
						|
@element core-input
 | 
						|
@extends input
 | 
						|
@homepage github.io
 | 
						|
-->
 | 
						|
<link href="../polymer/polymer.html" rel="import">
 | 
						|
 | 
						|
<style shim-shadowdom>
 | 
						|
  /* FIXME consider theming */
 | 
						|
 | 
						|
  html /deep/ input[is=core-input] {
 | 
						|
    width: 20em;
 | 
						|
    font: inherit;
 | 
						|
    margin: 0;
 | 
						|
    padding: 0;
 | 
						|
    background-color: transparent;
 | 
						|
    border: 0;
 | 
						|
    outline: none;
 | 
						|
  }
 | 
						|
</style>
 | 
						|
 | 
						|
<polymer-element name="core-input" extends="input">
 | 
						|
 | 
						|
<script>
 | 
						|
 | 
						|
  Polymer('core-input', {
 | 
						|
 | 
						|
    publish: {
 | 
						|
 | 
						|
      /**
 | 
						|
       * The "committed" value of the input, ie. the input value when the user
 | 
						|
       * hits the "enter" key or blurs the input after changing the value. You
 | 
						|
       * can bind to this value instead of listening for the "change" event.
 | 
						|
       * Setting this property has no effect on the input value.
 | 
						|
       *
 | 
						|
       * @attribute committedValue
 | 
						|
       * @type string
 | 
						|
       * @default ''
 | 
						|
       */
 | 
						|
      committedValue: '',
 | 
						|
 | 
						|
      /**
 | 
						|
       * Set to true to prevent invalid input from being entered.
 | 
						|
       *
 | 
						|
       * @attribute preventInvalidInput
 | 
						|
       * @type boolean
 | 
						|
       * @default false
 | 
						|
       */
 | 
						|
      preventInvalidInput: false
 | 
						|
 | 
						|
    },
 | 
						|
 | 
						|
    previousValidInput: '',
 | 
						|
 | 
						|
    eventDelegates: {
 | 
						|
      input: 'inputAction',
 | 
						|
      change: 'changeAction'
 | 
						|
    },
 | 
						|
 | 
						|
    ready: function() {
 | 
						|
      /* set ARIA attributes */
 | 
						|
      this.disabledHandler();
 | 
						|
      this.placeholderHandler();
 | 
						|
    },
 | 
						|
 | 
						|
    attributeChanged: function(attr, old) {
 | 
						|
      if (this[attr + 'Handler']) {
 | 
						|
        this[attr + 'Handler'](old);
 | 
						|
      }
 | 
						|
    },
 | 
						|
 | 
						|
    disabledHandler: function() {
 | 
						|
      if (this.disabled) {
 | 
						|
        this.setAttribute('aria-disabled', '');
 | 
						|
      } else {
 | 
						|
        this.removeAttribute('aria-disabled');
 | 
						|
      }
 | 
						|
    },
 | 
						|
 | 
						|
    placeholderHandler: function() {
 | 
						|
      if (this.placeholder) {
 | 
						|
        this.setAttribute('aria-label', this.placeholder);
 | 
						|
      } else {
 | 
						|
        this.removeAttribute('aria-label');
 | 
						|
      }
 | 
						|
    },
 | 
						|
 | 
						|
    /**
 | 
						|
     * Commits the `value` to `committedValue`
 | 
						|
     *
 | 
						|
     * @method commit
 | 
						|
     */
 | 
						|
    commit: function() {
 | 
						|
      this.committedValue = this.value;
 | 
						|
    },
 | 
						|
 | 
						|
    changeAction: function() {
 | 
						|
      this.commit();
 | 
						|
    },
 | 
						|
 | 
						|
    inputAction: function(e) {
 | 
						|
      if (this.preventInvalidInput) {
 | 
						|
        if (!e.target.validity.valid) {
 | 
						|
          e.target.value = this.previousValidInput;
 | 
						|
        } else {
 | 
						|
          this.previousValidInput = e.target.value;
 | 
						|
        }
 | 
						|
      }
 | 
						|
    }
 | 
						|
 | 
						|
  });
 | 
						|
 | 
						|
</script>
 | 
						|
 | 
						|
</polymer-element>
 |