enzostvs HF Staff commited on
Commit
6665e07
·
1 Parent(s): 4217770

input field

Browse files
components/input-generation.tsx CHANGED
@@ -2,20 +2,10 @@ import classNames from "classnames";
2
  import { useRef, useState } from "react";
3
  import { HiLightBulb, HiSearch } from "react-icons/hi";
4
  import { useUpdateEffect } from "react-use";
 
5
 
6
- interface Props {
7
- prompt: string;
8
- loading: boolean;
9
- onChange: (value: string) => void;
10
- onSubmit: () => void;
11
- }
12
-
13
- export const InputGeneration: React.FC<Props> = ({
14
- prompt,
15
- loading,
16
- onChange,
17
- onSubmit,
18
- }) => {
19
  const [value, setValue] = useState<string>(prompt);
20
  const input = useRef<HTMLInputElement>(null);
21
 
@@ -35,16 +25,16 @@ export const InputGeneration: React.FC<Props> = ({
35
  className="h-full text-lg placeholder:text-gray-400 text-gray-900 font-medium w-full outline-none truncate"
36
  placeholder="A thug cat riding his small bike"
37
  onChange={(e) => setValue(e.target.value)}
38
- onBlur={() => onChange(value)}
39
  onKeyDown={(e) => {
40
- if (e.key === "Enter" && prompt && !loading) {
41
- onSubmit();
42
  }
43
  }}
44
  />
45
  </div>
46
  <button
47
- disabled={!prompt}
48
  className={classNames(
49
  "bg-primary disabled:bg-gray-300 disabled:text-gray-500 uppercase text-white font-semibold rounded-full px-2 lg:px-4 py-2 text-base transition-all duration-200",
50
  {
@@ -52,8 +42,8 @@ export const InputGeneration: React.FC<Props> = ({
52
  }
53
  )}
54
  onClick={() => {
55
- if (!prompt || loading) return;
56
- onSubmit();
57
  }}
58
  >
59
  <span className="hidden lg:block">
 
2
  import { useRef, useState } from "react";
3
  import { HiLightBulb, HiSearch } from "react-icons/hi";
4
  import { useUpdateEffect } from "react-use";
5
+ import { useInputGeneration } from "./main/hooks/useInputGeneration";
6
 
7
+ export const InputGeneration: React.FC = () => {
8
+ const { prompt, setPrompt, submit, loading } = useInputGeneration();
 
 
 
 
 
 
 
 
 
 
 
9
  const [value, setValue] = useState<string>(prompt);
10
  const input = useRef<HTMLInputElement>(null);
11
 
 
25
  className="h-full text-lg placeholder:text-gray-400 text-gray-900 font-medium w-full outline-none truncate"
26
  placeholder="A thug cat riding his small bike"
27
  onChange={(e) => setValue(e.target.value)}
28
+ onBlur={() => setPrompt(value)}
29
  onKeyDown={(e) => {
30
+ if (e.key === "Enter" && (value || prompt) && !loading) {
31
+ submit();
32
  }
33
  }}
34
  />
35
  </div>
36
  <button
37
+ disabled={!prompt && !value}
38
  className={classNames(
39
  "bg-primary disabled:bg-gray-300 disabled:text-gray-500 uppercase text-white font-semibold rounded-full px-2 lg:px-4 py-2 text-base transition-all duration-200",
40
  {
 
42
  }
43
  )}
44
  onClick={() => {
45
+ if ((!value && !prompt) || loading) return;
46
+ submit();
47
  }}
48
  >
49
  <span className="hidden lg:block">
components/main/hooks/useInputGeneration.ts CHANGED
@@ -1,8 +1,8 @@
1
- import { useId, useState } from "react"
2
  import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
3
  import { useLocalStorage } from 'react-use';
4
 
5
- import { Collection, Image } from "@/utils/type"
6
  import list_styles from "@/assets/list_styles.json"
7
  import { useCollection } from "@/components/modal/useCollection";
8
 
 
1
+ import { useState } from "react"
2
  import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
3
  import { useLocalStorage } from 'react-use';
4
 
5
+ import { Image } from "@/utils/type"
6
  import list_styles from "@/assets/list_styles.json"
7
  import { useCollection } from "@/components/modal/useCollection";
8
 
components/main/index.tsx CHANGED
@@ -24,8 +24,7 @@ const categories = [
24
  ];
25
 
26
  export const Main = () => {
27
- const { prompt, setPrompt, submit, loading, list_styles, style, setStyle } =
28
- useInputGeneration();
29
  const [category, setCategory] = useState<string>("community");
30
  const [advancedSettings, setAdvancedSettings] = useState<boolean>(false);
31
 
@@ -33,12 +32,7 @@ export const Main = () => {
33
  <main className="px-6 z-[2] relative max-w-[1722px] mx-auto">
34
  <div className="py-2 pl-2 pr-2 lg:pr-4 bg-black bg-opacity-30 backdrop-blur-sm lg:sticky lg:top-4 z-10 rounded-full">
35
  <div className="flex flex-col lg:flex-row items-center justify-between w-full">
36
- <InputGeneration
37
- prompt={prompt}
38
- onChange={setPrompt}
39
- onSubmit={submit}
40
- loading={loading}
41
- />
42
  <div className="items-center justify-center lg:justify-end gap-5 w-full mt-6 lg:mt-0 hidden lg:flex">
43
  {categories.map(({ key, label, icon }) => (
44
  <Button
 
24
  ];
25
 
26
  export const Main = () => {
27
+ const { list_styles, style, setStyle } = useInputGeneration();
 
28
  const [category, setCategory] = useState<string>("community");
29
  const [advancedSettings, setAdvancedSettings] = useState<boolean>(false);
30
 
 
32
  <main className="px-6 z-[2] relative max-w-[1722px] mx-auto">
33
  <div className="py-2 pl-2 pr-2 lg:pr-4 bg-black bg-opacity-30 backdrop-blur-sm lg:sticky lg:top-4 z-10 rounded-full">
34
  <div className="flex flex-col lg:flex-row items-center justify-between w-full">
35
+ <InputGeneration />
 
 
 
 
 
36
  <div className="items-center justify-center lg:justify-end gap-5 w-full mt-6 lg:mt-0 hidden lg:flex">
37
  {categories.map(({ key, label, icon }) => (
38
  <Button