Skip to content

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

Merged
merged 2 commits into from
May 15, 2018

Conversation

pfeigl
Copy link
Contributor

@pfeigl pfeigl commented May 4, 2018

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

@pfeigl pfeigl requested a review from mmalerba as a code owner May 4, 2018 20:51
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label May 4, 2018
@pfeigl pfeigl force-pushed the autosize-bindable branch from 384d2e8 to 329a40f Compare May 5, 2018 00:54
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
@pfeigl pfeigl force-pushed the autosize-bindable branch from 329a40f to 01d1658 Compare May 5, 2018 00:54
@pfeigl
Copy link
Contributor Author

pfeigl commented May 5, 2018

@mmalerba Added unit tests and squashed commits, this should be ready for review if builds / tests pass.

@pfeigl pfeigl changed the title WIP: feat(cdk-input): change autosize to be bindable (#9884) feat(cdk-input): change autosize to be bindable (#9884) May 5, 2018
Copy link
Contributor

@mmalerba mmalerba left a 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


fixtureWithoutAutosize.detectChanges();

let previousHeight = textarea.clientHeight;
Copy link
Contributor

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

Copy link
Contributor Author

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?

Copy link
Contributor

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

Copy link
Contributor Author

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.

.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');
Copy link
Contributor

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.'

Copy link
Contributor Author

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".

fixtureWithoutAutosize.detectChanges();

expect(textarea.clientHeight)
.toBeGreaterThan(previousHeight, 'Expected textarea to have grown after enabling.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'...after enabling autosize.'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ups, good catch, changed

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');
Copy link
Contributor

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.'

fixtureWithoutAutosize.detectChanges();

expect(textarea.clientHeight)
.toEqual(previousHeight, 'Expected textarea to again have the original size.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove 'again'

.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');
Copy link
Contributor

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.'

private readonly _destroyed = new Subject<void>();

private _minRows: number;
private _maxRows: number;
private _enabled: boolean = true;

private get textarea(): HTMLTextAreaElement {
Copy link
Contributor

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;
}

Copy link
Contributor Author

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.

* Resets the textarea to it's original size
*/
reset() {
if (this._initialHeight === undefined) {
Copy link
Contributor

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?

Copy link
Contributor Author

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

@pfeigl
Copy link
Contributor Author

pfeigl commented May 8, 2018

Sent a new commit with the required changes, thanks for the feedback.

@mmalerba mmalerba added pr: lgtm action: merge The PR is ready for merge by the caretaker target: minor This PR is targeted for the next minor release labels May 8, 2018

@Input()
get matTextareaAutosize(): boolean { return this.enabled; }
set matTextareaAutosize(value: boolean) { this.enabled = value; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coerceBooleanProperty?

Copy link
Contributor Author

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; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coerceBooleanProperty?

Copy link
Contributor Author

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')
Copy link
Contributor

@rafaelss95 rafaelss95 May 8, 2018

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?

Copy link
Contributor Author

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]',

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, yes :)

@tinayuangao tinayuangao merged commit 2d227b7 into angular:master May 15, 2018
@pfeigl pfeigl deleted the autosize-bindable branch May 15, 2018 20:00
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

make matTextareaAutosize bindable
5 participants