Skip to content

Commit

Permalink
add placeholder option
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasCARPi committed Dec 15, 2021
1 parent 9de0a6c commit 47f4350
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ <h2>Selecting behavior on Enter keypress</h2>
<p><code>data-ma-enter='cancel'</code><br><span data-malleable='true' data-ma-enter='cancel'>Pressing Enter will cancel edition.</span></p>
<p><code>data-ma-enter='submit'</code><br><span data-malleable='true' data-ma-enter='submit'>Pressing Enter will submit changes.</span></p>
<p><code>data-ma-enter='ignore'</code><br><span data-malleable='true' data-ma-enter='ignore'>Pressing Enter will do nothing.</span></p>
<h2>Adding a placeholder</h2>
<p><code>data-ma-placeholder='[email protected]'</code><br>Your email is: <span data-malleable='true' data-ma-placeholder='[email protected]' data-ma-type='email'>[email protected]</span></p>
</div>

<script src='main.js' type='module'></script>
Expand Down
11 changes: 11 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface Options {
onBlur?: Action;
onEdit?(original: HTMLElement, event:Event, input: HTMLInputElement): boolean;
onEnter?: Action;
placeholder?: string;
selectOptions?: Array<SelectOptions>;
submit?: string;
submitClasses?: Array<string>;
Expand Down Expand Up @@ -89,6 +90,7 @@ export class Malle {
onBlur: Action.Submit,
onEdit: undefined,
onEnter: Action.Submit,
placeholder: '',
selectOptions: [],
submit: '',
submitClasses: [],
Expand Down Expand Up @@ -208,6 +210,15 @@ export class Malle {
// the value of the input is the current text of the original element
input.value = this.original.innerText;

// PLACEHOLDER
if (this.opt.placeholder) {
input.placeholder = this.opt.placeholder;
}
// data-ma-placeholder will override the option
if (this.original.dataset.maPlaceholder) {
input.placeholder = this.original.dataset.maPlaceholder;
}

// add options for a select
if (this.opt.inputType === InputType.Select) {
this.opt.selectOptions.forEach(o => {
Expand Down

0 comments on commit 47f4350

Please sign in to comment.