@antscorp/antsomi-ui 1.3.5-beta.997 → 1.3.5-beta.998

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.
@@ -1,4 +1,3 @@
1
- /// <reference types="react" />
2
1
  import type { PayloadInfo } from '@antscorp/antsomi-ui/es/types';
3
2
  export type TDisplayFormat = 'number' | 'percentage' | 'currency' | 'datetime';
4
3
  type DisplayFormatProps = {
@@ -1,9 +1,9 @@
1
1
  /// <reference types="react" />
2
2
  export type ValueType = 'input' | 'select';
3
- type Value = string | {
3
+ type Value = {
4
4
  source: string;
5
- code: string;
6
- } | null;
5
+ code: any;
6
+ } | string;
7
7
  type Option = {
8
8
  [key: string]: any;
9
9
  label: string;
@@ -11,16 +11,15 @@ type Option = {
11
11
  };
12
12
  export interface InputSelectAttributeProps {
13
13
  sourceOptions: Option[];
14
- codeOptions: Option[];
15
- codeOptionsInitial?: Option[];
14
+ mapCodeOptions: Record<string, Option[]>;
16
15
  label?: string;
16
+ isErrorTag?: boolean;
17
17
  errorMsg?: string;
18
18
  value: Value;
19
19
  onChange: (newValueData: {
20
20
  value: Value;
21
21
  valueType: ValueType;
22
22
  }) => void;
23
- onChangeSelectSource: (value: string) => void;
24
23
  }
25
24
  declare const _default: import("react").MemoExoticComponent<(props: InputSelectAttributeProps) => import("react/jsx-runtime").JSX.Element>;
26
25
  export default _default;
@@ -1,7 +1,7 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
2
  // Libraries
3
- import { memo, useCallback, useEffect, useMemo, useState } from 'react';
4
- import { get, isUndefined, keyBy } from 'lodash';
3
+ import { memo, useCallback, useMemo, useState } from 'react';
4
+ import { get, keyBy } from 'lodash';
5
5
  import { Flex, Form, Select, Tooltip, Typography } from 'antd';
6
6
  // Translations
7
7
  import { translate, translations } from '@antscorp/antsomi-ui/es/locales';
@@ -15,62 +15,75 @@ import { Dashboard30Icon, ErrorIcon } from '../../icons';
15
15
  // Constants
16
16
  import { THEME } from '@antscorp/antsomi-ui/es/constants';
17
17
  const InputSelectAttribute = (props) => {
18
- const { value, errorMsg, label, sourceOptions = [], codeOptions = [], codeOptionsInitial = [], onChange, onChangeSelectSource, } = props;
18
+ const { value, errorMsg, label, isErrorTag, sourceOptions = [], mapCodeOptions = {}, onChange, } = props;
19
19
  const [form] = Form.useForm();
20
+ const sourceValue = Form.useWatch('source', form);
20
21
  // States
21
22
  const [openModal, setOpenModal] = useState(false);
22
- const [attributeState, setAttributeState] = useState(() => {
23
- if (value && typeof value !== 'string') {
24
- return value;
23
+ const codeOptions = useMemo(() => {
24
+ if (sourceValue) {
25
+ return get(mapCodeOptions, sourceValue, []);
25
26
  }
26
- return {
27
- source: '',
28
- code: '',
29
- };
30
- });
31
- // Effects
32
- useEffect(() => {
33
- if (attributeState.code === '' ||
34
- !codeOptions.find(option => option.value === attributeState.code)) {
35
- form.setFieldsValue({ code: codeOptions[0]?.value });
36
- }
37
- }, [attributeState, codeOptions, form]);
38
- // Memoized
39
- const mapCodeOpts = useMemo(() => keyBy(codeOptions, 'value'), [codeOptions]);
40
- const mapCodeOriginOpts = useMemo(() => keyBy(codeOptionsInitial, 'value'), [codeOptionsInitial]);
27
+ return [];
28
+ }, [sourceValue, mapCodeOptions]);
29
+ const mapCode = useMemo(() => {
30
+ if (typeof value === 'string')
31
+ return {};
32
+ return keyBy(get(mapCodeOptions, value?.code, []), 'value');
33
+ }, [mapCodeOptions, value]);
34
+ const getCodeBySource = useCallback((source) => get(mapCodeOptions, [source, '0', 'value'], ''), [mapCodeOptions]);
41
35
  const onOpenModal = useCallback(() => {
36
+ if (typeof value !== 'string') {
37
+ form.setFieldsValue({ source: value?.source, code: value?.code });
38
+ }
39
+ else {
40
+ const source = sourceOptions[0]?.value || '';
41
+ const code = getCodeBySource(source);
42
+ form.setFieldsValue({ source, code });
43
+ }
42
44
  setOpenModal(true);
43
- }, []);
45
+ }, [value, form, sourceOptions, getCodeBySource]);
44
46
  const onDeselect = useCallback(() => {
45
47
  onChange({ value: '', valueType: 'input' });
46
48
  }, [onChange]);
47
49
  const onHideModal = useCallback(() => {
48
50
  setOpenModal(false);
49
51
  }, []);
52
+ const onAfterClose = useCallback(() => {
53
+ form.resetFields();
54
+ }, [form]);
50
55
  const onOk = async () => {
51
56
  try {
52
57
  // Validate form fields before getting values
53
58
  const values = await form.validateFields();
54
- const newValue = {
55
- source: values.sources,
56
- code: values.code,
57
- };
59
+ if (typeof values !== 'string') {
60
+ const newValue = {
61
+ source: values?.source,
62
+ code: values?.code,
63
+ };
64
+ onChange({ value: newValue, valueType: 'select' });
65
+ }
58
66
  setOpenModal(false);
59
- onChange({ value: newValue, valueType: 'select' });
60
67
  }
61
68
  catch (errorInfo) {
69
+ // eslint-disable-next-line no-console
62
70
  console.error('Validation Failed:', errorInfo);
63
71
  }
64
72
  };
65
73
  const onChangeInput = useCallback((val) => {
66
74
  onChange({ value: val, valueType: 'input' });
67
75
  }, [onChange]);
76
+ const onValuesChange = useCallback((changedValues) => {
77
+ // If source changed -> set new code based on new source
78
+ if (changedValues?.source) {
79
+ const newCode = getCodeBySource(changedValues.source);
80
+ form.setFieldValue('code', newCode);
81
+ }
82
+ }, [form, getCodeBySource]);
68
83
  const renderInput = () => {
69
84
  let element = null;
70
85
  const isObjValue = value && typeof value !== 'string';
71
86
  if (openModal || isObjValue) {
72
- const isErrorTag = (isObjValue &&
73
- isUndefined(get(mapCodeOriginOpts, [value.code])));
74
87
  element = (_jsxs("div", { style: {
75
88
  display: 'flex',
76
89
  alignItems: 'center',
@@ -78,7 +91,7 @@ const InputSelectAttribute = (props) => {
78
91
  height: 32,
79
92
  padding: '4px 12px 4px 4px',
80
93
  borderBottom: `1px solid ${errorMsg ? THEME.token?.colorError : THEME.token?.blue1}`,
81
- }, children: [_jsx("div", { style: { width: '100%', cursor: 'pointer' }, onClick: onOpenModal, children: isObjValue && (_jsx(Tag, { isError: isErrorTag, children: isErrorTag ? (_jsxs(Flex, { gap: 5, align: "center", children: ["Unknown", _jsx(Tooltip, { title: "The used dynamic content is removed", children: _jsx(ErrorIcon, { size: 16 }) })] })) : (get(mapCodeOpts, [value.code, 'label'], mapCodeOriginOpts[value.code]?.label)) })) }), _jsx(Icon, { type: "icon-ants-remove", style: { fontSize: 10, color: '#222', cursor: 'pointer' }, onClick: onDeselect })] }));
94
+ }, children: [_jsx("div", { style: { width: '100%', cursor: 'pointer' }, onClick: onOpenModal, children: isObjValue && (_jsx(Tag, { isError: isErrorTag, children: isErrorTag ? (_jsxs(Flex, { gap: 5, align: "center", children: ["Unknown", _jsx(Tooltip, { title: "The used dynamic content is removed", children: _jsx(ErrorIcon, { size: 16 }) })] })) : (get(mapCode, [value?.code, 'label'], value?.code)) })) }), _jsx(Icon, { type: "icon-ants-remove", style: { fontSize: 10, color: '#222', cursor: 'pointer' }, onClick: onDeselect })] }));
82
95
  }
