From f1851d951fdfb0ca1592828ccff59d2ee3cbb197 Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Wed, 21 Jul 2021 20:35:30 -0700 Subject: [PATCH] Revert "Remove deprecated disabledWhen" --- .../glimmer/lib/components/-link-to.ts | 6 ++ .../glimmer/lib/components/link-to.ts | 64 ++++++++++++++ .../components/link-to/routing-angle-test.js | 81 +++++++++++++++++- .../components/link-to/routing-curly-test.js | 83 ++++++++++++++++++- 4 files changed, 231 insertions(+), 3 deletions(-) diff --git a/packages/@ember/-internals/glimmer/lib/components/-link-to.ts b/packages/@ember/-internals/glimmer/lib/components/-link-to.ts index f0a8e651e13..642eeb64605 100644 --- a/packages/@ember/-internals/glimmer/lib/components/-link-to.ts +++ b/packages/@ember/-internals/glimmer/lib/components/-link-to.ts @@ -879,6 +879,12 @@ const LinkComponent = EmberComponent.extend({ loadingHref: '#', didReceiveAttrs() { + let { disabledWhen } = this; + + if (disabledWhen !== undefined) { + this.set('disabled', disabledWhen); + } + let { params } = this; if (!params || params.length === 0) { diff --git a/packages/@ember/-internals/glimmer/lib/components/link-to.ts b/packages/@ember/-internals/glimmer/lib/components/link-to.ts index 3bb0b105281..a139ba18c80 100644 --- a/packages/@ember/-internals/glimmer/lib/components/link-to.ts +++ b/packages/@ember/-internals/glimmer/lib/components/link-to.ts @@ -614,6 +614,70 @@ if (EMBER_MODERNIZED_BUILT_IN_COMPONENTS) { }); } + // @disabledWhen + { + let superIsSupportedArgument = prototype['isSupportedArgument']; + + Object.defineProperty(prototype, 'isSupportedArgument', { + configurable: true, + enumerable: false, + value: function isSupportedArgument(this: LinkTo, name: string): boolean { + if (this.modernized) { + if (name === 'disabledWhen') { + deprecate( + 'Passing the `@disabledWhen` argument to is deprecated. ' + + 'Use the `@disabled` argument instead.', + false, + { + id: 'ember.link-to.disabled-when', + for: 'ember-source', + since: {}, + until: '4.0.0', + } + ); + + return true; + } + } + + return superIsSupportedArgument.call(this, name); + }, + }); + + let superDescriptor = descriptorFor(prototype, 'isDisabled'); + + assert( + `[BUG] expecting isDisabled to be a getter on `, + superDescriptor && typeof superDescriptor.get === 'function' + ); + + let superGetter = superDescriptor.get as (this: LinkTo) => boolean; + + Object.defineProperty(prototype, 'isDisabled', { + configurable: true, + enumerable: false, + get: function isDisabled(this: LinkTo): boolean { + if ('disabledWhen' in this.args.named) { + deprecate( + 'Passing the `@disabledWhen` argument to is deprecated. ' + + 'Use the `@disabled` argument instead.', + false, + { + id: 'ember.link-to.disabled-when', + for: 'ember-source', + since: {}, + until: '4.0.0', + } + ); + + return Boolean(this.named('disabledWhen')); + } + + return superGetter.call(this); + }, + }); + } + // QP { let superModelsDescriptor = descriptorFor(prototype, 'models'); diff --git a/packages/@ember/-internals/glimmer/tests/integration/components/link-to/routing-angle-test.js b/packages/@ember/-internals/glimmer/tests/integration/components/link-to/routing-angle-test.js index f1d1cfc863c..fa7e4c22561 100644 --- a/packages/@ember/-internals/glimmer/tests/integration/components/link-to/routing-angle-test.js +++ b/packages/@ember/-internals/glimmer/tests/integration/components/link-to/routing-angle-test.js @@ -147,6 +147,64 @@ moduleFor( assert.strictEqual(this.$('#about-link').attr('href'), null, 'there is no href attribute'); } + async [`@test [DEPRECATED] it applies a 'disabled' class when disabledWhen`](assert) { + this.addTemplate( + 'index', + ` + About + About + ` + ); + + let controller; + + this.add( + 'controller:index', + class extends Controller { + constructor(...args) { + super(...args); + controller = this; + } + + dynamicDisabledWhen = true; + } + ); + + await expectDeprecationAsync( + () => this.visit('/'), + 'Passing the `@disabledWhen` argument to is deprecated. Use the `@disabled` argument instead.', + EMBER_MODERNIZED_BUILT_IN_COMPONENTS + ); + + assert.equal( + this.$('#about-link-static.disabled').length, + 1, + 'The static link is disabled when its disabledWhen is true' + ); + assert.equal( + this.$('#about-link-dynamic.disabled').length, + 1, + 'The dynamic link is disabled when its disabledWhen is true' + ); + + expectDeprecation( + () => runTask(() => controller.set('dynamicDisabledWhen', false)), + 'Passing the `@disabledWhen` argument to is deprecated. Use the `@disabled` argument instead.', + EMBER_MODERNIZED_BUILT_IN_COMPONENTS + ); + + assert.equal( + this.$('#about-link-static.disabled').length, + 1, + 'The static link is disabled when its disabledWhen is true' + ); + assert.strictEqual( + this.$('#about-link-dynamic.disabled').length, + 0, + 'The dynamic link is re-enabled when its disabledWhen becomes false' + ); + } + async [`@test it applies a 'disabled' class when disabled`](assert) { this.addTemplate( 'index', @@ -326,6 +384,27 @@ moduleFor( ); } + async [`@test [DEPRECATED] it does not respond to clicks when disabledWhen`](assert) { + this.addTemplate( + 'index', + `About` + ); + + await expectDeprecationAsync( + () => this.visit('/'), + 'Passing the `@disabledWhen` argument to is deprecated. Use the `@disabled` argument instead.', + EMBER_MODERNIZED_BUILT_IN_COMPONENTS + ); + + await expectDeprecationAsync( + () => this.click('#about-link'), + 'Passing the `@disabledWhen` argument to is deprecated. Use the `@disabled` argument instead.', + EMBER_MODERNIZED_BUILT_IN_COMPONENTS + ); + + assert.strictEqual(this.$('h3.about').length, 0, 'Transitioning did not occur'); + } + async [`@test it does not respond to clicks when disabled`](assert) { this.addTemplate( 'index', @@ -372,7 +451,7 @@ moduleFor( assert.equal( this.$('h3.about').length, 1, - 'Transitioning did occur when disabled became false' + 'Transitioning did occur when disabledWhen became false' ); } diff --git a/packages/@ember/-internals/glimmer/tests/integration/components/link-to/routing-curly-test.js b/packages/@ember/-internals/glimmer/tests/integration/components/link-to/routing-curly-test.js index eb12ae9e343..2ecf5ae4cf0 100644 --- a/packages/@ember/-internals/glimmer/tests/integration/components/link-to/routing-curly-test.js +++ b/packages/@ember/-internals/glimmer/tests/integration/components/link-to/routing-curly-test.js @@ -152,6 +152,64 @@ moduleFor( ); } + async [`@test [DEPRECATED] it applies a 'disabled' class when disabledWhen`](assert) { + this.addTemplate( + 'index', + ` + + + ` + ); + + let controller; + + this.add( + 'controller:index', + class extends Controller { + constructor(...args) { + super(...args); + controller = this; + } + + dynamicDisabledWhen = true; + } + ); + + await expectDeprecationAsync( + () => this.visit('/'), + 'Passing the `@disabledWhen` argument to is deprecated. Use the `@disabled` argument instead.', + EMBER_MODERNIZED_BUILT_IN_COMPONENTS + ); + + assert.equal( + this.$('#about-link-static > a.disabled').length, + 1, + 'The static link is disabled when its disabledWhen is true' + ); + assert.equal( + this.$('#about-link-dynamic > a.disabled').length, + 1, + 'The dynamic link is disabled when its disabledWhen is true' + ); + + expectDeprecation( + () => runTask(() => controller.set('dynamicDisabledWhen', false)), + 'Passing the `@disabledWhen` argument to is deprecated. Use the `@disabled` argument instead.', + EMBER_MODERNIZED_BUILT_IN_COMPONENTS + ); + + assert.equal( + this.$('#about-link-static > a.disabled').length, + 1, + 'The static link is disabled when its disabledWhen is true' + ); + assert.strictEqual( + this.$('#about-link-dynamic > a.disabled').length, + 0, + 'The dynamic link is re-enabled when its disabledWhen becomes false' + ); + } + async [`@test it applies a 'disabled' class when disabled`](assert) { this.addTemplate( 'index', @@ -193,7 +251,7 @@ moduleFor( assert.equal( this.$('#about-link-static > a.disabled').length, 1, - 'The static link is disabled when its disabled is true' + 'The static link is disabled when its disabledWhen is true' ); assert.strictEqual( this.$('#about-link-dynamic > a.disabled').length, @@ -338,6 +396,27 @@ moduleFor( ); } + async [`@test [DEPRECATED] it does not respond to clicks when disabledWhen`](assert) { + this.addTemplate( + 'index', + `` + ); + + await expectDeprecationAsync( + () => this.visit('/'), + 'Passing the `@disabledWhen` argument to is deprecated. Use the `@disabled` argument instead.', + EMBER_MODERNIZED_BUILT_IN_COMPONENTS + ); + + await expectDeprecationAsync( + () => this.click('#about-link > a'), + 'Passing the `@disabledWhen` argument to is deprecated. Use the `@disabled` argument instead.', + EMBER_MODERNIZED_BUILT_IN_COMPONENTS + ); + + assert.strictEqual(this.$('h3.about').length, 0, 'Transitioning did not occur'); + } + async [`@test it does not respond to clicks when disabled`](assert) { this.addTemplate( 'index', @@ -384,7 +463,7 @@ moduleFor( assert.equal( this.$('h3.about').length, 1, - 'Transitioning did occur when disabled became false' + 'Transitioning did occur when disabledWhen became false' ); }