1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "core/components/test/json/text_field_creator.h"
17
18 #include "base/json/json_util.h"
19 #include "core/components/test/json/color_creator.h"
20 #include "core/components/test/json/decoration_creator.h"
21 #include "core/components/test/json/text_style_creator.h"
22 #include "core/components/text_field/text_field_component.h"
23 #include "core/pipeline/base/constants.h"
24
25 namespace OHOS::Ace {
26
CreateFromJson(const JsonValue & json,const JsonComponentFactory &)27 RefPtr<Component> TextFieldCreator::CreateFromJson(const JsonValue& json, const JsonComponentFactory&)
28 {
29 std::string classType = json.GetValue(CLASS_NAME)->GetString();
30 if (classType != TEXT_FIELD_NAME) {
31 LOGE("Create TextField err: not a textField json.");
32 return nullptr;
33 }
34
35 auto textField = AceType::MakeRefPtr<TextFieldComponent>();
36
37 int32_t align = json.GetInt(TEXT_ALIGN, static_cast<int32_t>(TextAlign::START));
38 textField->SetTextAlign(static_cast<TextAlign>(align));
39
40 int32_t direction = json.GetInt(TEXT_OVERFLOW, static_cast<int32_t>(TextDirection::INHERIT));
41 textField->SetTextDirection(static_cast<TextDirection>(direction));
42
43 textField->SetObscure(json.GetBool(OBSCURE_TEXT, false));
44 textField->SetEnabled(json.GetBool(TEXT_FIELD_ENABLED, true));
45 textField->SetAutoFocus(json.GetBool(TEXT_FIELD_AUTO_FOCUS, false));
46 textField->SetTextMaxLines(json.GetUInt(TEXT_FIELD_MAX_LINES, 1));
47 const uint32_t DEFAULT_LENGTH_LIMIT = std::numeric_limits<uint32_t>::max();
48 uint32_t maxLength = json.GetUInt(TEXT_FIELD_MAX_LENGTH, DEFAULT_LENGTH_LIMIT);
49 if (maxLength != DEFAULT_LENGTH_LIMIT) {
50 textField->SetMaxLength(maxLength);
51 }
52 textField->SetTextInputType(
53 CastToTextInputType(json.GetInt(TEXT_FIELD_TYPE, static_cast<int32_t>(TextInputType::TEXT))));
54 textField->SetAction(
55 CastToTextInputAction(json.GetInt(TEXT_FIELD_ACTION, static_cast<int32_t>(TextInputAction::UNSPECIFIED))));
56 textField->SetActionLabel(json.GetString(TEXT_FIELD_ACTION_LABEL));
57
58 auto temp = json.GetValue(TEXT_STYLE);
59 if (temp && temp->IsObject()) {
60 textField->SetTextStyle(TextStyleCreator::CreateFromJson(*temp));
61 }
62
63 temp = json.GetValue(DECORATION_NAME);
64 if (temp && temp->IsObject()) {
65 textField->SetDecoration(DecorationCreator::CreateFromJson(*temp));
66 }
67
68 temp = json.GetValue(CURSOR_COLOR);
69 if (temp && temp->IsObject()) {
70 textField->SetCursorColor(ColorCreator::CreateFromJson(*temp));
71 }
72
73 textField->SetPlaceholder(json.GetString(TEXT_FIELD_PLACEHOLDER));
74 temp = json.GetValue(TEXT_FIELD_PLACEHOLDER_COLOR);
75 if (temp && temp->IsObject()) {
76 textField->SetPlaceholderColor(ColorCreator::CreateFromJson(*temp));
77 }
78
79 textField->SetExtend(json.GetBool(TEXT_FIELD_EXTEND, false));
80 textField->SetIconImage(json.GetString(TEXT_FIELD_ICON_IMAGE));
81
82 textField->SetOnTextChange(EventMarker { json.GetString(TEXT_FIELD_ON_TEXT_CHANGE) });
83 textField->SetOnFinishInput(EventMarker { json.GetString(TEXT_FIELD_ON_FINISH_INPUT) });
84 textField->SetOnTap(EventMarker { json.GetString(TEXT_FIELD_ON_TAP) });
85 return textField;
86 }
87
88 } // namespace OHOS::Ace
89