• Stars
    star
    252
  • Rank 155,679 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 4 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A flexible and unopinionated edit in place library for Angular applications

project logo


MIT commitizen PRs styled with prettier All Contributors ngneat spectator

A flexible and unopinionated edit in place library for Angular applications

Edit in place is a complete solution for switching modes between static content and an editable control that allows editing it.

Following open/closed principle, the library focuses on the switch mechanism, giving you full control of the data you want to update and the content you want to display and how to edit it.

alt-text

Demo

Features

  • βœ… Fully customizable
  • βœ… Manual trigger support
  • βœ… Reactive Forms support
  • βœ… Multiple Forms support

Installation

ng add @ngneat/edit-in-place

Usage

Add the EditableModule to your AppModule.

import { EditableModule } from '@ngneat/edit-in-place';

@NgModule({
  declarations: [AppComponent],
  imports: [EditableModule],
  bootstrap: [AppComponent]
})
export class AppModule {}

Now you can use the <editable> component:

@Component({
  template: `
    <editable (save)="update()" (cancel)="cancel()">
      <ng-template viewMode>{{ value }}</ng-template>

      <ng-template editMode>
        <input editableOnEnter editableOnEscape [formControl]="control" />
      </ng-template>
    </editable>
  `
})
class MyComponent {
  value = 'foo';
  control = new FormControl(this.value);

  update() {
    this.value = this.control.value;
  }

  cancel() {
    this.control.setValue(this.value);
  }
}

For more complex examples, check out the playground.

Changing the Active Mode

Click on the viewMode template to switch it to editMode or click outside the editable component to switch back to viewMode.

You can customize the switch trigger which set to click by default by providing a MouseEvent type:

<editable openBindingEvent="dblclick"
          closeBindingEvent="dblclick">
    ...
</editable>

You can also set this value globally by providing it in the EDITABLE_CONFIG provider:

@NgModule({
  ...
  providers: [
    {
      provide: EDITABLE_CONFIG, 
      useValue: {
        openBindingEvent: 'dblclick',
        closeBindingEvent: 'dblclick',
      } as EditableConfig
    }
  ]
})
export class AppModule {}

Handle Events Manually

You can use the editableOnUpdate and editableOnCancel directives to trigger the update or the reset of the value on chosen elements.

<editable (save)="updateField()" (cancel)="resetField()">
  <ng-template viewMode>...</ng-template>

  <ng-template editMode>
    <input formControlName="name">
    <button editableOnSave>Save</button>
    <button editableOnCancel>Cancel</button>    
  </ng-template>
</editable>

Track event changes

You can use the modeChange event to know what is the state of a given EditableComponent.

<editable (modeChange)="doWhatever()">
  <ng-template viewMode>...</ng-template>

  <ng-template editMode>
    <input formControlName="name">
    <button editableOnSave>Save</button>
    <button editableOnCancel>Cancel</button>    
  </ng-template>
</editable>

Handle Focus

As a focusable form tag might be nested or custom, it isn't focused by default when the editMode is displayed.

To make it focusable, you can add the editableFocus directive on the input:

<editable>

  <ng-template viewMode>
    ... 
  </ng-template>

  <ng-template editMode>
    <input editableFocusable formControlName="name">   
  </ng-template>
</editable>

Events

Add the (save) event binding to handle the update of the content.

<editable (save)="updateField()">
   ...
</editable>

The following actions will trigger this event:

  • editableOnEnter directive
  • editableOnSave directive
  • closeBindingEvent @Input() MouseEvent

Optionally you can add the (cancel) event binding to handle the reset of the value of a formControl:

<editable (cancel)="resetField()">
  ...
</editable>

The following actions will trigger this event:

  • editableCancel directive
  • editableOnEscape directive

Inputs

