-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(cdk-input): change autosize to be bindable (#9884) #11167
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
Conversation
This allows cdkTextareaAutosize selector to be bindable and toggle the internal enabled / disabled state. Upon disabling the initial height is restored (textarea.style.height before any changes were applied). Also added wrapper into (to be discontinued) matTextareaAutosize. Fixes angular#9884 Currently missing unit tests, thus still WIP. Manual tests in demo-app seem to work as expected
@mmalerba Added unit tests and squashed commits, this should be ready for review if builds / tests pass. |
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.
Looks good, just some nits
src/cdk/text-field/autosize.spec.ts
Outdated
|
||
fixtureWithoutAutosize.detectChanges(); | ||
|
||
let previousHeight = textarea.clientHeight; |
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.
nit: can be const since never re-assigned
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.
Changed, is there any specific reason for not having https://palantir.github.io/tslint/rules/prefer-const/ enabled in tslint?
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.
There's probably just a bunch of code that needs to be updated before we can enable it. But I don't see any reason not to
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.
Just found this on that topic: #5959
Seems to have been discussed in the past and decided against doing it. Tried adding it here and autofixing it which worked flawlessly but was actually really a huge chunk of changes.
src/cdk/text-field/autosize.spec.ts
Outdated
.toEqual(previousHeight, 'Expected textarea to still have the same size.'); | ||
expect(textarea.clientHeight) | ||
.toBeLessThan(textarea.scrollHeight, | ||
'Expected textarea height to be less than its scrollHeight'); |
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.
'Expected textarea to have scrollbar.'
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.
Changed, good improvement in readability. Honestly change based my text off of the first unit test which states "Expected textarea height to match its scrollHeight".
src/cdk/text-field/autosize.spec.ts
Outdated
fixtureWithoutAutosize.detectChanges(); | ||
|
||
expect(textarea.clientHeight) | ||
.toBeGreaterThan(previousHeight, 'Expected textarea to have grown after enabling.'); |
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.
'...after enabling autosize.'
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.
ups, good catch, changed
src/cdk/text-field/autosize.spec.ts
Outdated
expect(textarea.clientHeight) | ||
.toBeGreaterThan(previousHeight, 'Expected textarea to have grown after enabling.'); | ||
expect(textarea.clientHeight) | ||
.toBe(textarea.scrollHeight, 'Expected textarea height to match its scrollHeight'); |
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.
'Expected textarea not to have scrollbar.'
src/cdk/text-field/autosize.spec.ts
Outdated
fixtureWithoutAutosize.detectChanges(); | ||
|
||
expect(textarea.clientHeight) | ||
.toEqual(previousHeight, 'Expected textarea to again have the original size.'); |
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.
remove 'again'
src/cdk/text-field/autosize.spec.ts
Outdated
.toEqual(previousHeight, 'Expected textarea to again have the original size.'); | ||
expect(textarea.clientHeight) | ||
.toBeLessThan(textarea.scrollHeight, | ||
'Expected textarea height to be less than its scrollHeight again'); |
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.
'Expected textarea to have scrollbar.'
src/cdk/text-field/autosize.ts
Outdated
private readonly _destroyed = new Subject<void>(); | ||
|
||
private _minRows: number; | ||
private _maxRows: number; | ||
private _enabled: boolean = true; | ||
|
||
private get textarea(): HTMLTextAreaElement { |
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.
Instead of this, change the constructor:
constructor(elementRef, ...) {
this._textareaElement = elementRef.nativeElement as HTMLTextAreaElement;
}
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.
Good idea and changed. Doesn't really make to big of a difference in the transpiled js because the cast is removed anyways, but yea, way cleaner this way.
src/cdk/text-field/autosize.ts
Outdated
* Resets the textarea to it's original size | ||
*/ | ||
reset() { | ||
if (this._initialHeight === undefined) { |
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.
Can you add a comment explaining why we skip this if it's undefined?
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.
Added a comment for it to be more clear why this was added
Sent a new commit with the required changes, thanks for the feedback. |
|
||
@Input() | ||
get matTextareaAutosize(): boolean { return this.enabled; } | ||
set matTextareaAutosize(value: boolean) { this.enabled = value; } |
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.
coerceBooleanProperty
?
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.
enabled
is a setter within CdkTextareaAutosize which internally uses coerceBooleanProperty
|
||
@Input('mat-autosize') | ||
get matAutosize(): boolean { return this.enabled; } | ||
set matAutosize(value: boolean) { this.enabled = value; } |
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.
coerceBooleanProperty
?
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.
enabled
is a setter within CdkTextareaAutosize which internally uses coerceBooleanProperty
@@ -34,4 +34,12 @@ export class MatTextareaAutosize extends CdkTextareaAutosize { | |||
@Input() | |||
get matAutosizeMaxRows(): number { return this.maxRows; } | |||
set matAutosizeMaxRows(value: number) { this.maxRows = value; } | |||
|
|||
@Input('mat-autosize') |
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.
Is this correctly named, @mmalerba?
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.
Should be, see the selector definition: selector: 'textarea[mat-autosize], textarea[matTextareaAutosize]',
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.
Aha, yes :)
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
This allows cdkTextareaAutosize selector to be bindable and toggle the internal
enabled / disabled state.
Upon disabling the initial height is restored (textarea.style.height before any changes
were applied).
Also added wrapper into (to be discontinued) matTextareaAutosize.
Fixes #9884