Skip to content

Commit

Permalink
Added Selection prop to TextInputProps
Browse files Browse the repository at this point in the history
Summary:
Changelog: [iOS][Added]
1. Added new primitive type "Selection" to C++
2. Added property "selection" to TextInputProps
3. Added parser for that

Reviewed By: sammy-SC

Differential Revision: D30043256

fbshipit-source-id: eefa67ca23759761901cba1d2ab3052877a153a7
  • Loading branch information
Dmitry Rykun authored and facebook-github-bot committed Aug 23, 2021
1 parent 1d0fb08 commit 8434177
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
if (newTextInputProps.inputAccessoryViewID != oldTextInputProps.inputAccessoryViewID) {
_backedTextInputView.inputAccessoryViewID = RCTNSStringFromString(newTextInputProps.inputAccessoryViewID);
}

[super updateProps:props oldProps:oldProps];

[self setDefaultInputAccessoryView];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ TextInputProps::TextInputProps(
"autoFocus",
sourceProps.autoFocus,
{})),
selection(convertRawProp(
context,
rawProps,
"selection",
sourceProps.selection,
better::optional<Selection>())),
inputAccessoryViewID(convertRawProp(
context,
rawProps,
Expand Down Expand Up @@ -115,13 +121,5 @@ ParagraphAttributes TextInputProps::getEffectiveParagraphAttributes() const {
return result;
}

#ifdef ANDROID
folly::dynamic TextInputProps::getDynamic() const {
folly::dynamic props = folly::dynamic::object();
props["value"] = value;
return props;
}
#endif

} // namespace react
} // namespace facebook
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class TextInputProps final : public ViewProps, public BaseTextProps {
int const mostRecentEventCount{0};

bool autoFocus{false};
better::optional<Selection> selection{};

std::string const inputAccessoryViewID{};

Expand All @@ -66,10 +67,6 @@ class TextInputProps final : public ViewProps, public BaseTextProps {
*/
TextAttributes getEffectiveTextAttributes(Float fontSizeMultiplier) const;
ParagraphAttributes getEffectiveParagraphAttributes() const;

#ifdef ANDROID
folly::dynamic getDynamic() const;
#endif
};

} // namespace react
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ enum class KeyboardType {
VisiblePassword,
};

class Selection final {
public:
int start{0};
int end{0};
};

/*
* Controls features of text inputs.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,38 @@ static TextInputTraits convertRawProp(
return traits;
}

inline void fromRawValue(
const PropsParserContext &context,
const RawValue &value,
Selection &result) {
if (value.hasType<better::map<std::string, int>>()) {
auto map = (better::map<std::string, int>)value;
for (const auto &pair : map) {
if (pair.first == "start") {
result.start = pair.second;
} else if (pair.first == "end") {
result.end = pair.second;
} else {
LOG(ERROR) << "Unsupported Selection map key: " << pair.first;
react_native_assert(false);
}
}
return;
}

react_native_assert(value.hasType<std::vector<int>>());
if (value.hasType<std::vector<int>>()) {
auto array = (std::vector<int>)value;
react_native_assert(array.size() == 2);
if (array.size() >= 2) {
result = {array.at(0), array.at(1)};
} else {
result = {0, 0};
LOG(ERROR) << "Unsupported Selection vector size: " << array.size();
}
} else {
LOG(ERROR) << "Unsupported Selection type";
}
}
} // namespace react
} // namespace facebook

0 comments on commit 8434177

Please sign in to comment.