@Input Type Description Default
openBindingEvent string The MouseEvent type to display the editMode click
closeBindingEvent string The MouseEvent type to display the viewMode click
enabled boolean Allows or forbids edit mode (doesn't switch to it) true

Outputs

@Output Type Description
save void triggered by the editableOnSave and editableOnEnter directives and the MouseEvent on closeBindingEvent @Input
cancel void triggered by the editableCancel and editableOnEscape directives
modeChange edit or view triggered when the mode changes

Directives

editableFocusable

Focus the host element when switching to editMode (for nested inputs).

editableOnEnter

Listen to keyup enter to switch to viewMode and update the value of the viewMode host element.

editableOnEscape

Listen to keyup escape to switch to viewMode without updating the value of the viewMode host element.

editableOnSave

Listen to a MouseEvent on ths host element in order to switch to viewMode and udpate the value of the content of the viewMode*host element.

@Input Type Description Default
saveEvent string The MouseEvent type used to trigger the @Output() save click

editableOnCancel

Listen to a MouseEvent on ths host element in order to trigger to switch to viewMode without updating the value of the viewMode host element.

@Input Type Description Default
cancelEvent string The MouseEvent type used to trigger the @Output() cancel click

Multiple Forms Usage

edit-in-place also supports switching between modes for multiple components at once.

Add the editableGroup directive on a parent html tag of your editable components:

<section editableGroup>
  <editable></editable>
  <editable></editable>
  <editable></editable>
</section>

Changing the Active Mode

Unlike using a single editable component, an editableGroup doesn't support MouseEvent events on the component to switch modes.

You can switch modes by using dedicated directives on html button tag to switch mode for the whole group:

  • editableGroupEdit to switch to editMode
  • editableGroupSave to save the value of each form tag and switch to viewMode
  • editableGroupCancel to switch to viewMode without saving the value of each form tag
<section editableGroup>
  <button editableGroupEdit>Edit</button>
  <button editableGroupSave>Save</button>
  <button editableGroupCancel>Cancel</button>
  <editable></editable>
  <editable></editable>
  <editable></editable>
</section>

Add the (editableModeChange) event binding to keep track of the active mode.

It's triggered by the editableGroupEdit, editableGroupSave and editableGroupCancel directives.

<section (editableModeChange)="handleModeChange($event)">
  <editable></editable>
  <editable></editable>
  <editable></editable>
</section>

Add the (save) event binding to handle the update of the group.
It's triggered by the editableGroupSave directive:

<section (save)="updateGroup()">
  <editable></editable>
  <editable></editable>
  <editable></editable>
</section>

Optionally you can add the (cancel) event binding to handle the reset of the value of the group.

It's triggered by the editableGroupCancel:

<section (cancel)="cancelUpdate()">
  <editable></editable>
  <editable></editable>
  <editable></editable>
</section>

Directives

editableGroup

Overcharges the behavior of children editable Components to work as one entity.

@Output Type Description
save void triggered by the editableGroupSave directive
cancel void triggered by the editableGroupCancel directive
editableModeChange 'view' or 'edit' triggered by the editableGroupEdit, editableGroupSave and editableGroupCancel directives when switching modes

editableGroupEdit

Listen to a click MouseEvent to switch to editMode.

editableGroupSave

Listen to a click MouseEvent to switch to viewMode and update the value of the group.

editableGroupCancel

Listen to a click MouseEvent to switch to viewMode without updating the value of the group.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


GΓ©rΓ΄me Grignon

πŸ’» πŸ“– πŸ€”

Netanel Basal

πŸ“ πŸ“– πŸ€”

Itay Oded

πŸ’»

Artur Androsovych

πŸ’»

BΓ©renger

πŸ’»

This project follows the all-contributors specification. Contributions of any kind welcome!

Logo made by Freepik from www.flaticon.com

More Repositories

1

falso

All the Fake Data for All Your Real Needs πŸ™‚
TypeScript
3,098
star
2

spectator

🦊 πŸš€ A Powerful Tool to Simplify Your Angular Tests
TypeScript
2,029
star
3

transloco

πŸš€ 😍 The internationalization (i18n) library for Angular
TypeScript
1,856
star
4

until-destroy

🦊 RxJS operator that unsubscribe from observables on destroy
TypeScript
1,712
star
5

elf

πŸ§™β€β™€οΈ A Reactive Store with Magical Powers
TypeScript
1,527
star
6

content-loader

βšͺ️ SVG component to create placeholder loading, like Facebook cards loading.
TypeScript
733
star
7

hot-toast

🍞 Smoking hot Notifications for Angular. Lightweight, customizable and beautiful by default.
TypeScript
687
star
8

cashew

🐿 A flexible and straightforward library that caches HTTP requests in Angular
TypeScript
671
star
9

reactive-forms

(Angular Reactive) Forms with Benefits πŸ˜‰
TypeScript
610
star
10

tailwind

πŸ”₯ A schematic that adds Tailwind CSS to Angular applications
TypeScript
608
star
11

forms-manager

πŸ¦„ The Foundation for Proper Form Management in Angular
TypeScript
517
star
12

query

πŸš€ Powerful asynchronous state management, server-state utilities and data fetching for Angular Applications
TypeScript
510
star
13

error-tailor

πŸ¦„ Making sure your tailor-made error solution is seamless!
TypeScript
478
star
14

helipopper

🚁 A Powerful Tooltip and Popover for Angular Applications
TypeScript
392
star
15

nx-serverless

πŸš€ The Ultimate Monorepo Starter for Node.js Serverless Applications
TypeScript
388
star
16

dialog

πŸ‘» A simple to use, highly customizable, and powerful modal for Angular Applications
TypeScript
371
star
17

hotkeys

πŸ€– A declarative library for handling hotkeys in Angular applications
TypeScript
325
star
18

svg-icon

πŸ‘» A lightweight library that makes it easier to use SVG icons in your Angular Application
TypeScript
251
star
19

inspector

πŸ•΅οΈ An angular library that lets you inspect and change Angular component properties
TypeScript
218
star
20

dirty-check-forms

🐬Detect Unsaved Changes in Angular Forms
TypeScript
199
star
21

input-mask

🎭 @ngneat/input-mask is an angular library that creates an input mask
TypeScript
199
star
22

lib

πŸ€– Lets you focus on the stuff that matters
TypeScript
180
star
23

transloco-keys-manager

πŸ¦„ The Key to a Better Translation Experience
TypeScript
174
star
24

dag

🐠 An Angular service for managing directed acyclic graphs
TypeScript
153
star
25

bind-query-params

Sync URL Query Params with Angular Form Controls
TypeScript
147
star
26

from-event

🦊 ViewChild and FromEvent β€” a Match Made in Angular Heaven
TypeScript
137
star
27

overview

πŸ€– A collection of tools to make your Angular views more modular, scalable, and maintainable
TypeScript
104
star
28

aim

Angular Inline Module Schematics
TypeScript
97
star
29

cmdk

Fast, composable, unstyled command menu for Angular. Directly inspired from pacocoursey/cmdk
TypeScript
91
star
30

copy-to-clipboard

βœ‚οΈ Modern copy to clipboard. No Flash.
TypeScript
78
star
31

variabless

JS & CSS - A Match Made in Heaven πŸ’Ž
HTML
78
star
32

loadoff

🀯 When it comes to loaders, take a load off your mind...
TypeScript
78
star
33

effects

πŸͺ„ A framework-agnostic RxJS effects implementation
TypeScript
59
star
34

avvvatars

Beautifully crafted unique avatar placeholder for your next angular project.
TypeScript
42
star
35

react-rxjs

πŸ”Œ "Plug and play" for Observables in React Apps!
TypeScript
37
star
36

subscribe

Subscription Handling Directive
TypeScript
34
star
37

elf-ng-router-store

Bindings to connect Angular router to Elf
TypeScript
24
star
38

ng-standalone-nx

TypeScript
24
star
39

lit-file-generator

🎁 A lit generator for a component, directive, and controller.
JavaScript
19
star
40

storage

TypeScript
18
star
41

material-schematics

TypeScript
3
star
42

svg-icon-demo

TypeScript
1
star