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(core): pass scoped API to lifecycle hooks #422

Merged
merged 1 commit into from
Feb 1, 2021

Conversation

francoischalifour
Copy link
Member

Description

This forwards the setters and the refresh APIs to all the lifecycle hooks, including onStateChange and the plugin's subscribe function.

It allows for a more powerful plugins integration, required for Autocomplete Tags for example.

Example

function createTagPlugin<TItem extends {}>({
  getTagLabel,
  onTag,
}: CreateTagPluginProps<TItem>): AutocompletePlugin<TItem> {
  const tags = [];

  return {
    subscribe({ onSelect, refresh, setQuery }) {
      onSelect((params) => {
        tags.push(getTagLabel(params));
        setQuery("");
        refresh();
        onTag(params);
      });
    },
    onStateChange({ refresh }) {
      render(
        <ul className="aa-InputTagList">
          {tags.map((tag, index) => (
            <li key={index} className="aa-InputTagItem">
              <AutocompleteTag
                label={tag}
                onRemove={() => {
                  tags.splice(tags.indexOf(tag), 1);
                  refresh();
                }}
              />
            </li>
          ))}
        </ul>,
        tagContainer
      );
    }
  };
}

@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 1, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 47ebb11:

Sandbox Source
@algolia/js-example Configuration
@algolia/react-renderer-example Configuration

isOpen: false,
}),
});
expect(onStateChange).not.toHaveBeenLastCalledWith(
Copy link
Member

Choose a reason for hiding this comment

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

As it's checking that it's last called with status: idle, is this test needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't change this test logic but you've got a point. This assertion is required but it should probably use .not.toHaveBeenCalledWith. I'll take care of that in another commit.

Base automatically changed from refactor/jsx to next February 1, 2021 18:36
@francoischalifour francoischalifour merged commit 049b343 into next Feb 1, 2021
@francoischalifour francoischalifour deleted the core/pass-scoped-api-to-hooks branch February 1, 2021 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants