react-select#SingleValue TypeScript Examples

The following examples show how to use react-select#SingleValue. 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: Code.tsx    From what-the-filter with MIT License 5 votes vote down vote up
Code: React.FC<CodeProps> = ({
  onSubmit,
  width,
  submitOnMount,
}) => {
  const [isCustom, setIsCustom] = useState(false);
  const [editorValue, setEditorValue] = useState('');
  const [selectedExample, setSelectedExample] = useState<Example>(
    EXAMPLES[0].options[0],
  );

  useEffect(() => {
    if (isCustom) {
      setSelectedExample(CUSTOM_OPTION);
    } else {
      setEditorValue(selectedExample.value);
    }
  }, [isCustom, selectedExample]);

  const handleExampleChange = (selected: SingleValue<Example>) => {
    if (!selected) return;
    setIsCustom(false);
    setSelectedExample(selected);
    onSubmit(selected.value, true);
  };

  return (
    <Container>
      <Description>Select an example below...</Description>
      <Select<Example, false>
        value={selectedExample}
        onChange={handleExampleChange}
        options={
          isCustom ? [...EXAMPLES, CUSTOM_OPTION_GROUP] : EXAMPLES
        }
        classNamePrefix="select"
      />

      <Description>... and edit as you will</Description>

      <CodeEditor
        value={editorValue}
        onChange={setEditorValue}
        setIsCustom={setIsCustom}
        onSubmit={onSubmit}
        submitOnMount={submitOnMount}
        width={width}
      />
      <Description>
        (you can also CTRL + S or SHIFT + ENTER)
      </Description>
    </Container>
  );
}