Skip to content

Commit a82ea10

Browse files
Implement suggested feature
1 parent 50e9c4e commit a82ea10

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

Sources/LUAutocompleteView.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ open class LUAutocompleteView: UIView {
2626
public var maximumHeight: CGFloat = 200.0
2727
/// A boolean value that determines whether the view should hide after a suggestion is selected. Default value is `true`.
2828
public var shouldHideAfterSelecting = true
29+
/// A boolean value that determines whether the view should show even when no text has been digited. When text is empty, you can return a list of elements in order to show suggestions if this value is set to `true`. Default value is `false`.
30+
public var showElementsWithEmptyText = false
31+
2932
/** The attributes for the text suggestions.
3033

3134
- Note: This property will be ignored if `autocompleteCell` is not `nil`.
@@ -38,6 +41,7 @@ open class LUAutocompleteView: UIView {
3841

3942
textField.addTarget(self, action: #selector(textFieldEditingChanged), for: .editingChanged)
4043
textField.addTarget(self, action: #selector(textFieldEditingEnded), for: .editingDidEnd)
44+
textField.addTarget(self, action: #selector(textFieldEditingBegan), for: .editingDidBegin)
4145

4246
setupConstraints()
4347
}
@@ -172,7 +176,7 @@ open class LUAutocompleteView: UIView {
172176
@objc private func getElements() {
173177
guard let dataSource else { return }
174178

175-
guard let text = textField?.text, !text.isEmpty else {
179+
guard let text = textField?.text, !text.isEmpty || showElementsWithEmptyText else {
176180
elements.removeAll()
177181
return
178182
}
@@ -184,6 +188,14 @@ open class LUAutocompleteView: UIView {
184188

185189
@objc private func textFieldEditingEnded() {
186190
height = 0
191+
delegate?.autocompleteViewEditingEnded(self)
192+
}
193+
194+
@objc private func textFieldEditingBegan() {
195+
guard showElementsWithEmptyText else { return }
196+
197+
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(getElements), object: nil)
198+
perform(#selector(getElements), with: nil, afterDelay: throttleTime)
187199
}
188200
}
189201

@@ -199,7 +211,7 @@ extension LUAutocompleteView: UITableViewDataSource {
199211
- Returns: The number of rows in `section`.
200212
*/
201213
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
202-
!(textField?.text?.isEmpty ?? true) ? elements.count : 0
214+
elements.count
203215
}
204216

205217
/** Asks the data source for a cell to insert in a particular location of the table view.

Sources/LUAutocompleteViewDelegate.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,17 @@ public protocol LUAutocompleteViewDelegate: AnyObject {
1717
- text: A string that was selected in autocomplete view.
1818
*/
1919
func autocompleteView(_ autocompleteView: LUAutocompleteView, didSelect text: String)
20+
21+
22+
/** Tells the delegate the editing of text view ended
23+
24+
- Parameters:
25+
- autocompleteView: An autocomplete view object informing the delegate when editing ended.
26+
*/
27+
func autocompleteViewEditingEnded(_ autocompleteView: LUAutocompleteView)
28+
}
29+
30+
public extension LUAutocompleteViewDelegate {
31+
func autocompleteViewEditingEnded(_ autocompleteView: LUAutocompleteView) {
32+
}
2033
}

0 commit comments

Comments
 (0)