react#startTransition JavaScript Examples

The following examples show how to use react#startTransition. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Usage.js    From react-menu with MIT License 4 votes vote down vote up
function MenuOverflowExample() {
  const [overflow, setOverflow] = useState('auto');
  const [position, setPosition] = useState('auto');
  const [input, setInput] = useState('');
  const [filter, setFilter] = useState('');
  const setToast = useContext(ToastContext);

  return (
    <Example data={codeExamples.overflow}>
      <form className="option-form">
        <MenuOptions
          name="overflowGroup"
          title="Overflow"
          data={overflowOptions}
          option={overflow}
          onOptionChange={setOverflow}
        />
        <MenuOptions
          name="positionGroup"
          title="Menu position"
          data={positionOptions}
          option={position}
          onOptionChange={setPosition}
        />
      </form>

      <div>
        <Menu
          menuButton={<MenuButton>Overflow</MenuButton>}
          overflow={overflow}
          position={position}
          align="end"
        >
          {new Array(50).fill(0).map((_, i) => {
            const item = `Item ${i + 1}`;
            return (
              <MenuItem key={i} onClick={() => setToast(item + ' clicked')}>
                {item}
              </MenuItem>
            );
          })}
        </Menu>
        <Menu
          menuButton={<MenuButton style={{ marginTop: '2rem' }}>Grouping</MenuButton>}
          setDownOverflow
          overflow={overflow}
          position={position}
          boundingBoxPadding="10"
          onMenuChange={(e) => {
            if (e.open) {
              setInput('');
              setFilter('');
            }
          }}
          align="end"
        >
          <FocusableItem style={{ padding: '0.375rem 1rem' }}>
            {({ ref }) => (
              <input
                ref={ref}
                type="text"
                placeholder="Type a number"
                value={input}
                onChange={(e) => {
                  const value = e.target.value;
                  setInput(value);
                  startTransition(() => {
                    setFilter(value.trim());
                  });
                }}
              />
            )}
          </FocusableItem>
          <MenuGroup takeOverflow>
            {new Array(50)
              .fill(0)
              .map((_, i) => `Item ${i + 1}`)
              .filter((item) => item.includes(filter))
              .map((item, i) => (
                <MenuItem key={i} onClick={() => setToast(item + ' clicked')}>
                  {item}
                </MenuItem>
              ))}
          </MenuGroup>
          <MenuItem onClick={() => setToast('Last item clicked')}>Last (fixed)</MenuItem>
        </Menu>
      </div>
    </Example>
  );
}