@bouko/react 2.7.4 → 2.7.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.
@@ -0,0 +1,6 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="1em" height="1em">
2
+ <path
3
+ d="M220.6 121.2L271.1 96 448 96l0 96-114.8 0c-21.9-15.1-48.5-24-77.2-24s-55.2 8.9-77.2 24L64 192l0-64 128 0c9.9 0 19.7-2.3 28.6-6.8zM0 128L0 416c0 35.3 28.7 64 64 64l384 0c35.3 0 64-28.7 64-64l0-320c0-35.3-28.7-64-64-64L271.1 32c-9.9 0-19.7 2.3-28.6 6.8L192 64l-32 0 0-16c0-8.8-7.2-16-16-16L80 32c-8.8 0-16 7.2-16 16l0 16C28.7 64 0 92.7 0 128zM168 304a88 88 0 1 1 176 0 88 88 0 1 1 -176 0z"
4
+ fill="currentColor"
5
+ />
6
+ </svg>
@@ -11,6 +11,7 @@ type FieldProps<T> = {
11
11
  };
12
12
  type Variant = "normal" | "ghost";
13
13
  type Props = Omit<FieldProps<string>, "id"> & {
14
+ type?: "text" | "number" | "email" | "password";
14
15
  variant?: Variant;
15
16
  style?: string;
16
17
  placeholder?: string;
@@ -33,5 +34,5 @@ type Props = Omit<FieldProps<string>, "id"> & {
33
34
  * @param {string} placeholder - Placeholder text for the input. (optional)
34
35
  * @param {Function} onEnter - Callback when `Enter` key is pressed. (optional)
35
36
  **/
36
- export default function Input({ variant, style, value, update, onEnter, disabled, placeholder, ...props }: Props): import("react/jsx-runtime").JSX.Element;
37
+ export default function Input({ type, variant, style, value, update, onEnter, disabled, placeholder, ...props }: Props): import("react/jsx-runtime").JSX.Element;
37
38
  export {};
@@ -25,8 +25,8 @@ const fieldStyles = {
25
25
  * @param {string} placeholder - Placeholder text for the input. (optional)
26
26
  * @param {Function} onEnter - Callback when `Enter` key is pressed. (optional)
27
27
  **/
28
- export default function Input({ variant, style, value, update, onEnter, disabled, placeholder, ...props }) {
29
- return (_jsx(Field, { ...props, children: _jsx("input", { className: cn(styles({ variant }), style), placeholder: placeholder, value: value, onChange: ({ target: { value } }) => update?.(value), onKeyUp: ({ key }) => key === "Enter" && value ? onEnter?.(value) : null, disabled: disabled }) }));
28
+ export default function Input({ type = "text", variant, style, value, update, onEnter, disabled, placeholder, ...props }) {
29
+ return (_jsx(Field, { ...props, children: _jsx("input", { type: type, className: cn(styles({ variant }), style), placeholder: placeholder, value: value, onChange: ({ target: { value } }) => update?.(value), onKeyUp: ({ key }) => key === "Enter" && value ? onEnter?.(value) : null, disabled: disabled }) }));
30
30
  }
31
31
  const styles = tv({
32
32
  base: "px-3 py-2 duration-200 rounded text-xs sm:text-sm placeholder:text-primary-darker text-primary disabled:brightness-90 disabled:cursor-not-allowed",
@@ -3,8 +3,8 @@ type Props<T> = {
3
3
  id?: string;
4
4
  style?: string;
5
5
  placeholder?: string;
6
- value?: File;
7
- update?: (x: File) => void;
6
+ value?: string;
7
+ update?: (x: string) => void;
8
8
  updateForm?: Dispatch<SetStateAction<T>>;
9
9
  disabled?: boolean;
10
10
  };
@@ -6,27 +6,34 @@ import Camera from "../../assets/icons/camera.svg";
6
6
  export function ImageUploader({ id, style, placeholder, value, update, updateForm, disabled = false }) {
7
7
  const [preview, setPreview] = useState();
8
8
  useEffect(() => {
9
- if (value) {
10
- const imageUrl = URL.createObjectURL(value);
11
- setPreview(imageUrl);
12
- }
9
+ if (value)
10
+ setPreview(value);
13
11
  }, [value]);
14
12
  const ref = useRef(null);
15
13
  const uploadFile = () => ref.current?.click();
16
14
  if (!update)
17
15
  disabled = true;
18
- const handleFileChange = (e) => {
16
+ const handleFileChange = async (e) => {
19
17
  const file = e.target.files?.[0];
20
18
  if (!file)
21
19
  return;
20
+ const base64 = await fileToBase64(file);
22
21
  if (id && updateForm)
23
- updateForm(x => ({ ...x, [id]: file }));
22
+ updateForm(x => ({ ...x, [id]: base64 }));
24
23
  else
25
- update?.(file);
24
+ update?.(base64);
26
25
  };
27
26
  return (_jsxs("div", { className: cn(styles.container, style), onClick: uploadFile, children: [_jsx("input", { type: "file", className: "hidden", onChange: handleFileChange, ref: ref }), _jsx("img", { className: "absolute top-0 left-0 w-full h-full", src: preview || placeholder, alt: "Image Uploader" }), _jsx("div", { className: "flex justify-center items-center w-full h-full z-10 opacity-0 hover:opacity-50 bg-background-light/80 duration-200 cursor-pointer", children: _jsx(Camera, { className: "text-xl text-primary-light" }) })] }));
28
27
  }
29
28
  const styles = {
30
29
  container: "relative shrink-0 border border-border rounded-md overflow-hidden",
31
30
  };
31
+ function fileToBase64(file) {
32
+ return new Promise((resolve, reject) => {
33
+ const reader = new FileReader();
34
+ reader.onload = () => resolve(reader.result); // data:image/...;base64,...
35
+ reader.onerror = reject;
36
+ reader.readAsDataURL(file);
37
+ });
38
+ }
32
39
  export default ImageUploader;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
 
3
3
  "name": "@bouko/react",
4
- "version": "2.7.4",
4
+ "version": "2.7.6",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "license": "MIT",