[React] React hook, component props for refactoring
Idea is put component props inside hook, and return from hook, that's way to keep component clean and simple
Hook:
import { MachineOptions } from "./machine";
import { machine } from "./machine";
import { useMachine } from "@zag-js/react";
import { ComponentProps } from "react";
type LabelProps = ComponentProps<"label"> & { "data-part": "label" };
type InputProps = ComponentProps<"input"> & {
"data-part": string;
};
export function usePinMachine(options: MachineOptions) {
const [state, send] = useMachine(machine(options));
const { value, name } = state.context;
const valueAsString = value.join("");
return {
value,
valueAsString,
getLabelProps(): LabelProps {
return {
"data-part": "label",
onClick() {
send({ type: "LABEL_CLICK" });
},
};
},
getHiddenInput(): InputProps {
return {
"data-part": "hidden-input",
type: "hidden",
name,
value: value.join(""),
};
},
getInputProps({ index }: { index: number }): InputProps {
return {
"data-part": "input",
value: value[index],
maxLength: 2,
onChange(event) {
const { value } = event.target;
send({ type: "INPUT", index, value });
},
onFocus() {
send({ type: "FOCUS", index });
},
onBlur() {
send({ type: "BLUR" });
},
onKeyDown(event) {
const { key } = event;
if (key === "Backspace") {
send({ type: "BACKSPACE", index });
}
},
onPaste(event) {
event.preventDefault();
const value = event.clipboardData.getData("Text").trim();
send({ type: "PASTE", value, index });
},
};
},
};
}
Component:
import "./App.css";
import { usePinMachine } from "./use-pin-machine";
function App() {
const { value, getLabelProps, getHiddenInput, getInputProps } = usePinMachine(
{
numOfFields: 4,
name: "pincode",
onCompleted(value) {
console.log(value);
},
}
);
return (
<div className="App">
<form
onSubmit={(event) => {
event.preventDefault();
const formData = new FormData(event.currentTarget);
}}
>
<div data-part="container">
<label {...getLabelProps()}>Enter verification</label>
<input {...getHiddenInput()} />
<div data-part="input-group">
{value.map((_, index) => (
<input key={index} {...getInputProps({ index })} />
))}
</div>
</div>
<button type="submit">Submit</button>
</form>
</div>
);
}
export default App;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2020-02-19 【逻辑思维】排中律:生存还是毁灭,只能选一个
2020-02-19 【逻辑思维】矛盾律:谁给理发师理发
2020-02-19 [AST Babel] Create a simple babel plugin
2020-02-19 [Angular 8 Unit testing] Testing a smart component with service injection -- 1
2019-02-19 [React] Ensure all React useEffect Effects Run Synchronously in Tests with react-testing-library
2019-02-19 [Functional Programming] Write a simple version of Maybe
2019-02-19 [JS Compose] 3. Use chain for composable error handling with nested Eithers (flatMap)