From 5fbae0d178e3a413df870630a017d530db30f1e7 Mon Sep 17 00:00:00 2001 From: Sarah Dayan <5370675+sarahdayan@users.noreply.github.com> Date: Tue, 12 Apr 2022 13:01:56 +0200 Subject: [PATCH] fix(autocomplete-js): avoid warning when renderer is not specified at all (#947) --- .../src/__tests__/renderer.test.ts | 35 +++++++++++++++++++ .../autocomplete-js/src/getDefaultOptions.ts | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/autocomplete-js/src/__tests__/renderer.test.ts b/packages/autocomplete-js/src/__tests__/renderer.test.ts index a0306602d..93cf810ef 100644 --- a/packages/autocomplete-js/src/__tests__/renderer.test.ts +++ b/packages/autocomplete-js/src/__tests__/renderer.test.ts @@ -440,6 +440,41 @@ describe('renderer', () => { ); }); + test('does not warn at all when only passing a `render` option', () => { + const container = document.createElement('div'); + const panelContainer = document.createElement('div'); + + document.body.appendChild(panelContainer); + + expect(() => { + autocomplete<{ label: string }>({ + container, + panelContainer, + initialState: { + isOpen: true, + }, + getSources() { + return [ + { + sourceId: 'testSource', + getItems() { + return [{ label: '1' }]; + }, + templates: { + item({ item }) { + return item.label; + }, + }, + }, + ]; + }, + render({ children, render }, root) { + render(children, root); + }, + }); + }).not.toWarnDev(); + }); + test('does not warn at all when not passing a custom renderer', () => { const container = document.createElement('div'); const panelContainer = document.createElement('div'); diff --git a/packages/autocomplete-js/src/getDefaultOptions.ts b/packages/autocomplete-js/src/getDefaultOptions.ts index a61be9c39..e2c3b11bd 100644 --- a/packages/autocomplete-js/src/getDefaultOptions.ts +++ b/packages/autocomplete-js/src/getDefaultOptions.ts @@ -102,7 +102,7 @@ export function getDefaultOptions( ); warn( - Boolean(!render || renderer?.render), + !(render && renderer && !renderer?.render), `You provided the \`render\` option but did not provide a \`renderer.render\`. Since v1.6.0, you can provide a \`render\` function directly in \`renderer\`.` + `\nTo get rid of this warning, do any of the following depending on your use case.` + "\n- If you are using the `render` option only to override Autocomplete's default `render` function, pass the `render` function into `renderer` and remove the `render` option." +