mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2026-07-25 17:34:52 +10:00
Capture the current Keyword in the input in state
Keeps a parent State that tracks if a pending Keyword is in the input field up-to-date
This commit is contained in:
@@ -1,14 +1,15 @@
|
|||||||
import { forwardRef, useCallback, useEffect, useState } from "react";
|
import { Dispatch, forwardRef, SetStateAction, useCallback, useEffect, useState } from "react";
|
||||||
|
|
||||||
import { Input, InputProps } from "./input";
|
import { Input, InputProps } from "./input";
|
||||||
|
|
||||||
type BadgeInputProps = Omit<InputProps, "value" | "onChange"> & {
|
type BadgeInputProps = Omit<InputProps, "value" | "onChange"> & {
|
||||||
value: string[];
|
value: string[];
|
||||||
onChange: (value: string[]) => void;
|
onChange: (value: string[]) => void;
|
||||||
|
setPendingKeyword?: Dispatch<SetStateAction<string>>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const BadgeInput = forwardRef<HTMLInputElement, BadgeInputProps>(
|
export const BadgeInput = forwardRef<HTMLInputElement, BadgeInputProps>(
|
||||||
({ value, onChange, ...props }, ref) => {
|
({ value, onChange, setPendingKeyword, ...props }, ref) => {
|
||||||
const [label, setLabel] = useState("");
|
const [label, setLabel] = useState("");
|
||||||
|
|
||||||
const processInput = useCallback(() => {
|
const processInput = useCallback(() => {
|
||||||
@@ -27,6 +28,10 @@ export const BadgeInput = forwardRef<HTMLInputElement, BadgeInputProps>(
|
|||||||
}
|
}
|
||||||
}, [label, processInput]);
|
}, [label, processInput]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (setPendingKeyword) setPendingKeyword(label);
|
||||||
|
}, [label, setPendingKeyword]);
|
||||||
|
|
||||||
const onKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
|
const onKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
|
||||||
if (event.key === "Enter") {
|
if (event.key === "Enter") {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|||||||
Reference in New Issue
Block a user