-
Notifications
You must be signed in to change notification settings - Fork 11
Locale support for sa-currency-input and date-picker #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dastaan2k
wants to merge
17
commits into
simplifiedautomation:master
Choose a base branch
from
Dastaan2k:locale-support-new
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7f4b8da
Added basic implementations of all components in the 'example' direct…
Dastaan2k 8c880aa
added locale support to currency input and date time picker
Dastaan2k f7511b2
- Added group selector for currency input.
Dastaan2k ae33625
- Removed log statements
Dastaan2k bab07d9
- Changed the apporach to get locale based decimal and group separator.
Dastaan2k 09b80dd
- ..
Dastaan2k 29e0246
- Added examples
Dastaan2k d43d655
Deleted package-lock.json
Dastaan2k 71e549e
- Added examples
Dastaan2k 5b7bbe5
- Removed extra files
Dastaan2k 86908b5
Merge branch 'locale-support-new' of https://github.com/Dastaan2k/sim…
Dastaan2k 141b105
- Removed extra files ...
Dastaan2k f152145
- Reverted allowNegativeValue.
Dastaan2k 8963fe6
- Completed remaining changes
Dastaan2k 1348350
- Completed remaining changes
Dastaan2k 449d7c0
- Removed example related components and package-lock and angular.json
Dastaan2k d91227c
- First numerical value initialization bug through form control for c…
Dastaan2k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
projects/simplified-ui/src/lib/sa-currency-input/sa-currency-input.component.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| <input matInput [placeholder]="placeholder" type="text" [formControl]="currencyValue" required="required" /> | ||
| <input matInput [placeholder]="placeholder" type="text" [formControl]="currencyValue" [required]="required" /> | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,73 +1,83 @@ | ||
| import { | ||
| Component, | ||
| OnDestroy, | ||
| ViewChild, | ||
| DoCheck, | ||
| ElementRef, | ||
| Inject, | ||
| Input, | ||
| LOCALE_ID, | ||
| OnDestroy, | ||
| OnInit, | ||
| Optional, | ||
| Self, | ||
| OnInit, | ||
| Inject, | ||
| LOCALE_ID, | ||
| DoCheck | ||
| ViewChild | ||
| } from '@angular/core'; | ||
| import { Subject } from 'rxjs'; | ||
| import { coerceBooleanProperty } from '@angular/cdk/coercion'; | ||
| import { FocusMonitor } from '@angular/cdk/a11y'; | ||
| import { debounceTime } from 'rxjs/operators'; | ||
| import { formatCurrency, getCurrencySymbol } from '@angular/common'; | ||
| import { | ||
| CurrencyPipe, | ||
| getLocaleCurrencyCode, getLocaleCurrencySymbol, | ||
| getLocaleNumberFormat, getLocaleNumberSymbol, | ||
| NumberFormatStyle, NumberSymbol, | ||
| registerLocaleData | ||
| } from '@angular/common'; | ||
| import { MatFormFieldControl } from '@angular/material/form-field'; | ||
| import { ControlValueAccessor, FormControl, NgControl, Validators } from '@angular/forms'; | ||
| import { symbolFormatEnum } from '../pipes/sa-value-formatter.pipe'; | ||
| import { ControlValueAccessor, FormControl, NgControl } from '@angular/forms'; | ||
|
|
||
| /* | ||
| // Register the locale data in the module/component class where this component will be used. | ||
| import localeEs from '@angular/common/locales/es'; | ||
| registerLocaleData(localeEs); | ||
| */ | ||
|
|
||
| @Component({ | ||
| selector: 'sa-currency-input', | ||
| templateUrl: './sa-currency-input.component.html', | ||
| styleUrls: ['./sa-currency-input.component.scss'], | ||
| providers: [{ provide: MatFormFieldControl, useExisting: SaCurrencyInputComponent }] | ||
| providers: [ | ||
| CurrencyPipe, | ||
| { provide: MatFormFieldControl, useExisting: SaCurrencyInputComponent }, | ||
| ] | ||
| }) | ||
| export class SaCurrencyInputComponent | ||
| implements ControlValueAccessor, MatFormFieldControl<any>, OnInit, OnDestroy, DoCheck { | ||
| export class SaCurrencyInputComponent implements ControlValueAccessor, MatFormFieldControl<any>, OnInit, OnDestroy, DoCheck { | ||
| @ViewChild('input') inputRef: ElementRef; | ||
|
|
||
| @Input() allowNegative: boolean = true; | ||
|
|
||
| static nextId = 0; | ||
|
|
||
| private decimalSeparator: string; | ||
| private groupSeparator: string; | ||
|
|
||
| public currencyValue = new FormControl(); | ||
| stateChanges = new Subject<void>(); | ||
| private _value: any; | ||
| private viewValue: string; | ||
| focused = false; | ||
| errorState = false; | ||
| controlType = 'currency-input'; | ||
| id = `currency-input-${SaCurrencyInputComponent.nextId++}`; | ||
| describedBy = ''; | ||
| onChange = (_: any) => {}; | ||
| onTouched = () => {}; | ||
| private _empty = true; | ||
| private symbol: string; | ||
| private _disabled = false; | ||
| private _placeholder: string; | ||
|
|
||
| constructor( | ||
| private _focusMonitor: FocusMonitor, | ||
| private _elementRef: ElementRef<HTMLElement>, | ||
| @Optional() @Self() public ngControl: NgControl, | ||
| @Inject(LOCALE_ID) private locale: string | ||
| ) { | ||
| this.decimalSeparator = '.'; | ||
| this.symbol = getCurrencySymbol('USD', symbolFormatEnum.narrow); | ||
| onChange = (_: any) => {}; | ||
| onTouched = () => {}; | ||
|
|
||
| constructor(private _focusMonitor: FocusMonitor, private _elementRef: ElementRef<HTMLElement>, @Optional() @Self() public ngControl: NgControl, @Inject(LOCALE_ID) private locale: string,private currencyPipe: CurrencyPipe) { | ||
|
|
||
| this.groupSeparator = getLocaleNumberSymbol(this.locale, NumberSymbol.CurrencyGroup); | ||
| this.decimalSeparator = getLocaleNumberSymbol(this.locale, NumberSymbol.CurrencyDecimal); | ||
|
|
||
| _focusMonitor.monitor(_elementRef, true).subscribe((origin) => { | ||
| if (this.focused && !origin) { | ||
| if (this.viewValue) { | ||
| if (this.parse(this.viewValue)) | ||
| this.currencyValue.patchValue(formatCurrency(parseFloat(this.parse(this.viewValue)), 'en-US', this.symbol)); | ||
| else this.currencyValue.patchValue(''); | ||
|
|
||
| if(this.currencyValue.value != null) { | ||
| if (origin) { | ||
| this.currencyValue.setValue(this.parse(this.currencyValue.value)); | ||
| } | ||
| this.onTouched(); | ||
| } else { | ||
| if (this._value) { | ||
| this.currencyValue.patchValue(this.parse(this._value)); | ||
| else { | ||
| this.currencyValue.setValue(currencyPipe.transform(this.parse(this.currencyValue.value), getLocaleCurrencyCode(this.locale))); | ||
| } | ||
| } | ||
| this.focused = !!origin; | ||
|
|
@@ -79,13 +89,6 @@ export class SaCurrencyInputComponent | |
| } | ||
| } | ||
|
|
||
| ngOnInit() { | ||
| this.currencyValue.valueChanges.pipe(debounceTime(200)).subscribe((num) => { | ||
| this.viewValue = num; | ||
| this.value = this.parse(num); | ||
| }); | ||
| } | ||
|
|
||
| ngDoCheck(): void { | ||
| if (this.ngControl) { | ||
| this.errorState = this.ngControl.invalid && this.ngControl.touched; | ||
|
|
@@ -98,12 +101,21 @@ export class SaCurrencyInputComponent | |
| this._focusMonitor.stopMonitoring(this._elementRef); | ||
| } | ||
|
|
||
| ngOnInit() { | ||
| this.currencyValue.valueChanges.pipe(debounceTime(200)).subscribe((num) => { | ||
| this.value = this.parse(num); | ||
| }); | ||
| } | ||
|
|
||
| get empty() { | ||
| return this._empty; | ||
| } | ||
|
|
||
| get shouldLabelFloat() { | ||
| return this.focused || !this.empty; | ||
| if (!this.focused) { | ||
| return this.currencyValue.value !== null; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we use this.empty instead of checking null? |
||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @Input() | ||
|
|
@@ -119,10 +131,12 @@ export class SaCurrencyInputComponent | |
| get required(): boolean { | ||
| return this._required; | ||
| } | ||
|
|
||
| set required(value: boolean) { | ||
| this._required = coerceBooleanProperty(value); | ||
| this.stateChanges.next(); | ||
| } | ||
|
|
||
| private _required = false; | ||
|
|
||
| @Input() | ||
|
|
@@ -142,11 +156,7 @@ export class SaCurrencyInputComponent | |
|
|
||
| set value(val: string | null) { | ||
| this._value = val; | ||
| if (this._value != null && this._value !== '') { | ||
| this._empty = false; | ||
| } else { | ||
| this._empty = true; | ||
| } | ||
| this._empty = !(this._value != null && this._value !== ''); | ||
| this.onChange(this._value); | ||
| this.stateChanges.next(); | ||
| } | ||
|
|
@@ -160,7 +170,7 @@ export class SaCurrencyInputComponent | |
| writeValue(val: string | null): void { | ||
| if (val != null) { | ||
| this._empty = false; | ||
| this.currencyValue.patchValue(formatCurrency(parseFloat(val), 'en-US', this.symbol)); | ||
| this.currencyValue.setValue(this.currencyPipe.transform(this.parse(val),getLocaleCurrencyCode(this.locale))) | ||
| } | ||
| this.value = val; | ||
| } | ||
|
|
@@ -182,10 +192,12 @@ export class SaCurrencyInputComponent | |
| } | ||
|
|
||
| parse(value: string) { | ||
| let [integer, fraction = ''] = (value.toString() || '').split(this.decimalSeparator); | ||
| const temp = value.toString().replace(getLocaleCurrencySymbol(this.locale), ''); | ||
| let [integer, fraction = ''] = (temp.toString() || '').split(this.decimalSeparator); | ||
| integer = integer.replace(new RegExp(/[^\d\.]/, 'g'), ''); | ||
| integer = integer.replace(this.groupSeparator, ''); | ||
| fraction = parseInt(fraction, 10) > 0 && 2 > 0 ? this.decimalSeparator + (fraction + '000000').substring(0, 2) : ''; | ||
| if (this.allowNegative && (value.toString() || '').startsWith('-')) { | ||
| if (this.allowNegative && (temp.toString() || '').startsWith('-')) { | ||
| return (-1 * parseFloat(integer + fraction)).toString(); | ||
| } else { | ||
| return integer + fraction; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can register locale in the app only