All files / components/atajo/atajo-notes-and-flags atajo-notes-and-flags.tsx

47.62% Statements 10/21
0% Branches 0/4
25% Functions 3/12
47.62% Lines 10/21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 782x   2x 2x                     2x           2x 2x 2x       2x       2x                                     1x                                                        
import { Component, Prop, Watch } from "@stencil/core";
import { iConfig } from "./atajo-notes-and-flags.interface";
import { defaultNote } from "./atajo-notes-and-flags.structure";
import Storage from "../../../services/storage";
 
/**
 * A component to create notes and flags
 *
 * Basic usage example:
 * ```html
 *  <script src="https://components.atajo.io/atajo-web-components.js"></script>
 *  <atajo-notes-and-flags></atajo-notes-and-flags>
 * ```
 */
@Component({
  tag: "atajo-notes-and-flags",
  styleUrl: "atajo-notes-and-flags.scss",
  shadow: true
})
export class AtajoNotesAndFlagsComponent {
  @Prop() labelCancel: string = 'Cancel';
  @Prop() labelSave: string = 'Save';
  @Prop({ mutable: true }) config: iConfig = defaultNote;
 
  storage: Storage;
  constructor() {
    this.storage = new Storage('atajo-notes-and-flags');
  }
 
  createNewNoteOrFlag() {
    this.storage.insert(this.config);
  }
 
  inputChanged(sectionIndex, fieldId, event) {
    this.config.sections[sectionIndex].fields.forEach(field => {
      if(field.id == fieldId) {
        field['value'] = event.target.value;
      }
      if(field.isTitle) {
        this.config.title = field['value'];
      }
    });
  }
 
  clickCancel() {
    // TODO: Handle cancel event
  }
 
  clickSave() {
    this.createNewNoteOrFlag();
  }
 
  render() {
    return (
      <atajo-card>
        {
          this.config.sections.map((section, sectionIndex) => {
            return section.fields.map((field) =>
              <atajo-textfield
                id={field.id}
                type={field.type}
                label={field.label}
                value={field.value}
                disabled={field.isDisabled}
                onInput={(event) => this.inputChanged(sectionIndex, field.id, event)}
              ></atajo-textfield>
            )
          })
        }
        <atajo-card-actions>
          <atajo-button onClick={() => this.clickCancel()}>{this.labelCancel}</atajo-button>
          <atajo-button raised onClick={() => this.clickSave()}>{this.labelSave}</atajo-button>
        </atajo-card-actions>
      </atajo-card>
    );
  }
}