1 /*
2 * Copyright (c) 2021-2022 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/picker/picker_text_component.h"
17
18 namespace OHOS::Ace {
19
PickerTextComponent()20 PickerTextComponent::PickerTextComponent()
21 {
22 auto column = AceType::MakeRefPtr<PickerColumnComponent>();
23 column->SetColumnTag(PICKER_TEXT_COLUMN);
24 AppendColumn(column);
25 }
26
OnTitleBuilding()27 void PickerTextComponent::OnTitleBuilding()
28 {
29 PickerBaseComponent::OnTitleBuilding();
30 SetHasTitle(false);
31 }
32
OnColumnsBuilding()33 void PickerTextComponent::OnColumnsBuilding()
34 {
35 auto column = GetColumn(PICKER_TEXT_COLUMN);
36 if (!column) {
37 LOGE("can not get column of text");
38 return;
39 }
40
41 column->ClearOption();
42 for (const auto& item : range_) {
43 column->AppendOption(item);
44 }
45 selectedIndex_ = range_.empty() ? 0 : selectedIndex_ % range_.size();
46 column->SetCurrentIndex(selectedIndex_);
47 }
48
GetSelectedObject(bool isColumnChange,const std::string & changeColumnTag,int32_t status) const49 std::string PickerTextComponent::GetSelectedObject(bool isColumnChange,
50 const std::string& changeColumnTag, int32_t status) const
51 {
52 auto value = selectedValue_;
53 auto index = selectedIndex_;
54 if (isColumnChange) {
55 auto column = GetColumn(PICKER_TEXT_COLUMN);
56 if (column) {
57 value = column->GetCurrentText();
58 index = column->GetCurrentIndex();
59 }
60 }
61 auto container = Container::Current();
62 if (!container) {
63 return "";
64 }
65 auto context = container->GetPipelineContext();
66 if (!context) {
67 return "";
68 }
69 if (context->GetIsDeclarative()) {
70 return std::string("{\"value\":") + "\"" + value + "\"" +
71 ",\"index\":" + std::to_string(index) +
72 ",\"status\":" + std::to_string(status) + "}";
73 } else {
74 return std::string("{\"newValue\":") + "\"" + value + "\"" +
75 ",\"newSelected\":" + std::to_string(index) +
76 ",\"status\":" + std::to_string(status) + "}";
77 }
78 }
79
OnSelectedSaving()80 void PickerTextComponent::OnSelectedSaving()
81 {
82 auto column = GetColumn(PICKER_TEXT_COLUMN);
83 if (!column) {
84 LOGE("can not get column of text");
85 return;
86 }
87 selectedIndex_ = column->GetCurrentIndex();
88 selectedValue_ = column->GetCurrentText();
89 }
90
HandleSelectedChange()91 void PickerTextComponent::HandleSelectedChange()
92 {
93 if (!IsDialogShowed()) {
94 return;
95 }
96 auto column = GetColumn(PICKER_TEXT_COLUMN);
97 if (!column) {
98 LOGE("can not get column of text");
99 return;
100 }
101
102 OnColumnsBuilding();
103 column->HandleChangeCallback(true, false);
104 }
105
HandleRangeChange()106 void PickerTextComponent::HandleRangeChange()
107 {
108 if (!IsDialogShowed()) {
109 return;
110 }
111
112 auto column = GetColumn(PICKER_TEXT_COLUMN);
113 if (!column) {
114 LOGE("can not get column of text");
115 return;
116 }
117 auto backupIndex = selectedIndex_;
118 auto backupValue = selectedValue_;
119 OnSelectedSaving();
120 OnColumnsBuilding();
121 column->HandleChangeCallback(true, false);
122 selectedIndex_ = backupIndex;
123 selectedValue_ = backupValue;
124 }
125
126 } // namespace OHOS::Ace
127