@lowdefy/blocks-antd 4.0.0-rc.4 → 4.0.0-rc.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.
@@ -14,7 +14,7 @@
14
14
  "theme": {
15
15
  "type": "string",
16
16
  "enum": ["light", "dark"],
17
- "default": "light",
17
+ "default": "dark",
18
18
  "description": "Page theme."
19
19
  }
20
20
  }
@@ -0,0 +1,201 @@
1
+ /*
2
+ Copyright 2020-2023 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import React from 'react';
16
+ import { Input, Select } from 'antd';
17
+ import { blockDefaultProps } from '@lowdefy/block-utils';
18
+ import regions from './regions.js';
19
+ import Label from '../Label/Label.js';
20
+ import getValueIndex from '../../getValueIndex.js';
21
+ import getUniqueValues from '../../getUniqueValues.js';
22
+ const Option = Select.Option;
23
+ function getAllowedRegions({ allowedRegions , regions }) {
24
+ if (!allowedRegions || allowedRegions.length === 0) {
25
+ return regions;
26
+ }
27
+ return regions.filter((region)=>allowedRegions.includes(region.code));
28
+ }
29
+ function getDefaultRegion({ allowedRegions , defaultRegion , uniqueValueOptions }) {
30
+ if (!defaultRegion) {
31
+ return getValueIndex(allowedRegions[0], uniqueValueOptions);
32
+ }
33
+ const index = allowedRegions.findIndex((region)=>region.code === defaultRegion);
34
+ if (index === -1) {
35
+ return getValueIndex(allowedRegions[0], uniqueValueOptions);
36
+ }
37
+ return getValueIndex(allowedRegions[index], uniqueValueOptions);
38
+ }
39
+ function AddOnSelect({ blockId , defaultValue , loading , methods , properties , uniqueValueOptions , value }) {
40
+ return /*#__PURE__*/ React.createElement(Select, {
41
+ id: `${blockId}_select_input`,
42
+ bordered: properties.bordered,
43
+ className: methods.makeCssClass([
44
+ {
45
+ minWidth: 100
46
+ },
47
+ methods.makeCssClass(properties.selectStyle)
48
+ ]),
49
+ defaultValue: defaultValue,
50
+ disabled: properties.disabled || loading,
51
+ dropdownMatchSelectWidth: false,
52
+ filterOption: (input, option)=>option.filterstring.toLowerCase().indexOf(input.toLowerCase()) >= 0,
53
+ mode: "single",
54
+ notFoundContent: 'Not found',
55
+ onChange: (newVal)=>{
56
+ const input = value?.input;
57
+ const region = uniqueValueOptions[newVal].value;
58
+ const phone_number = `${region.dial_code}${input}`;
59
+ methods.setValue({
60
+ input,
61
+ region,
62
+ phone_number
63
+ });
64
+ methods.triggerEvent({
65
+ name: 'onCodeChange'
66
+ });
67
+ methods.triggerEvent({
68
+ name: 'onChange'
69
+ });
70
+ },
71
+ optionFilterProp: "filterString",
72
+ optionLabelProp: "label",
73
+ placeholder: 'Select item',
74
+ showArrow: properties.showArrow,
75
+ showSearch: true,
76
+ size: properties.size,
77
+ value: getValueIndex(value?.region, uniqueValueOptions)
78
+ }, uniqueValueOptions.map((opt, i)=>{
79
+ const displayLabel = properties.showFlags === false ? `${opt.value.name} ${opt.value.dial_code}` : `${opt.value.flag} ${opt.value.name} ${opt.value.dial_code}`;
80
+ return /*#__PURE__*/ React.createElement(Option, {
81
+ className: methods.makeCssClass([
82
+ properties.optionsStyle
83
+ ]),
84
+ filterString: displayLabel,
85
+ id: `${blockId}_${i}`,
86
+ key: `${i}`,
87
+ value: `${i}`,
88
+ label: opt.label
89
+ }, displayLabel);
90
+ }));
91
+ }
92
+ const PhoneNumberInput = ({ blockId , components: { Icon , Link } , events , loading , methods , properties , required , validation , value })=>{
93
+ const allowedRegions = getAllowedRegions({
94
+ allowedRegions: properties.allowedRegions,
95
+ regions
96
+ });
97
+ const uniqueValueOptions = getUniqueValues(allowedRegions.map((region)=>({
98
+ value: {
99
+ ...region
100
+ },
101
+ label: properties.showFlags === false ? `${region.dial_code}` : `${region.flag} ${region.dial_code}`
102
+ })));
103
+ const defaultValue = getDefaultRegion({
104
+ allowedRegions,
105
+ defaultRegion: properties.defaultRegion,
106
+ methods,
107
+ uniqueValueOptions
108
+ });
109
+ if (value === null) {
110
+ methods.setValue({
111
+ input: '',
112
+ region: allowedRegions[defaultValue],
113
+ phone_number: allowedRegions[defaultValue].dial_code
114
+ });
115
+ }
116
+ return /*#__PURE__*/ React.createElement(Label, {
117
+ blockId: blockId,
118
+ components: {
119
+ Icon,
120
+ Link
121
+ },
122
+ events: events,
123
+ properties: {
124
+ title: properties.title,
125
+ size: properties.size,
126
+ ...properties.label
127
+ },
128
+ required: required,
129
+ validation: validation,
130
+ content: {
131
+ content: ()=>{
132
+ return /*#__PURE__*/ React.createElement(Input, {
133
+ id: `${blockId}_input`,
134
+ addonBefore: /*#__PURE__*/ React.createElement(AddOnSelect, {
135
+ blockId: blockId,
136
+ defaultValue: defaultValue,
137
+ loading: loading,
138
+ methods: methods,
139
+ properties: properties,
140
+ uniqueValueOptions: uniqueValueOptions,
141
+ value: value
142
+ }),
143
+ allowClear: properties.allowClear,
144
+ autoFocus: properties.autoFocus,
145
+ bordered: properties.bordered,
146
+ className: `ldf-phone-number-input ${methods.makeCssClass(properties.inputStyle)}`,
147
+ disabled: properties.disabled || loading,
148
+ maxLength: properties.maxLength,
149
+ placeholder: properties.placeholder,
150
+ size: properties.size,
151
+ status: validation.status,
152
+ type: 'number',
153
+ value: value?.input,
154
+ onChange: (event)=>{
155
+ const input = event.target.value;
156
+ const region = value.region;
157
+ const phone_number = `${region.dial_code}${input}`;
158
+ methods.setValue({
159
+ input,
160
+ region,
161
+ phone_number
162
+ });
163
+ methods.triggerEvent({
164
+ name: 'onInputChange'
165
+ });
166
+ methods.triggerEvent({
167
+ name: 'onChange'
168
+ });
169
+ },
170
+ onPressEnter: ()=>{
171
+ methods.triggerEvent({
172
+ name: 'onPressEnter'
173
+ });
174
+ },
175
+ prefix: properties.prefix || properties.prefixIcon && /*#__PURE__*/ React.createElement(Icon, {
176
+ blockId: `${blockId}_prefixIcon`,
177
+ events: events,
178
+ properties: properties.prefixIcon
179
+ }),
180
+ suffix: (properties.suffix || properties.suffixIcon) && /*#__PURE__*/ React.createElement(React.Fragment, null, properties.suffix && properties.suffix, properties.suffixIcon && /*#__PURE__*/ React.createElement(Icon, {
181
+ blockId: `${blockId}_suffixIcon`,
182
+ events: events,
183
+ properties: properties.suffixIcon
184
+ }))
185
+ });
186
+ }
187
+ }
188
+ });
189
+ };
190
+ PhoneNumberInput.defaultProps = blockDefaultProps;
191
+ PhoneNumberInput.meta = {
192
+ valueType: 'object',
193
+ category: 'input',
194
+ icons: [
195
+ ...Label.meta.icons
196
+ ],
197
+ styles: [
198
+ 'blocks/PhoneNumberInput/style.less'
199
+ ]
200
+ };
201
+ export default PhoneNumberInput;