@sonamu-kit/react-components 0.4.5 → 0.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2,6 +2,11 @@ import { Override } from '../../lib/types';
|
|
|
2
2
|
import * as React from "react";
|
|
3
3
|
export type InputProps = Override<React.ComponentProps<"input">, {
|
|
4
4
|
onValueChange?: (value: string) => void;
|
|
5
|
+
/**
|
|
6
|
+
* 활성화 시 내용이 채워지면 오른쪽에 X 아이콘이 뜨고, 누르면 내용을 초기화한다.
|
|
7
|
+
* controlled/uncontrolled 입력 모두 지원한다.
|
|
8
|
+
*/
|
|
9
|
+
clearable?: boolean;
|
|
5
10
|
}>;
|
|
6
11
|
declare const Input: React.ForwardRefExoticComponent<Omit<InputProps, "ref"> & React.RefAttributes<HTMLInputElement>>;
|
|
7
12
|
export { Input };
|
|
@@ -1,22 +1,39 @@
|
|
|
1
1
|
import { cn as e } from "../../lib/utils.js";
|
|
2
|
-
import
|
|
3
|
-
import
|
|
2
|
+
import t from "../../_virtual/~icons/lucide/x.js";
|
|
3
|
+
import * as n from "react";
|
|
4
|
+
import { jsx as r, jsxs as i } from "react/jsx-runtime";
|
|
4
5
|
//#region src/components/ui/input.tsx
|
|
5
|
-
var
|
|
6
|
-
let
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
var a = n.forwardRef(({ className: a, type: o, onValueChange: s, onChange: c, clearable: l = !1, ...u }, d) => {
|
|
7
|
+
let f = n.useRef(null), p = n.useCallback((e) => {
|
|
8
|
+
f.current = e, typeof d == "function" ? d(e) : d && (d.current = e);
|
|
9
|
+
}, [d]), m = u.value !== void 0, [h, g] = n.useState(() => String(u.defaultValue ?? "").length > 0), _ = m ? String(u.value ?? "").length > 0 : h, v = (e) => {
|
|
10
|
+
m || g(e.target.value.length > 0), s?.(e.target.value), c?.(e);
|
|
11
|
+
}, y = () => {
|
|
12
|
+
let e = f.current;
|
|
13
|
+
e && ((Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value")?.set)?.call(e, ""), e.dispatchEvent(new Event("input", { bubbles: !0 })), e.focus()), m || g(!1);
|
|
14
|
+
}, b = l && _ && !u.disabled && !u.readOnly, x = /* @__PURE__ */ r("input", {
|
|
15
|
+
type: o,
|
|
11
16
|
"data-slot": "input",
|
|
12
|
-
className: e("file:text-foreground placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 border-input flex h-9 w-full min-w-0 rounded-md border px-3 py-1 text-base bg-input-background transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 md:text-sm", "focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]", "aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive",
|
|
13
|
-
ref:
|
|
14
|
-
...
|
|
15
|
-
onChange:
|
|
17
|
+
className: e("file:text-foreground placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 border-input flex h-9 w-full min-w-0 rounded-md border px-3 py-1 text-base bg-input-background transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 md:text-sm", "focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]", "aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive", l && "pr-8", a),
|
|
18
|
+
ref: p,
|
|
19
|
+
...u,
|
|
20
|
+
onChange: v
|
|
16
21
|
});
|
|
22
|
+
return l ? /* @__PURE__ */ i("div", {
|
|
23
|
+
className: "relative w-full",
|
|
24
|
+
children: [x, b && /* @__PURE__ */ r("button", {
|
|
25
|
+
type: "button",
|
|
26
|
+
tabIndex: -1,
|
|
27
|
+
"aria-label": "Clear",
|
|
28
|
+
onMouseDown: (e) => e.preventDefault(),
|
|
29
|
+
onClick: y,
|
|
30
|
+
className: "absolute right-2 top-1/2 -translate-y-1/2 flex items-center justify-center text-muted-foreground opacity-60 hover:opacity-100 transition-opacity cursor-pointer",
|
|
31
|
+
children: /* @__PURE__ */ r(t, { className: "h-4 w-4" })
|
|
32
|
+
})]
|
|
33
|
+
}) : x;
|
|
17
34
|
});
|
|
18
|
-
|
|
35
|
+
a.displayName = "Input";
|
|
19
36
|
//#endregion
|
|
20
|
-
export {
|
|
37
|
+
export { a as Input };
|
|
21
38
|
|
|
22
39
|
//# sourceMappingURL=input.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"input.js","names":[],"sources":["../../../src/components/ui/input.tsx"],"sourcesContent":["import * as React from \"react\";\n\nimport { type Override } from \"../../lib/types\";\nimport { cn } from \"../../lib/utils\";\n\nexport type InputProps = Override<\n React.ComponentProps<\"input\">,\n {\n onValueChange?: (value: string) => void;\n }\n>;\n\nconst Input = React.forwardRef<HTMLInputElement, InputProps>(\n ({ className, type, onValueChange, onChange, ...props }, ref) => {\n const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {\n onValueChange?.(e.target.value);\n onChange?.(e);\n };\n\n
|
|
1
|
+
{"version":3,"file":"input.js","names":[],"sources":["../../../src/components/ui/input.tsx"],"sourcesContent":["import * as React from \"react\";\nimport XIcon from \"~icons/lucide/x\";\n\nimport { type Override } from \"../../lib/types\";\nimport { cn } from \"../../lib/utils\";\n\nexport type InputProps = Override<\n React.ComponentProps<\"input\">,\n {\n onValueChange?: (value: string) => void;\n /**\n * 활성화 시 내용이 채워지면 오른쪽에 X 아이콘이 뜨고, 누르면 내용을 초기화한다.\n * controlled/uncontrolled 입력 모두 지원한다.\n */\n clearable?: boolean;\n }\n>;\n\nconst Input = React.forwardRef<HTMLInputElement, InputProps>(\n ({ className, type, onValueChange, onChange, clearable = false, ...props }, ref) => {\n const innerRef = React.useRef<HTMLInputElement>(null);\n\n // 전달된 ref와 내부 ref를 함께 채우는 콜백 ref\n const setRefs = React.useCallback(\n (node: HTMLInputElement | null) => {\n innerRef.current = node;\n if (typeof ref === \"function\") {\n ref(node);\n } else if (ref) {\n ref.current = node;\n }\n },\n [ref],\n );\n\n // controlled면 value로, uncontrolled면 내부 상태로 내용 유무를 판단한다.\n const isControlled = props.value !== undefined;\n const [uncontrolledHasValue, setUncontrolledHasValue] = React.useState(\n () => String(props.defaultValue ?? \"\").length > 0,\n );\n const hasValue = isControlled ? String(props.value ?? \"\").length > 0 : uncontrolledHasValue;\n\n const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {\n if (!isControlled) {\n setUncontrolledHasValue(e.target.value.length > 0);\n }\n onValueChange?.(e.target.value);\n onChange?.(e);\n };\n\n const handleClear = () => {\n const node = innerRef.current;\n if (node) {\n // native value setter로 값을 비우고 input 이벤트를 dispatch하면\n // React의 value tracker가 변화를 인식해 controlled/uncontrolled 모두에서 onChange가 발화된다.\n const nativeInputValueSetter = Object.getOwnPropertyDescriptor(\n window.HTMLInputElement.prototype,\n \"value\",\n )?.set;\n nativeInputValueSetter?.call(node, \"\");\n node.dispatchEvent(new Event(\"input\", { bubbles: true }));\n node.focus();\n }\n if (!isControlled) {\n setUncontrolledHasValue(false);\n }\n };\n\n const showClear = clearable && hasValue && !props.disabled && !props.readOnly;\n\n const inputEl = (\n <input\n type={type}\n data-slot=\"input\"\n className={cn(\n \"file:text-foreground placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 border-input flex h-9 w-full min-w-0 rounded-md border px-3 py-1 text-base bg-input-background transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 md:text-sm\",\n \"focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]\",\n \"aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive\",\n clearable && \"pr-8\",\n className,\n )}\n ref={setRefs}\n {...props}\n onChange={handleChange}\n />\n );\n\n if (!clearable) {\n return inputEl;\n }\n\n return (\n <div className=\"relative w-full\">\n {inputEl}\n {showClear && (\n <button\n type=\"button\"\n tabIndex={-1}\n aria-label=\"Clear\"\n onMouseDown={(e) => e.preventDefault()}\n onClick={handleClear}\n className=\"absolute right-2 top-1/2 -translate-y-1/2 flex items-center justify-center text-muted-foreground opacity-60 hover:opacity-100 transition-opacity cursor-pointer\"\n >\n <XIcon className=\"h-4 w-4\" />\n </button>\n )}\n </div>\n );\n },\n);\nInput.displayName = \"Input\";\n\nexport { Input };\n"],"mappings":";;;;;AAkBA,IAAM,IAAQ,EAAM,YACjB,EAAE,cAAW,SAAM,kBAAe,aAAU,eAAY,IAAO,GAAG,KAAS,MAAQ;CAClF,IAAM,IAAW,EAAM,OAAyB,IAAI,GAG9C,IAAU,EAAM,aACnB,MAAkC;EAEjC,AADA,EAAS,UAAU,GACf,OAAO,KAAQ,aACjB,EAAI,CAAI,IACC,MACT,EAAI,UAAU;CAElB,GACA,CAAC,CAAG,CACN,GAGM,IAAe,EAAM,UAAU,KAAA,GAC/B,CAAC,GAAsB,KAA2B,EAAM,eACtD,OAAO,EAAM,gBAAgB,EAAE,EAAE,SAAS,CAClD,GACM,IAAW,IAAe,OAAO,EAAM,SAAS,EAAE,EAAE,SAAS,IAAI,GAEjE,KAAgB,MAA2C;EAK/D,AAJK,KACH,EAAwB,EAAE,OAAO,MAAM,SAAS,CAAC,GAEnD,IAAgB,EAAE,OAAO,KAAK,GAC9B,IAAW,CAAC;CACd,GAEM,UAAoB;EACxB,IAAM,IAAO,EAAS;EAYtB,AAXI,OAG6B,OAAO,yBACpC,OAAO,iBAAiB,WACxB,OACF,GAAG,MACqB,KAAK,GAAM,EAAE,GACrC,EAAK,cAAc,IAAI,MAAM,SAAS,EAAE,SAAS,GAAK,CAAC,CAAC,GACxD,EAAK,MAAM,IAER,KACH,EAAwB,EAAK;CAEjC,GAEM,IAAY,KAAa,KAAY,CAAC,EAAM,YAAY,CAAC,EAAM,UAE/D,IACJ,kBAAC,SAAD;EACQ;EACN,aAAU;EACV,WAAW,EACT,8bACA,iFACA,0GACA,KAAa,QACb,CACF;EACA,KAAK;EACL,GAAI;EACJ,UAAU;CACX,CAAA;CAOH,OAJK,IAKH,kBAAC,OAAD;EAAK,WAAU;YAAf,CACG,GACA,KACC,kBAAC,UAAD;GACE,MAAK;GACL,UAAU;GACV,cAAW;GACX,cAAc,MAAM,EAAE,eAAe;GACrC,SAAS;GACT,WAAU;aAEV,kBAAC,GAAD,EAAO,WAAU,UAAW,CAAA;EACtB,CAAA,CAEP;MAlBE;AAoBX,CACF;AACA,EAAM,cAAc"}
|