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

can't input chinese #237

Closed
tjx666 opened this issue Aug 10, 2022 · 4 comments
Closed

can't input chinese #237

tjx666 opened this issue Aug 10, 2022 · 4 comments
Labels
bug Something isn't working wontfix This will not be worked on

Comments

@tjx666
Copy link

tjx666 commented Aug 10, 2022

should auto remove the letters before the chinese I input.

2022-08-10.20.30.55.mov
@timc1 timc1 added the bug Something isn't working label Aug 25, 2022
@tjx666 tjx666 closed this as completed Sep 14, 2022
@tjx666 tjx666 reopened this Sep 15, 2022
@JaeSeoKim
Copy link

JaeSeoKim commented Sep 21, 2022

The same error occurs in the environment below.

// ...
    "next": "12.3.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
// ...

Errors occur not only in Chinese, but also in CJK(Chines, Japanese, Korean )language, multi byte languages.
The cause seems to be a problem with the synthesis event in React.

This code is a simple correction to the error.

// KBarSearch.tsx
import { useKBar, VisualState } from "kbar";
import React, { useState } from "react";

export const KBAR_LISTBOX = "kbar-listbox";
export const getListboxItemId = (id: number) => `kbar-listbox-item-${id}`;

export function KBarSearch(
  props: React.InputHTMLAttributes<HTMLInputElement> & {
    defaultPlaceholder?: string;
  },
) {
  const { query, searchQuery, actions, currentRootActionId, activeIndex, showing, options } =
    useKBar(state => ({
      searchQuery: state.searchQuery,
      currentRootActionId: state.currentRootActionId,
      actions: state.actions,
      activeIndex: state.activeIndex,
      showing: state.visualState === VisualState.showing,
    }));
  const [search, setSearch] = useState(searchQuery);

  const ownRef = React.useRef<HTMLInputElement>(null);

  const { defaultPlaceholder, ...rest } = props;

  React.useEffect(() => {
    query.setSearch("");
    ownRef.current!.focus();
    return () => query.setSearch("");
  }, [currentRootActionId, query]);

  React.useEffect(() => {
    query.setSearch(search);
  }, [query, search]);

  const placeholder = React.useMemo((): string => {
    const defaultText = defaultPlaceholder ?? "Type a command or search…";
    return currentRootActionId && actions[currentRootActionId]
      ? actions[currentRootActionId].name
      : defaultText;
  }, [actions, currentRootActionId, defaultPlaceholder]);

  return (
    <input
      {...rest}
      ref={ownRef}
      autoFocus
      autoComplete="off"
      role="combobox"
      spellCheck="false"
      aria-expanded={showing}
      aria-controls={KBAR_LISTBOX}
      aria-activedescendant={getListboxItemId(activeIndex)}
      value={search}
      placeholder={placeholder}
      onChange={event => {
        props.onChange?.(event);
        setSearch(event.target.value);
        options?.callbacks?.onQueryChange?.(event.target.value);
      }}
      onKeyDown={event => {
        props.onKeyDown?.(event);
        if (currentRootActionId && !search && event.key === "Backspace") {
          const parent = actions[currentRootActionId].parent;
          query.setCurrentRootAction(parent);
        }
      }}
    />
  );
}

Kamigami55 added a commit to Kamigami55/easonchang.com-next that referenced this issue Oct 8, 2022
@raRaRa
Copy link

raRaRa commented Nov 10, 2022

Same with some icelandic letters, e.g. if I want to type á, then I first type ´ and then a to get á. For some reason this fails in kbar. The result will be: ´a

@bepyan
Copy link

bepyan commented Jan 27, 2023

Same issue with korean.
Above solution will work, but not with "kbar": ^0.1.0-beta.39

Invariant failed: Input is undefined, make sure you apple `query.inputRefSetter` to your search input.

and this will work!

// KBarSearch.tsx
import { useKBar, VisualState } from 'kbar';
import * as React from 'react';

export const KBAR_LISTBOX = 'kbar-listbox';
export const getListboxItemId = (id: number) => `kbar-listbox-item-${id}`;

export function KBarSearch(
  props: React.InputHTMLAttributes<HTMLInputElement> & {
    defaultPlaceholder?: string;
  },
) {
  const { query, searchQuery, actions, currentRootActionId, activeIndex, showing, options } =
    useKBar((state) => ({
      searchQuery: state.searchQuery,
      currentRootActionId: state.currentRootActionId,
      actions: state.actions,
      activeIndex: state.activeIndex,
      showing: state.visualState === VisualState.showing,
    }));
  const [search, setSearch] = React.useState(searchQuery);

  const { defaultPlaceholder, ...rest } = props;

  React.useEffect(() => {
    query.setSearch('');
    query.getInput().focus();
    return () => query.setSearch('');
  }, [currentRootActionId, query]);

  React.useEffect(() => {
    query.setSearch(search);
  }, [query, search]);

  const placeholder = React.useMemo((): string => {
    const defaultText = defaultPlaceholder ?? 'Type a command or search…';
    return currentRootActionId && actions[currentRootActionId]
      ? actions[currentRootActionId].name
      : defaultText;
  }, [actions, currentRootActionId, defaultPlaceholder]);

  return (
    <input
      {...rest}
      ref={query.inputRefSetter}
      autoFocus
      autoComplete="off"
      role="combobox"
      spellCheck="false"
      aria-expanded={showing}
      aria-controls={KBAR_LISTBOX}
      aria-activedescendant={getListboxItemId(activeIndex)}
      value={search}
      placeholder={placeholder}
      onChange={(event) => {
        props.onChange?.(event);
        setSearch(event.target.value);
        options?.callbacks?.onQueryChange?.(event.target.value);
      }}
      onKeyDown={(event) => {
        props.onKeyDown?.(event);
        if (currentRootActionId && !search && event.key === 'Backspace') {
          const parent = actions[currentRootActionId].parent;
          query.setCurrentRootAction(parent);
        }
      }}
    />
  );
}

@stale
Copy link

stale bot commented May 31, 2023

Hey! This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

5 participants