Skip to content

Commit

Permalink
fix: graphemes 디폴트 값 지정, type better
Browse files Browse the repository at this point in the history
  • Loading branch information
te6-in committed Dec 4, 2024
1 parent 1c059de commit 0b7098b
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions packages/react-headless/text-field/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,33 @@ export function useTextFieldState({
defaultProp: defaultValue,
onChange: onValueChange,
});
const [graphemes, setGraphemes] = useState<string[]>([]);
const [graphemes, setGraphemes] = useState<string[]>(() =>
getSlicedGraphemes({ value, maxGraphemeCount }),
);

const [isHovered, setIsHovered] = useState(false);
const [isPressed, setIsPressed] = useState(false);
const [isFocused, setIsFocused] = useState(false);
const [isFocusVisible, setIsFocusVisible] = useState(false);

const onGraphemesChange = useEvent(__onGraphemesChange);

const updateValue = useCallback(
const handleValueChange = useCallback(
(newValue: string) => {
const slicedGraphemes = getSlicedGraphemes({
maxGraphemeCount,
const newGraphemes = getSlicedGraphemes({
value: newValue,
maxGraphemeCount,
});

const value = slicedGraphemes.join("");

setValue(value);
setGraphemes(slicedGraphemes);
setValue(newValue);
setGraphemes(newGraphemes);
},
[maxGraphemeCount, graphemes.length, setValue],
[maxGraphemeCount, setValue],
);

useEffect(() => onGraphemesChange(graphemes), [graphemes, onGraphemesChange]);
useEffect(() => {
onGraphemesChange(graphemes);
}, [graphemes, onGraphemesChange]);

return {
value,
Expand All @@ -84,7 +87,7 @@ export function useTextFieldState({
isFocused,
isFocusVisible,

updateValue,
handleValueChange,
setIsHovered,
setIsPressed,
setIsFocused,
Expand Down Expand Up @@ -116,9 +119,9 @@ export interface UseTextFieldProps extends UseTextFieldStateProps {
description?: string;
errorMessage?: string;

onChange?: (e: ChangeEvent) => void;
onFocus?: (e: FocusEvent) => void;
onBlur?: (e: FocusEvent) => void;
onChange?: (event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
onFocus?: (event: FocusEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
onBlur?: (event: FocusEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
}

export function useTextField(props: UseTextFieldProps) {
Expand All @@ -143,17 +146,17 @@ export function useTextField(props: UseTextFieldProps) {
} = props;

const {
updateValue,
value: stateValue,
graphemes,
setIsHovered,
isHovered,
setIsPressed,
isPressed,
setIsFocused,
isFocused,
setIsFocusVisible,
isFocusVisible,
handleValueChange,
setIsHovered,
setIsPressed,
setIsFocused,
setIsFocusVisible,
} = useTextFieldState({
value: propValue,
defaultValue,
Expand Down Expand Up @@ -187,10 +190,11 @@ export function useTextField(props: UseTextFieldProps) {
return {
value: stateValue,
graphemes,
updateValue,
isFocused,
isInvalid: invalid,
isRequired: required,

handleValueChange,
setIsFocused,
setIsFocusVisible,

Expand Down Expand Up @@ -225,44 +229,46 @@ export function useTextField(props: UseTextFieldProps) {

inputProps: inputProps({
...stateProps,
disabled,
readOnly,
...(propValue && { value: stateValue }),
...(label && { "aria-labelledby": getLabelId(id) }),
"aria-describedby": ariaDescribedBy,
"aria-required": ariaAttr(required),
"aria-invalid": ariaAttr(invalid),
"aria-describedby": ariaDescribedBy,
...(label && { "aria-labelledby": getLabelId(id) }),
disabled,
readOnly,
id: getInputId(id),
name: props.name || id,
onChange: (event) => {
const givenValue = event.target.value;
handleValueChange(event.target.value);

updateValue(givenValue);
setIsFocusVisible(event.target.matches(":focus-visible"));

onChange?.(event);
},
onBlur(event) {
setIsFocused(false);
setIsFocusVisible(false);

onBlur?.(event);
},
onFocus(event) {
setIsFocused(true);
setIsFocusVisible(event.target.matches(":focus-visible"));

onFocus?.(event);
},
name: props.name || id,
id: getInputId(id),
...(propValue && { value: stateValue }),
}) as unknown as InputHTMLAttributes<HTMLInputElement> &
TextareaHTMLAttributes<HTMLTextAreaElement>,

descriptionProps: elementProps({
id: getDescriptionId(id),
...(invalid && { style: { display: "none" } }),
...stateProps,
...(invalid && { style: { display: "none" } }),
id: getDescriptionId(id),
}),

errorMessageProps: elementProps({
id: getErrorMessageId(id),
...stateProps,
id: getErrorMessageId(id),
}),
};
}

0 comments on commit 0b7098b

Please sign in to comment.