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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ComboBox] Use isomorphic layout effect #4018

Merged
merged 1 commit into from Feb 24, 2021
Merged

[ComboBox] Use isomorphic layout effect #4018

merged 1 commit into from Feb 24, 2021

Conversation

dfmcphee
Copy link
Contributor

WHY are these changes introduced?

As @BPScott pointed out in #4015 (comment) useLayoutEffect will throw a warning during SSR.

WHAT is this pull request doing?

This follows the suggestions in this issue conditionally using useEffect instead on the server.

How to 馃帺

Try this in the playground:

Copy-paste this code in playground/Playground.tsx:
import React, {useCallback, useState, useMemo} from 'react';
import {SearchMinor} from '@shopify/polaris-icons';

import {Icon, Autocomplete, TextContainer} from '../src';

export function Playground() {
  const deselectedOptions = useMemo(
    () => [
      {value: 'rustic', label: 'Rustic'},
      {value: 'antique', label: 'Antique'},
      {value: 'vinyl', label: 'Vinyl'},
      {value: 'vintage', label: 'Vintage'},
      {value: 'refurbished', label: 'Refurbished'},
    ],
    [],
  );
  const [selectedOptions, setSelectedOptions] = useState([]);
  const [inputValue, setInputValue] = useState('');
  const [options, setOptions] = useState(deselectedOptions);
  const [loading, setLoading] = useState(false);

  const updateText = useCallback(
    (value) => {
      setInputValue(value);

      if (!loading) {
        setLoading(true);
      }

      setTimeout(() => {
        if (value === '') {
          setOptions(deselectedOptions);
          setLoading(false);
          return;
        }
        const filterRegex = new RegExp(value, 'i');
        const resultOptions = options.filter((option) =>
          option.label.match(filterRegex),
        );
        setOptions(resultOptions);
        setLoading(false);
      }, 300);
    },
    [deselectedOptions, loading, options],
  );

  const updateSelection = useCallback(
    (selected) => {
      const selectedText = selected.map((selectedItem) => {
        const matchedOption = options.find((option) => {
          return option.value.match(selectedItem);
        });
        return matchedOption && matchedOption.label;
      });
      setSelectedOptions(selected);
      setInputValue(selectedText);
    },
    [options],
  );

  const textField = (
    <Autocomplete.TextField
      onChange={updateText}
      label="Tags"
      value={inputValue}
      prefix={<Icon source={SearchMinor} color="subdued" />}
      placeholder="Search"
    />
  );

  const emptyState = (
    <>
      <Icon source={SearchMinor} />
      <div style={{textAlign: 'center'}}>
        <TextContainer>Could not find any results</TextContainer>
      </div>
    </>
  );

  return (
    <div style={{height: '225px'}}>
      <Autocomplete
        options={options}
        selected={selectedOptions}
        onSelect={updateSelection}
        loading={loading}
        textField={textField}
        emptyState={emptyState}
      />
    </div>
  );
}

馃帺 checklist

@dfmcphee dfmcphee added the 馃Skip Changelog Causes CI to ignore changelog update check. label Feb 24, 2021
Copy link
Member

@alex-page alex-page left a comment

Choose a reason for hiding this comment

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

Nice, didn't tophat but this makes sense.

@github-actions
Copy link
Contributor

馃煝 This pull request modifies 1 files and might impact 1 other files.

Details:
All files potentially affected (total: 1)
馃З src/components/Autocomplete/components/ComboBox/ComboBox.tsx (total: 1)

Files potentially affected (total: 1)

@dfmcphee dfmcphee merged commit 7bb90d9 into main Feb 24, 2021
@dfmcphee dfmcphee deleted the combobox-effect-ssr branch February 24, 2021 19:58
sylvhama pushed a commit that referenced this pull request Mar 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
馃Skip Changelog Causes CI to ignore changelog update check.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants