123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- import { withDirectives as _withDirectives, vShow as _vShow, createVNode as _createVNode } from "vue";
- import { ref, watch, computed, nextTick, reactive, defineComponent } from "vue";
- import { extend, isObject, isMobile, truthProp, numericProp, makeArrayProp, makeNumericProp, createNamespace } from "../utils/index.mjs";
- import { useExpose } from "../composables/use-expose.mjs";
- import { Area } from "../area/index.mjs";
- import { Cell } from "../cell/index.mjs";
- import { Form } from "../form/index.mjs";
- import { Field } from "../field/index.mjs";
- import { Popup } from "../popup/index.mjs";
- import { Toast } from "../toast/index.mjs";
- import { Button } from "../button/index.mjs";
- import { Switch } from "../switch/index.mjs";
- import AddressEditDetail from "./AddressEditDetail.mjs";
- const [name, bem, t] = createNamespace("address-edit");
- const DEFAULT_DATA = {
- name: "",
- tel: "",
- city: "",
- county: "",
- country: "",
- province: "",
- areaCode: "",
- isDefault: false,
- postalCode: "",
- addressDetail: ""
- };
- const isPostal = (value) => /^\d{6}$/.test(value);
- const addressEditProps = {
- areaList: Object,
- isSaving: Boolean,
- isDeleting: Boolean,
- validator: Function,
- showArea: truthProp,
- showDetail: truthProp,
- showDelete: Boolean,
- showPostal: Boolean,
- disableArea: Boolean,
- searchResult: Array,
- telMaxlength: numericProp,
- showSetDefault: Boolean,
- saveButtonText: String,
- areaPlaceholder: String,
- deleteButtonText: String,
- showSearchResult: Boolean,
- detailRows: makeNumericProp(1),
- detailMaxlength: makeNumericProp(200),
- areaColumnsPlaceholder: makeArrayProp(),
- addressInfo: {
- type: Object,
- default: () => extend({}, DEFAULT_DATA)
- },
- telValidator: {
- type: Function,
- default: isMobile
- },
- postalValidator: {
- type: Function,
- default: isPostal
- }
- };
- var stdin_default = defineComponent({
- name,
- props: addressEditProps,
- emits: ["save", "focus", "delete", "click-area", "change-area", "change-detail", "select-search", "change-default"],
- setup(props, {
- emit,
- slots
- }) {
- const areaRef = ref();
- const data = reactive({});
- const showAreaPopup = ref(false);
- const detailFocused = ref(false);
- const areaListLoaded = computed(() => isObject(props.areaList) && Object.keys(props.areaList).length);
- const areaText = computed(() => {
- const {
- country,
- province,
- city,
- county,
- areaCode
- } = data;
- if (areaCode) {
- const arr = [country, province, city, county];
- if (province && province === city) {
- arr.splice(1, 1);
- }
- return arr.filter(Boolean).join("/");
- }
- return "";
- });
- const hideBottomFields = computed(() => {
- var _a;
- return ((_a = props.searchResult) == null ? void 0 : _a.length) && detailFocused.value;
- });
- const assignAreaValues = () => {
- if (areaRef.value) {
- const detail = areaRef.value.getArea();
- detail.areaCode = detail.code;
- delete detail.code;
- extend(data, detail);
- }
- };
- const onFocus = (key) => {
- detailFocused.value = key === "addressDetail";
- emit("focus", key);
- };
- const rules = computed(() => {
- const {
- validator,
- telValidator,
- postalValidator
- } = props;
- const makeRule = (name2, emptyMessage) => ({
- validator: (value) => {
- if (validator) {
- const message = validator(name2, value);
- if (message) {
- return message;
- }
- }
- if (!value) {
- return emptyMessage;
- }
- return true;
- }
- });
- return {
- name: [makeRule("name", t("nameEmpty"))],
- tel: [makeRule("tel", t("telInvalid")), {
- validator: telValidator,
- message: t("telInvalid")
- }],
- areaCode: [makeRule("areaCode", t("areaEmpty"))],
- addressDetail: [makeRule("addressDetail", t("addressEmpty"))],
- postalCode: [makeRule("addressDetail", t("postalEmpty")), {
- validator: postalValidator,
- message: t("postalEmpty")
- }]
- };
- });
- const onSave = () => emit("save", data);
- const onChangeDetail = (val) => {
- data.addressDetail = val;
- emit("change-detail", val);
- };
- const onAreaConfirm = (values) => {
- values = values.filter(Boolean);
- if (values.some((value) => !value.code)) {
- Toast(t("areaEmpty"));
- } else {
- showAreaPopup.value = false;
- assignAreaValues();
- emit("change-area", values);
- }
- };
- const onDelete = () => emit("delete", data);
- const getArea = () => {
- var _a;
- return ((_a = areaRef.value) == null ? void 0 : _a.getValues()) || [];
- };
- const setAreaCode = (code) => {
- data.areaCode = code || "";
- if (code) {
- nextTick(assignAreaValues);
- }
- };
- const onDetailBlur = () => {
- setTimeout(() => {
- detailFocused.value = false;
- });
- };
- const setAddressDetail = (value) => {
- data.addressDetail = value;
- };
- const renderSetDefaultCell = () => {
- if (props.showSetDefault) {
- const slots2 = {
- "right-icon": () => _createVNode(Switch, {
- "modelValue": data.isDefault,
- "onUpdate:modelValue": ($event) => data.isDefault = $event,
- "size": "24",
- "onChange": (event) => emit("change-default", event)
- }, null)
- };
- return _withDirectives(_createVNode(Cell, {
- "center": true,
- "title": t("defaultAddress"),
- "class": bem("default")
- }, slots2), [[_vShow, !hideBottomFields.value]]);
- }
- };
- useExpose({
- getArea,
- setAreaCode,
- setAddressDetail
- });
- watch(() => props.areaList, () => setAreaCode(data.areaCode));
- watch(() => props.addressInfo, (value) => {
- extend(data, DEFAULT_DATA, value);
- setAreaCode(value.areaCode);
- }, {
- deep: true,
- immediate: true
- });
- return () => {
- const {
- disableArea
- } = props;
- return _createVNode(Form, {
- "class": bem(),
- "onSubmit": onSave
- }, {
- default: () => {
- var _a;
- return [_createVNode("div", {
- "class": bem("fields")
- }, [_createVNode(Field, {
- "modelValue": data.name,
- "onUpdate:modelValue": ($event) => data.name = $event,
- "clearable": true,
- "label": t("name"),
- "rules": rules.value.name,
- "placeholder": t("name"),
- "onFocus": () => onFocus("name")
- }, null), _createVNode(Field, {
- "modelValue": data.tel,
- "onUpdate:modelValue": ($event) => data.tel = $event,
- "clearable": true,
- "type": "tel",
- "label": t("tel"),
- "rules": rules.value.tel,
- "maxlength": props.telMaxlength,
- "placeholder": t("tel"),
- "onFocus": () => onFocus("tel")
- }, null), _withDirectives(_createVNode(Field, {
- "readonly": true,
- "label": t("area"),
- "is-link": !disableArea,
- "modelValue": areaText.value,
- "rules": rules.value.areaCode,
- "placeholder": props.areaPlaceholder || t("area"),
- "onFocus": () => onFocus("areaCode"),
- "onClick": () => {
- emit("click-area");
- showAreaPopup.value = !disableArea;
- }
- }, null), [[_vShow, props.showArea]]), _createVNode(AddressEditDetail, {
- "show": props.showDetail,
- "rows": props.detailRows,
- "rules": rules.value.addressDetail,
- "value": data.addressDetail,
- "focused": detailFocused.value,
- "maxlength": props.detailMaxlength,
- "searchResult": props.searchResult,
- "showSearchResult": props.showSearchResult,
- "onBlur": onDetailBlur,
- "onFocus": () => onFocus("addressDetail"),
- "onInput": onChangeDetail,
- "onSelect-search": (event) => emit("select-search", event)
- }, null), props.showPostal && _withDirectives(_createVNode(Field, {
- "modelValue": data.postalCode,
- "onUpdate:modelValue": ($event) => data.postalCode = $event,
- "type": "tel",
- "rules": rules.value.postalCode,
- "label": t("postal"),
- "maxlength": "6",
- "placeholder": t("postal"),
- "onFocus": () => onFocus("postalCode")
- }, null), [[_vShow, !hideBottomFields.value]]), (_a = slots.default) == null ? void 0 : _a.call(slots)]), renderSetDefaultCell(), _withDirectives(_createVNode("div", {
- "class": bem("buttons")
- }, [_createVNode(Button, {
- "block": true,
- "round": true,
- "type": "danger",
- "text": props.saveButtonText || t("save"),
- "class": bem("button"),
- "loading": props.isSaving,
- "nativeType": "submit"
- }, null), props.showDelete && _createVNode(Button, {
- "block": true,
- "round": true,
- "class": bem("button"),
- "loading": props.isDeleting,
- "text": props.deleteButtonText || t("delete"),
- "onClick": onDelete
- }, null)]), [[_vShow, !hideBottomFields.value]]), _createVNode(Popup, {
- "show": showAreaPopup.value,
- "onUpdate:show": ($event) => showAreaPopup.value = $event,
- "round": true,
- "teleport": "body",
- "position": "bottom",
- "lazyRender": false
- }, {
- default: () => [_createVNode(Area, {
- "ref": areaRef,
- "value": data.areaCode,
- "loading": !areaListLoaded.value,
- "areaList": props.areaList,
- "columnsPlaceholder": props.areaColumnsPlaceholder,
- "onConfirm": onAreaConfirm,
- "onCancel": () => {
- showAreaPopup.value = false;
- }
- }, null)]
- })];
- }
- });
- };
- }
- });
- export {
- stdin_default as default
- };
|