Skip to content

Commit bebd776

Browse files
committed
make it compatible with native form submit close #14
1 parent 2deb045 commit bebd776

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#changelog
2+
## [3.8.0] - 2023-3-30
3+
### new features
4+
- make component compatible with native form element value getter.
25
## [3.7.13] - 2023-3-30
36
### fixed
47
- fix calendar open and close problem #13 on calendar button clicked

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ web component date input (picker) to get date (jalali & gregorian) from user.
2828

2929
- customizable value format so you can get your value in standard iso format or custom format like `1400/12/08` or `1400_12_08`
3030

31-
- support `esm` import build for modern `ECMA Script` nodejs app.
31+
- support `esm` import build for modern `ECMA Script` nodejs app.
32+
33+
- compatible with native HTML `form` element to send data to server.
3234

3335
Demo & Sample
3436
in github: <https://javadbat.github.io/jb-date-input/>

lib/JBDateInput.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if(HTMLElement== undefined){
2626
console.error('you cant render web component on a server side');
2727
}
2828
export class JBDateInputWebComponent extends HTMLElement {
29-
static get formAssociated() { return true; }
29+
static formAssociated = true;
3030
internals_?: ElementInternals;
3131
elements!: ElementsObject;
3232
#dateFactory: DateFactory = new DateFactory({ inputType: (this.getAttribute("value-type") as InputTypes), valueType: this.getAttribute("value-type") as ValueTypes });
@@ -45,16 +45,23 @@ export class JBDateInputWebComponent extends HTMLElement {
4545
required = false;
4646
DefaultValidationErrorMessage = "مقدار وارد شده نا معتبر است"
4747
#valueObject: JBDateInputValueObject = getEmptyValueObject();
48+
get name() { return this.getAttribute('name'); }
49+
get form() { return this.internals_!.form; }
4850
get value(): string {
4951
const value = this.getDateValue();
5052
return value;
5153
}
5254
set value(value: string) {
53-
if (this.internals_ && typeof this.internals_.setFormValue == "function") {
54-
this.internals_.setFormValue(value);
55-
}
55+
5656
this.setDateValue(value);
5757
this.updateinputTextFromValue();
58+
this.#updateFormAssossicatedValue();
59+
}
60+
#updateFormAssossicatedValue():void{
61+
//in html form we need to get date input value in native way this function update and set value of the input so form can get it when needed
62+
if (this.internals_ && typeof this.internals_.setFormValue == "function") {
63+
this.internals_.setFormValue(this.value);
64+
}
5865
}
5966
get inputValue() {
6067
return this.#inputValue;
@@ -609,6 +616,7 @@ export class JBDateInputWebComponent extends HTMLElement {
609616
this.setDateValueFromTimeStamp(value);
610617
break;
611618
}
619+
this.#updateFormAssossicatedValue();
612620

613621
}
614622
setValueObjNull() {
@@ -679,6 +687,7 @@ export class JBDateInputWebComponent extends HTMLElement {
679687
const result: JBDateInputValueObject = this.#dateFactory.getDateValueObjectBaseOnInputType(year, month, day, prevYear, prevMonth);
680688
this.#valueObject = result;
681689
this.updateCalendarView();
690+
this.#updateFormAssossicatedValue();
682691
}
683692
updateinputTextFromValue() {
684693
let str = this.inputFormat;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"jalali web-component",
2929
"jalali web component"
3030
],
31-
"version": "3.7.13",
31+
"version": "3.8.0",
3232
"bugs": "https://github.com/javadbat/jb-date-input/issues",
3333
"license": "MIT",
3434
"files": [

0 commit comments

Comments
 (0)