MHzarini / ngx-time-duration-picker

A generic time duration picker Angular component (v2+)
2 stars 7 forks source link

Set time by default #4

Open andreyRybchuk opened 5 years ago

andreyRybchuk commented 5 years ago

Is it possible to set time by default, not 0? For example 2h23m.

AmmoTrio commented 4 years ago

Any updates on this matter? This is a great and easy to use component. But, this issue is the only thing preventing me from using this component.

stnor commented 4 years ago

Nice job with this library. It's unfortunately unusable for many/most applications if you cannot use this component with an initial value. Edit mode exists in forms you know... CRUD. You support only C (and D).

setValue methods on the parent and the children please.

stnor commented 4 years ago

I copied the project and adopted it to my needs . Thanks. I use moment.js, so adding that dep is not an issue for me.

  @Input('value')
  set _value(val: number) {
    this.setValue(val);
  }

  public setValue(value: number): void {
    setTimeout(() => {
      // @ts-ignore
      let duration = moment.duration(value, this.unit + 's');
      this.units.toArray().forEach(element => {
        switch (element.name.toLocaleLowerCase()) {
          case "year":
            element.setValue(duration.years())
            break;
          case "month":
            element.setValue(duration.months())
            break;
          case "week":
            element.setValue(duration.weeks())
            break;
          case "day":
            element.setValue(duration.days())
            break;
          case "hour":
            element.setValue(duration.hours())
            break;
          case "minute":
            element.setValue(duration.minutes())
            break;
          case "second":
            element.setValue(duration.seconds())
            break;
          case "millisecond":
            element.setValue(duration.milliseconds())
            break;
        }
      });
    })
MHzarini commented 4 years ago

@stnor if using moment is solving this issue. please open a PR and i will add that