pglejzer / timepicker-ui

timepicker-ui is an easy library with timepicker. Fully wrote with TypeScript. This library is based on Material Design from Google.
MIT License
62 stars 18 forks source link

Is it possible to set a default value? #22

Closed lluukinha closed 2 years ago

lluukinha commented 2 years ago

When there is no time defined, it starts at 12:00, can I set the default value for it to be the current time (for example)?

pglejzer commented 2 years ago

Yes, you're right. By default I setted if there is no value in input this is gonna setting to 12:00 with 24h clockType or to 12:00 PM with 12h clockType.

The is no option to set current time on start if you don't have value or not but you can avoid that behaviur to set value to input by js/ts.

Something like this:

<div class="test" style="max-width: 300px">
  <input type="text" class="timepicker-ui-input form-control" />
</div>
const test = document.querySelector('.test') as HTMLDivElement;
const testPicker = new TimepickerUI(test);

const inputElement = test.querySelector('input') as HTMLInputElement;

inputElement.value = new Date().toLocaleTimeString('en-us', { timeStyle: 'short' });

testPicker.create();

But I can add this option if you want.

lluukinha commented 2 years ago

Thanks for the feedback, indeed this way it works, thanks!

But it's something that would be good to have in there when you have time.

Thank you for your answer, and congratz for the package. It's very useful.

pglejzer commented 2 years ago

Thanks! I will try to add this option. I'm not closing this issue now, If I will release new version I will close it.

pglejzer commented 2 years ago

Hi, I added new release with option called currentTime, it's allows to set currentTime to picker/input if you set it currentTime: true it's forcing clockType to set based on your time locatin and update input on start or you can use object with keys time, updateInput, locales, preventClockType.

lluukinha commented 2 years ago

Hello @pglejzer , its not working as it should (i think now its a bug), when we have date defined, it still opens with a default value set.

pglejzer commented 2 years ago

Hmm, can you show me your code what you try to init?

lluukinha commented 2 years ago

The currentTime is overriding the value of the input even if the value is set

const currentDate = new Date();
            currentDate.setMinutes(0);
            const tm = new TimepickerUI(this.tmRef.current, {
                clockType,
                timeLabel: 'Add scheduled start',
                currentTime: {
                    time: currentDate,
                    preventClockType: this.props.store.team.timeFormat === TimeFormat.TwentyFourHour
                }
            });
lluukinha commented 2 years ago

But even if I just set it to currentTime: true, the same thing happens, every time I open the picker it shows the current time instead of startTime from state.

OBS: My intention is not change the input, just the default value instead of 12:00PM to current hour

pglejzer commented 2 years ago

Ok, thanks for the explanation . I see that you probably using framework like Vue or library like React? I think It can be problem with that. I think state is not updeting in every state change casue this library is not using a virtual DOM and it's is based on normal DOM. I try will look into that and find some solution

lluukinha commented 2 years ago

indeed, I am using react

pglejzer commented 2 years ago

Ok, I think I found the problem. The problem was in my code. I will try to make new release asap :)

pglejzer commented 2 years ago

So as an explanation, it's working now that:

if object is set:

lluukinha commented 2 years ago

Seems that is working now! Thanks a lot.