Skip to content
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

feat: add lazy layout hook #260

Merged
merged 1 commit into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions apis/supernova/src/__tests__/hooks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
useSelections,
useTheme,
useLayout,
useLazyLayout,
useTranslator,
onTakeSnapshot,
} from '../hooks';
Expand Down Expand Up @@ -623,6 +624,21 @@ describe('hooks', () => {
run(c);
expect(value).to.equal('layout');
});
it('useLazyLayout', () => {
let value;
c.context.layout = { hc: 'h' };
c.fn = () => {
value = useLazyLayout();
};
run(c);
c.context.layout = { hc: 'a', qSelectionInfo: { qInSelections: true } };
run(c);
expect(value).to.eql({ hc: 'h' });

c.context.layout = { hc: 'a' };
run(c);
expect(value).to.eql({ hc: 'a' });
});
it('useTranslator', () => {
let value;
c.fn = () => {
Expand Down
9 changes: 9 additions & 0 deletions apis/supernova/src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,15 @@ export function useLayout() {
return useInternalContext('layout');
}

export function useLazyLayout() {
const layout = useInternalContext('layout');
const [ref] = useState({ current: layout });
if (!layout.qSelectionInfo || !layout.qSelectionInfo.qInSelections) {
ref.current = layout;
}
return ref.current;
}

export function useTranslator() {
return useInternalEnv('translator');
}
Expand Down
1 change: 1 addition & 0 deletions apis/supernova/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export { useElement } from './hooks';
export { useSelections } from './hooks';
export { useTheme } from './hooks';
export { useLayout } from './hooks';
export { useLazyLayout } from './hooks';
export { useTranslator } from './hooks';

export { onTakeSnapshot } from './hooks';
6 changes: 0 additions & 6 deletions test/component/hooks/sn.comp.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ describe('hooks', () => {
});

it('usePromise', async () => {
await page.waitForFunction(
selector => document.querySelector(selector).getAttribute('data-render-count') === '0',
{},
`${snSelector}`
);
expect(await page.$eval(`${snSelector} .promise`, el => el.textContent)).to.equal('pending');
await page.waitForFunction(
selector => document.querySelector(selector).getAttribute('data-render-count') === '1',
{},
Expand Down