@kashi-ui/react 0.22.1 → 0.23.0

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,28 @@
1
+ import { type ReactNode } from "react";
2
+ import { type IranAddress } from "@kashi-ui/persian";
3
+ type Field = keyof IranAddress;
4
+ export interface AddressFormProps {
5
+ value?: IranAddress;
6
+ defaultValue?: Partial<IranAddress>;
7
+ onChange?: (address: IranAddress) => void;
8
+ /** خطای هر فیلد، مثلاً از اعتبارسنجی سرور */
9
+ errors?: Partial<Record<Field, ReactNode>>;
10
+ /** پیشوند نام فیلدها در فرم HTML: name="shipping" ← shipping[province]، shipping[city]، … */
11
+ name?: string;
12
+ /** عنوان فیلدها (legend) @default «نشانی» */
13
+ legend?: ReactNode;
14
+ /** برچسب هر فیلد */
15
+ labels?: Partial<Record<Field, string>>;
16
+ /** نقشهٔ ایران کنار فیلدها برای انتخاب استان @default false */
17
+ map?: boolean;
18
+ /** فیلد واحد اختیاری است و بقیه اجباری @default true */
19
+ required?: boolean;
20
+ disabled?: boolean;
21
+ className?: string;
22
+ }
23
+ /**
24
+ * فرم نشانی ایرانی: استان، شهرِ همان استان، نشانی، پلاک، واحد و کد پستی ده‌رقمی.
25
+ * با انتخاب شهر بدون استان، استان خودکار پر می‌شود؛ با عوض شدن استان، شهرِ نامربوط پاک می‌شود.
26
+ */
27
+ export declare function AddressForm({ value, defaultValue, onChange, errors, name, legend, labels: labelsProp, map, required, disabled, className, }: AddressFormProps): import("react").JSX.Element;
28
+ export {};
@@ -0,0 +1,33 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
+ import { useState } from "react";
4
+ import { EMPTY_ADDRESS, getCities } from "@kashi-ui/persian";
5
+ import { cx } from "./cx.js";
6
+ import { CitySelect } from "./CitySelect.js";
7
+ import { Input, Textarea } from "./Input.js";
8
+ import { IranMap } from "./IranMap.js";
9
+ import { PostalCodeInput } from "./PostalCodeInput.js";
10
+ import { ProvinceSelect } from "./ProvinceSelect.js";
11
+ const LABELS = { province: "استان", city: "شهر", street: "نشانی", plaque: "پلاک", unit: "واحد", postalCode: "کد پستی" };
12
+ /**
13
+ * فرم نشانی ایرانی: استان، شهرِ همان استان، نشانی، پلاک، واحد و کد پستی ده‌رقمی.
14
+ * با انتخاب شهر بدون استان، استان خودکار پر می‌شود؛ با عوض شدن استان، شهرِ نامربوط پاک می‌شود.
15
+ */
16
+ export function AddressForm({ value, defaultValue, onChange, errors = {}, name, legend = "نشانی", labels: labelsProp, map = false, required = true, disabled, className, }) {
17
+ const [inner, setInner] = useState({ ...EMPTY_ADDRESS, ...defaultValue });
18
+ const address = value ?? inner;
19
+ const labels = { ...LABELS, ...labelsProp };
20
+ const field = (key) => (name ? `${name}[${key}]` : undefined);
21
+ const update = (patch) => {
22
+ const next = { ...address, ...patch };
23
+ if (value === undefined)
24
+ setInner(next);
25
+ onChange?.(next);
26
+ };
27
+ const setProvince = (province) => {
28
+ // شهر فقط وقتی می‌ماند که در استان تازه هم باشد (شهرهای خارج از فهرست هم پاک می‌شوند)
29
+ const keep = province && address.city && getCities(province).includes(address.city);
30
+ update({ province, city: keep ? address.city : null });
31
+ };
32
+ return (_jsxs("fieldset", { className: cx("k-address", map && "k-address--map", className), disabled: disabled, children: [legend && _jsx("legend", { className: "k-address__legend", children: legend }), _jsxs("div", { className: "k-address__fields", children: [_jsx(ProvinceSelect, { className: "k-address__province", label: labels.province, value: address.province, onChange: setProvince, error: errors.province, required: required, showCapital: false, placeholder: "\u062C\u0633\u062A\u062C\u0648\u06CC \u0627\u0633\u062A\u0627\u0646" }), _jsx(CitySelect, { className: "k-address__city", label: labels.city, province: address.province, value: address.city, onChange: (city, province) => update({ city, ...(province && !address.province ? { province } : {}) }), error: errors.city, required: required }), _jsx(Textarea, { fieldClassName: "k-address__street", label: labels.street, value: address.street, onChange: (e) => update({ street: e.target.value }), error: errors.street, required: required, name: field("street"), autoComplete: "street-address", placeholder: "\u062E\u06CC\u0627\u0628\u0627\u0646\u060C \u06A9\u0648\u0686\u0647\u060C \u0646\u0627\u0645 \u0633\u0627\u062E\u062A\u0645\u0627\u0646", rows: 2, autoResize: true }), _jsx(Input, { fieldClassName: "k-address__plaque", label: labels.plaque, value: address.plaque, onChange: (e) => update({ plaque: e.target.value }), error: errors.plaque, required: required, name: field("plaque"), inputMode: "numeric", autoComplete: "address-line2" }), _jsx(Input, { fieldClassName: "k-address__unit", label: labels.unit, value: address.unit, onChange: (e) => update({ unit: e.target.value }), error: errors.unit, optional: required, name: field("unit"), inputMode: "numeric", autoComplete: "address-line3" }), _jsx(PostalCodeInput, { fieldClassName: "k-address__postal", label: labels.postalCode, value: address.postalCode, onValueChange: (postalCode) => update({ postalCode }), error: errors.postalCode, required: required, name: field("postalCode") })] }), map && (_jsx(IranMap, { className: "k-address__map", label: `${labels.province} روی نقشه`, value: address.province, onChange: setProvince })), name && (_jsxs(_Fragment, { children: [_jsx("input", { type: "hidden", name: field("province"), value: address.province ?? "" }), _jsx("input", { type: "hidden", name: field("city"), value: address.city ?? "" })] }))] }));
33
+ }
@@ -0,0 +1,14 @@
1
+ import { type ComboboxProps } from "./Combobox.js";
2
+ export interface CitySelectProps extends Omit<ComboboxProps, "options" | "value" | "defaultValue" | "onChange"> {
3
+ /** فقط شهرهای این استان؛ بدون استان، همهٔ شهرها با نام استانشان */
4
+ province?: string | null;
5
+ /** نام شهر */
6
+ value?: string | null;
7
+ defaultValue?: string | null;
8
+ /** استان هم برمی‌گردد تا بدون انتخاب استان، از روی شهر پر شود؛ برای شهر خارج از فهرست null */
9
+ onChange?: (city: string | null, province: string | null) => void;
10
+ /** وقتی شهر در فهرست نیست، همان متن تایپ‌شده پذیرفته شود (شهرهای تازه‌تأسیس) @default true */
11
+ allowCustom?: boolean;
12
+ }
13
+ /** انتخاب شهر با جستجو؛ با استان فقط شهرهای همان استان، بدون آن همهٔ ۱۲۰۰+ شهر ایران با نام استان */
14
+ export declare const CitySelect: import("react").ForwardRefExoticComponent<CitySelectProps & import("react").RefAttributes<HTMLInputElement>>;
@@ -0,0 +1,39 @@
1
+ "use client";
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { forwardRef, useMemo, useState } from "react";
4
+ import { CITIES, getCities } from "@kashi-ui/persian";
5
+ import { Combobox, searchText } from "./Combobox.js";
6
+ const SEP = "|";
7
+ // «علی آباد»، «علی‌آباد» و «علیآباد» یکی‌اند
8
+ const loose = (s) => searchText(s).replace(/[\s‌]/g, "");
9
+ const filter = (o, q) => o.value.startsWith(`custom${SEP}`) || [o.label, o.description ?? ""].some((t) => loose(t).includes(loose(q)));
10
+ /** انتخاب شهر با جستجو؛ با استان فقط شهرهای همان استان، بدون آن همهٔ ۱۲۰۰+ شهر ایران با نام استان */
11
+ export const CitySelect = forwardRef(function CitySelect({ province, value, defaultValue = null, onChange, allowCustom = true, label = "شهر", placeholder, onInputChange, ...rest }, ref) {
12
+ const [query, setQuery] = useState("");
13
+ const [inner, setInner] = useState(defaultValue);
14
+ const city = value !== undefined ? value : inner;
15
+ const options = useMemo(() => {
16
+ const list = province
17
+ ? getCities(province).map((c) => ({ value: `${province}${SEP}${c}`, label: c }))
18
+ : Object.entries(CITIES).flatMap(([p, cities]) => cities.map((c) => ({ value: `${p}${SEP}${c}`, label: c, description: p })));
19
+ const q = query.trim();
20
+ const known = list.some((o) => o.label === city);
21
+ // شهر انتخاب‌شده‌ای که در فهرست نیست هم باید در فیلد بماند
22
+ if (city && !known)
23
+ list.unshift({ value: `custom${SEP}${city}`, label: city, description: "خارج از فهرست" });
24
+ if (allowCustom && q && q !== city && !list.some((o) => loose(o.label) === loose(q)))
25
+ list.push({ value: `custom${SEP}${q}`, label: q, description: "شهر دیگر؛ همین نام ثبت شود" });
26
+ return list;
27
+ }, [province, query, city, allowCustom]);
28
+ const selected = city ? options.find((o) => o.label === city && (!province || o.value.startsWith(`${province}${SEP}`) || o.value.startsWith(`custom${SEP}`)))?.value ?? null : null;
29
+ return (_jsx(Combobox, { ref: ref, label: label, placeholder: placeholder ?? (province ? "جستجوی شهر" : "نام شهر، مثلاً کاشان"), options: options, filter: filter, value: selected, onInputChange: (q) => {
30
+ setQuery(q);
31
+ onInputChange?.(q);
32
+ }, onChange: (v) => {
33
+ const [p, c] = v ? v.split(SEP) : [null, null];
34
+ const next = c ?? null;
35
+ if (value === undefined)
36
+ setInner(next);
37
+ onChange?.(next, p === "custom" ? null : p);
38
+ }, ...rest }));
39
+ });
package/dist/index.d.ts CHANGED
@@ -123,6 +123,10 @@ export type { InlineEditProps } from "./InlineEdit.js";
123
123
  export { IranMap } from "./IranMap.js";