83
96
  else {
84
97
  element = (_jsx(StyledSelect, { mode: "multiple", options: [{ value: '', label: 'Or select a field' }], notFoundContent: null, onSelect: onOpenModal, style: { width: '100%', borderTop: 'none', borderLeft: 'none', borderRight: 'none' }, onDeselect: onDeselect, autoClearSearchValue: false, searchValue: typeof value === 'string' ? value : '', onSearch: onChangeInput, status: errorMsg ? 'error' : undefined, placeholder: typeof value === 'string' ? value : translate(translations.inputYourValue.title), "$isPlaceholder": !value, "$isError": !!errorMsg, dropdownStyle: {
@@ -87,12 +100,6 @@ const InputSelectAttribute = (props) => {
87
100
  }
88
101
  return element;
89
102
  };
90
- return (_jsxs(_Fragment, { children: [renderInput(), errorMsg ? (_jsx(Typography.Text, { style: { marginLeft: 8, color: THEME.token?.red8, marginTop: 5 }, children: errorMsg })) : null, _jsx(ModalV2, { title: translate(translations._PREDICT_MODEL_SELECT_ATTRIBUTE, 'Select attribute'), okText: translate(translations._ACT_APPLY, 'Apply'), open: openModal, onOk: onOk, onCancel: onHideModal, destroyOnClose: true, centered: true, children: _jsxs(Form, { colon: false, form: form, initialValues: {
91
- sources: attributeState.source || sourceOptions[0]?.value,
92
- code: attributeState.code || codeOptions[0]?.value,
93
- }, children: [_jsx(Form.Item, { label: translate(translations._TITL_PERSONALIZATION_TYPE, 'Content Source'), name: "sources", required: true, labelCol: { span: 6 }, labelAlign: "left", rules: [{ required: true, message: 'Please select field!' }], children: _jsx(Select, { suffixIcon: _jsx(Icon, { type: "icon-ants-expand-more", style: { fontSize: '20px', color: THEME.token?.colorIcon } }), options: sourceOptions, placeholder: "Please select an item", value: attributeState.source, onChange: newValue => {
94
- setAttributeState(state => ({ ...state, code: '', source: newValue }));
95
- onChangeSelectSource(newValue);
96
- } }) }), _jsx(Form.Item, { label: label || 'Allocated Code', required: true, labelCol: { span: 6 }, labelAlign: "left", name: "code", rules: [{ required: true, message: 'Please select field!' }], children: _jsx(Select, { suffixIcon: _jsx(Icon, { type: "icon-ants-expand-more", style: { fontSize: '20px', color: THEME.token?.colorIcon } }), notFoundContent: _jsx(EmptyData, { size: "small", icon: _jsx(Dashboard30Icon, {}), description: "No personalized content in this journey" }), placeholder: "Please select an item", value: attributeState.code, options: codeOptions }) })] }) })] }));
103
+ return (_jsxs(_Fragment, { children: [renderInput(), errorMsg ? (_jsx(Typography.Text, { style: { marginLeft: 8, color: THEME.token?.red8, marginTop: 5 }, children: errorMsg })) : null, _jsx(ModalV2, { title: translate(translations._PREDICT_MODEL_SELECT_ATTRIBUTE, 'Select attribute'), okText: translate(translations._ACT_APPLY, 'Apply'), open: openModal, onOk: onOk, onCancel: onHideModal, afterClose: onAfterClose, destroyOnClose: true, centered: true, children: _jsxs(Form, { colon: false, form: form, onValuesChange: onValuesChange, children: [_jsx(Form.Item, { label: translate(translations._TITL_PERSONALIZATION_TYPE, 'Content Source'), name: "source", required: true, labelCol: { span: 6 }, labelAlign: "left", rules: [{ required: true, message: 'Please select field!' }], children: _jsx(Select, { suffixIcon: _jsx(Icon, { type: "icon-ants-expand-more", style: { fontSize: '20px', color: THEME.token?.colorIcon } }), options: sourceOptions, placeholder: "Please select an item" }) }), _jsx(Form.Item, { label: label || 'Allocated Code', required: true, labelCol: { span: 6 }, labelAlign: "left", name: "code", rules: [{ required: true, message: 'Please select field!' }], children: _jsx(Select, { suffixIcon: _jsx(Icon, { type: "icon-ants-expand-more", style: { fontSize: '20px', color: THEME.token?.colorIcon } }), notFoundContent: _jsx(EmptyData, { size: "small", icon: _jsx(Dashboard30Icon, {}), description: "No personalized content in this journey" }), placeholder: "Please select an item", options: codeOptions }) })] }) })] }));
97
104
  };
98
105
  export default memo(InputSelectAttribute);
@@ -3,5 +3,4 @@
3
3
  * Asynchronously loads the component for TemplateListing
4
4
  *
5
5
  */
6
- /// <reference types="react" />
7
6
  export declare const TemplateListing: (props: import("./types").TemplateListingProps<{}>) => JSX.Element;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antscorp/antsomi-ui",
3
- "version": "1.3.5-beta.997",
3
+ "version": "1.3.5-beta.998",
4
4
  "description": "An enterprise-class UI design language and React UI library.",
5
5
  "sideEffects": [
6
6
  "dist/*",