Commit 06bb711e authored by Dominique Feyer's avatar Dominique Feyer
Browse files

TASK: More "real life" form-input

parent 0a240e7d
Loading
Loading
Loading
Loading
+206 −0
Original line number Diff line number Diff line
@@ -25,6 +25,12 @@ declare global {
  interface HTMLAttributes {}
}

import {
  EventEmitter,
} from '@stencil/core';
import {
  InputChangeEvent,
} from './interface';

declare global {

@@ -108,9 +114,97 @@ declare global {

  namespace StencilComponents {
    interface NeosFormInput {
      /**
       * If the value of the type attribute is `"file"`, then this attribute will indicate the types of files that the server accepts, otherwise it will be ignored. The value must be a comma-separated list of unique content type specifiers.
       */
      'accept': string;
      /**
       * Indicates whether the value of the control can be automatically completed by the browser. Defaults to `"off"`.
       */
      'autocomplete': string;
      /**
       * Whether autocorrection should be enabled when the user is entering/editing the text value. Defaults to `"off"`.
       */
      'autocorrect': string;
      /**
       * This Boolean attribute lets you specify that a form control should have input focus when the page loads. Defaults to `false`.
       */
      'autofocus': boolean;
      /**
       * If true, the value will be cleared after focus upon edit. Defaults to `true` when `type` is `"password"`, `false` for all other types.
       */
      'clearOnEdit': boolean;
      /**
       * If true, the user cannot interact with the input. Defaults to `false`.
       */
      'disabled': boolean;
      /**
       * A hint to the browser for which keyboard to display. This attribute applies when the value of the type attribute is `"text"`, `"password"`, `"email"`, or `"url"`. Possible values are: `"verbatim"`, `"latin"`, `"latin-name"`, `"latin-prose"`, `"full-width-latin"`, `"kana"`, `"katakana"`, `"numeric"`, `"tel"`, `"email"`, `"url"`.
       */
      'inputmode': string;
      /**
       * A descriptive label for the current input
       */
      'label': string;
      /**
       * The maximum value, which must not be less than its minimum (min attribute) value.
       */
      'max': string;
      /**
       * If the value of the type attribute is `text`, `email`, `search`, `password`, `tel`, or `url`, this attribute specifies the maximum number of characters that the user can enter.
       */
      'maxlength': number;
      /**
       * The minimum value, which must not be greater than its maximum (max attribute) value.
       */
      'min': string;
      /**
       * If the value of the type attribute is `text`, `email`, `search`, `password`, `tel`, or `url`, this attribute specifies the minimum number of characters that the user can enter.
       */
      'minlength': number;
      /**
       * If true, the user can enter more than one value. This attribute applies when the type attribute is set to `"email"` or `"file"`, otherwise it is ignored.
       */
      'multiple': boolean;
      /**
       * The name of the control, which is submitted with the form data.
       */
      'name': string;
      /**
       * A regular expression that the value is checked against. The pattern must match the entire value, not just some subset. Use the title attribute to describe the pattern to help the user. This attribute applies when the value of the type attribute is `"text"`, `"search"`, `"tel"`, `"url"`, `"email"`, or `"password"`, otherwise it is ignored.
       */
      'pattern': string;
      /**
       * Instructional text that shows before the input has a value.
       */
      'placeholder': string;
      /**
       * If true, the user cannot modify the value. Defaults to `false`.
       */
      'readonly': boolean;
      /**
       * If true, the user must fill in a value before submitting a form.
       */
      'required': boolean;
      /**
       * This is a nonstandard attribute supported by Safari that only applies when the type is `"search"`. Its value should be a nonnegative decimal integer.
       */
      'results': number;
      /**
       * The initial size of the control. This value is in pixels unless the value of the type attribute is `"text"` or `"password"`, in which case it is an integer number of characters. This attribute applies only when the `type` attribute is set to `"text"`, `"search"`, `"tel"`, `"url"`, `"email"`, or `"password"`, otherwise it is ignored.
       */
      'size': number;
      /**
       * If true, the element will have its spelling and grammar checked. Defaults to `false`.
       */
      'spellcheck': boolean;
      /**
       * Works with the min and max attributes to limit the increments at which a value can be set. Possible values are: `"any"` or a positive floating point number.
       */
      'step': string;
      /**
       * The type of control to display. The default type is text. Possible values are: `"text"`, `"password"`, `"email"`, `"number"`, `"search"`, `"tel"`, or `"url"`.
       */
      'type': string;
      'value': string;
    }
@@ -135,9 +229,121 @@ declare global {
  }
  namespace JSXElements {
    export interface NeosFormInputAttributes extends HTMLAttributes {
      /**
       * If the value of the type attribute is `"file"`, then this attribute will indicate the types of files that the server accepts, otherwise it will be ignored. The value must be a comma-separated list of unique content type specifiers.
       */
      'accept'?: string;
      /**
       * Indicates whether the value of the control can be automatically completed by the browser. Defaults to `"off"`.
       */
      'autocomplete'?: string;
      /**
       * Whether autocorrection should be enabled when the user is entering/editing the text value. Defaults to `"off"`.
       */
      'autocorrect'?: string;
      /**
       * This Boolean attribute lets you specify that a form control should have input focus when the page loads. Defaults to `false`.
       */
      'autofocus'?: boolean;
      /**
       * If true, the value will be cleared after focus upon edit. Defaults to `true` when `type` is `"password"`, `false` for all other types.
       */
      'clearOnEdit'?: boolean;
      /**
       * If true, the user cannot interact with the input. Defaults to `false`.
       */
      'disabled'?: boolean;
      /**
       * A hint to the browser for which keyboard to display. This attribute applies when the value of the type attribute is `"text"`, `"password"`, `"email"`, or `"url"`. Possible values are: `"verbatim"`, `"latin"`, `"latin-name"`, `"latin-prose"`, `"full-width-latin"`, `"kana"`, `"katakana"`, `"numeric"`, `"tel"`, `"email"`, `"url"`.
       */
      'inputmode'?: string;
      /**
       * A descriptive label for the current input
       */
      'label'?: string;
      /**
       * The maximum value, which must not be less than its minimum (min attribute) value.
       */
      'max'?: string;
      /**
       * If the value of the type attribute is `text`, `email`, `search`, `password`, `tel`, or `url`, this attribute specifies the maximum number of characters that the user can enter.
       */
      'maxlength'?: number;
      /**
       * The minimum value, which must not be greater than its maximum (max attribute) value.
       */
      'min'?: string;
      /**
       * If the value of the type attribute is `text`, `email`, `search`, `password`, `tel`, or `url`, this attribute specifies the minimum number of characters that the user can enter.
       */
      'minlength'?: number;
      /**
       * If true, the user can enter more than one value. This attribute applies when the type attribute is set to `"email"` or `"file"`, otherwise it is ignored.
       */
      'multiple'?: boolean;
      /**
       * The name of the control, which is submitted with the form data.
       */
      'name'?: string;
      /**
       * Emitted when the input loses focus.
       */
      'onNeosBlur'?: (event: CustomEvent<void>) => void;
      /**
       * Emitted when the value has changed.
       */
      'onNeosChange'?: (event: CustomEvent<InputChangeEvent>) => void;
      /**
       * Emitted when the input has focus.
       */
      'onNeosFocus'?: (event: CustomEvent<void>) => void;
      /**
       * Emitted when a keyboard input ocurred.
       */
      'onNeosInput'?: (event: CustomEvent<KeyboardEvent>) => void;
      /**
       * Emitted when the input has been created.
       */
      'onNeosInputDidLoad'?: (event: CustomEvent<void>) => void;
      /**
       * Emitted when the input has been removed.
       */
      'onNeosInputDidUnload'?: (event: CustomEvent<void>) => void;
      /**
       * A regular expression that the value is checked against. The pattern must match the entire value, not just some subset. Use the title attribute to describe the pattern to help the user. This attribute applies when the value of the type attribute is `"text"`, `"search"`, `"tel"`, `"url"`, `"email"`, or `"password"`, otherwise it is ignored.
       */
      'pattern'?: string;
      /**
       * Instructional text that shows before the input has a value.
       */
      'placeholder'?: string;
      /**
       * If true, the user cannot modify the value. Defaults to `false`.
       */
      'readonly'?: boolean;
      /**
       * If true, the user must fill in a value before submitting a form.
       */
      'required'?: boolean;
      /**
       * This is a nonstandard attribute supported by Safari that only applies when the type is `"search"`. Its value should be a nonnegative decimal integer.
       */
      'results'?: number;
      /**
       * The initial size of the control. This value is in pixels unless the value of the type attribute is `"text"` or `"password"`, in which case it is an integer number of characters. This attribute applies only when the `type` attribute is set to `"text"`, `"search"`, `"tel"`, `"url"`, `"email"`, or `"password"`, otherwise it is ignored.
       */
      'size'?: number;
      /**
       * If true, the element will have its spelling and grammar checked. Defaults to `false`.
       */
      'spellcheck'?: boolean;
      /**
       * Works with the min and max attributes to limit the increments at which a value can be set. Possible values are: `"any"` or a positive floating point number.
       */
      'step'?: string;
      /**
       * The type of control to display. The default type is text. Possible values are: `"text"`, `"password"`, `"email"`, `"number"`, `"search"`, `"tel"`, or `"url"`.
       */
      'type'?: string;
      'value'?: string;
    }
+265 −18
Original line number Diff line number Diff line
import {Component, Prop} from '@stencil/core';
import {Component, Prop, Watch, Element, Event, EventEmitter} from '@stencil/core';
import {InputChangeEvent} from "../../interface";

@Component({
  tag: 'neos-form-input',
@@ -7,22 +8,242 @@ import {Component, Prop} from '@stencil/core';
})
export class FormInput {

  @Prop() placeholder: string;
  @Prop() value: string;
  private nativeInput: HTMLInputElement | undefined;
  didBlurAfterEdit = false;

  @Element() el!: HTMLElement;

  /**
   * Emitted when a keyboard input ocurred.
   */
  @Event() neosInput!: EventEmitter<KeyboardEvent>;

  /**
   * Emitted when the value has changed.
   */
  @Event() neosChange!: EventEmitter<InputChangeEvent>;

  /**
   * Emitted when the input loses focus.
   */
  @Event() neosBlur!: EventEmitter<void>;

  /**
   * Emitted when the input has focus.
   */
  @Event() neosFocus!: EventEmitter<void>;

  /**
   * Emitted when the input has been created.
   */
  @Event() neosInputDidLoad!: EventEmitter<void>;

  /**
   * Emitted when the input has been removed.
   */
  @Event() neosInputDidUnload!: EventEmitter<void>;

  /**
   * If the value of the type attribute is `"file"`, then this attribute will indicate the types of files that the server accepts, otherwise it will be ignored. The value must be a comma-separated list of unique content type specifiers.
   */
  @Prop() accept?: string;

  /**
   * Indicates whether the value of the control can be automatically completed by the browser. Defaults to `"off"`.
   */
  @Prop() autocomplete = 'off';

  /**
   * Whether autocorrection should be enabled when the user is entering/editing the text value. Defaults to `"off"`.
   */
  @Prop() autocorrect = 'off';

  /**
   * This Boolean attribute lets you specify that a form control should have input focus when the page loads. Defaults to `false`.
   */
  @Prop() autofocus = false;


  /**
   * If true, the user cannot interact with the input. Defaults to `false`.
   */
  @Prop() disabled = false;

  /**
   * If true, the value will be cleared after focus upon edit. Defaults to `true` when `type` is `"password"`, `false` for all other types.
   */
  @Prop({mutable: true}) clearOnEdit!: boolean;

  /**
   * A descriptive label for the current input
   */
  @Prop() label: string;
  @Prop() type: string = 'text';
  @Prop() disabled: boolean = false;

  renderInput() {
    return (<input
      placeholder={this.placeholder}
      type={this.type}
      disabled={this.disabled}
      value={this.value}
    />)
  /**
   * A hint to the browser for which keyboard to display. This attribute applies when the value of the type attribute is `"text"`, `"password"`, `"email"`, or `"url"`. Possible values are: `"verbatim"`, `"latin"`, `"latin-name"`, `"latin-prose"`, `"full-width-latin"`, `"kana"`, `"katakana"`, `"numeric"`, `"tel"`, `"email"`, `"url"`.
   */
  @Prop() inputmode?: string;

  /**
   * The maximum value, which must not be less than its minimum (min attribute) value.
   */
  @Prop() max?: string;

  /**
   * If the value of the type attribute is `text`, `email`, `search`, `password`, `tel`, or `url`, this attribute specifies the maximum number of characters that the user can enter.
   */
  @Prop() maxlength?: number;

  /**
   * The minimum value, which must not be greater than its maximum (max attribute) value.
   */
  @Prop() min?: string;

  /**
   * If the value of the type attribute is `text`, `email`, `search`, `password`, `tel`, or `url`, this attribute specifies the minimum number of characters that the user can enter.
   */
  @Prop() minlength?: number;

  /**
   * If true, the user can enter more than one value. This attribute applies when the type attribute is set to `"email"` or `"file"`, otherwise it is ignored.
   */
  @Prop() multiple?: boolean;

  /**
   * The name of the control, which is submitted with the form data.
   */
  @Prop() name?: string;

  /**
   * A regular expression that the value is checked against. The pattern must match the entire value, not just some subset. Use the title attribute to describe the pattern to help the user. This attribute applies when the value of the type attribute is `"text"`, `"search"`, `"tel"`, `"url"`, `"email"`, or `"password"`, otherwise it is ignored.
   */
  @Prop() pattern?: string;

  /**
   * Instructional text that shows before the input has a value.
   */
  @Prop() placeholder?: string;

  /**
   * If true, the user cannot modify the value. Defaults to `false`.
   */
  @Prop() readonly = false;

  /**
   * If true, the user must fill in a value before submitting a form.
   */
  @Prop() required = false;

  /**
   * This is a nonstandard attribute supported by Safari that only applies when the type is `"search"`. Its value should be a nonnegative decimal integer.
   */
  @Prop() results?: number;

  /**
   * If true, the element will have its spelling and grammar checked. Defaults to `false`.
   */
  @Prop() spellcheck = false;

  /**
   * Works with the min and max attributes to limit the increments at which a value can be set. Possible values are: `"any"` or a positive floating point number.
   */
  @Prop() step?: string;

  /**
   * The initial size of the control. This value is in pixels unless the value of the type attribute is `"text"` or `"password"`, in which case it is an integer number of characters. This attribute applies only when the `type` attribute is set to `"text"`, `"search"`, `"tel"`, `"url"`, `"email"`, or `"password"`, otherwise it is ignored.
   */
  @Prop() size?: number;

  /**
   * The type of control to display. The default type is text. Possible values are: `"text"`, `"password"`, `"email"`, `"number"`, `"search"`, `"tel"`, or `"url"`.
   */
  @Prop() type = 'text';


  @Prop({mutable: true}) value = '';

  /**
   * Update the native input element when the value changes
   */
  @Watch('value')
  protected valueChanged() {
    const inputEl = this.nativeInput;
    const value = this.value;
    if (inputEl && inputEl.value !== value) {
      inputEl.value = value;
    }
    this.neosChange.emit({value});
  }

  renderInputWithLabel() {

  componentDidUnload() {
    this.nativeInput = undefined;
    this.neosInputDidUnload.emit();
  }

  private onInput(ev: KeyboardEvent) {
    const input = ev.target as HTMLInputElement;
    if (input) {
      this.value = ev.target && (ev.target as HTMLInputElement).value || '';
    }
    this.neosInput.emit(ev);
  }

  private onBlur() {
    this.focusChanged();

    this.neosBlur.emit();
  }

  private onFocus() {
    this.focusChanged();

    this.neosFocus.emit();
  }

  private focusChanged() {
    // If clearOnEdit is enabled and the input blurred but has a value, set a flag
    if (this.clearOnEdit && !this.hasFocus() && this.hasValue()) {
      this.didBlurAfterEdit = true;
    }
  }

  private inputKeydown() {
    this.checkClearOnEdit();
  }

  /**
   * Check if we need to clear the text input if clearOnEdit is enabled
   */
  private checkClearOnEdit() {
    if (!this.clearOnEdit) {
      return;
    }

    // Did the input value change after it was blurred and edited?
    if (this.didBlurAfterEdit && this.hasValue()) {
      // Clear the input
      this.clearTextInput();
    }

    // Reset the flag
    this.didBlurAfterEdit = false;
  }

  private clearTextInput() {
    this.value = '';
  }

  private hasFocus(): boolean {
    // check if an input has focus or not
    return this.nativeInput === document.activeElement;
  }

  private hasValue(): boolean {
    return (this.value !== '');
  }

  render() {
    return (
      <label>
        <div class="label">
@@ -33,14 +254,40 @@ export class FormInput {
        </div>
        <div class="input">
          <slot name="before"/>
          {this.renderInput()}
          <input
            ref={input => this.nativeInput = input as any}
            aria-disabled={this.disabled ? 'true' : false}
            accept={this.accept}
            autoComplete={this.autocomplete}
            autoCorrect={this.autocorrect}
            autoFocus={this.autofocus}
            disabled={this.disabled}
            inputMode={this.inputmode}
            min={this.min}
            max={this.max}
            minLength={this.minlength}
            maxLength={this.maxlength}
            multiple={this.multiple}
            name={this.name}
            pattern={this.pattern}
            placeholder={this.placeholder}
            results={this.results}
            readOnly={this.readonly}
            required={this.required}
            spellCheck={this.spellcheck}
            step={this.step}
            size={this.size}
            type={this.type}
            value={this.value}

            onInput={this.onInput.bind(this)}
            onBlur={this.onBlur.bind(this)}
            onFocus={this.onFocus.bind(this)}
            onKeyDown={this.inputKeydown.bind(this)}
          />
          <slot name="after"/>
        </div>
      </label>
    );
  }

  render() {
    return this.label ? this.renderInputWithLabel() : this.renderInput();
  }
}
+10 −0
Original line number Diff line number Diff line
@@ -19,4 +19,14 @@
    background-color: neos-color($colors-dark, 'success');
    color: neos-color($colors-dark, 'success', 'contrast');
  }

  .t-warning {
    background-color: neos-color($colors-dark, 'warning');
    color: neos-color($colors-dark, 'warning', 'contrast');
  }

  .t-danger {
    background-color: neos-color($colors-dark, 'danger');
    color: neos-color($colors-dark, 'danger', 'contrast');
  }
}
+4 −0
Original line number Diff line number Diff line
@@ -234,6 +234,10 @@
        <neos-icon squared name="at" type="solid" theme="neutral" slot="before"></neos-icon>
        <neos-icon squared name="check-circle" type="solid" theme="success" slot="after"></neos-icon>
      </neos-form-input>
      <neos-form-input label="Alternative Email" value="hello@neos">
        <neos-icon squared name="at" type="solid" theme="neutral" slot="before"></neos-icon>
        <neos-icon squared name="times-circle" type="solid" theme="danger" slot="after"></neos-icon>
      </neos-form-input>
    </neos-inspector-group>
    <neos-inspector-group icon="cogs" label="Ownership" closed>
      <neos-form-input label="Author"></neos-form-input>

src/interface.d.ts

0 → 100644
+1 −0
Original line number Diff line number Diff line
export * from './utils/input-interface';
Loading