124
124
  export type { IranMapProps } from "./IranMap.js";
125
125
  export { IRAN_PROVINCES, IRAN_VIEWBOX } from "./iranMapData.js";
126
+ export { CitySelect } from "./CitySelect.js";
127
+ export type { CitySelectProps } from "./CitySelect.js";
128
+ export { AddressForm } from "./AddressForm.js";
129
+ export type { AddressFormProps } from "./AddressForm.js";
126
130
  export { ProvinceSelect } from "./ProvinceSelect.js";
127
131
  export type { ProvinceSelectProps } from "./ProvinceSelect.js";
128
132
  export { Carousel } from "./Carousel.js";
package/dist/index.js CHANGED
@@ -62,6 +62,8 @@ export { ContextMenu } from "./ContextMenu.js";
62
62
  export { InlineEdit } from "./InlineEdit.js";
63
63
  export { IranMap } from "./IranMap.js";
64
64
  export { IRAN_PROVINCES, IRAN_VIEWBOX } from "./iranMapData.js";
65
+ export { CitySelect } from "./CitySelect.js";
66
+ export { AddressForm } from "./AddressForm.js";
65
67
  export { ProvinceSelect } from "./ProvinceSelect.js";
66
68
  export { Carousel } from "./Carousel.js";
67
69
  export { ColorPicker, contrastRatio } from "./ColorPicker.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kashi-ui/react",
3
- "version": "0.22.1",
3
+ "version": "0.23.0",
4
4
  "description": "کامپوننت‌های React دیزاین‌سیستم کاشی",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -22,7 +22,7 @@
22
22
  "test": "node --test test/*.test.mjs"
23
23
  },
24
24
  "peerDependencies": {
25
- "@kashi-ui/css": "^0.22.1",
25
+ "@kashi-ui/css": "^0.23.0",
26
26
  "react": ">=18"
27
27
  },
28
28
  "devDependencies": {
@@ -59,7 +59,7 @@
59
59
  }
60
60
  },
61
61
  "dependencies": {
62
- "@kashi-ui/bank-logos": "^0.22.1",
63
- "@kashi-ui/persian": "^0.22.1"
62
+ "@kashi-ui/bank-logos": "^0.23.0",
63
+ "@kashi-ui/persian": "^0.23.0"
64
64
  }
65
65
  }