From 0a674aef6cf1d0da2b3a51adf29344ccd306fec5 Mon Sep 17 00:00:00 2001 From: Artur Hutsul Date: Wed, 13 Sep 2023 15:43:33 +0300 Subject: [PATCH] make possible to specify search values outside of the lib --- .../src/components/Picker/Picker.tsx | 34 +++++++++++++++++++ .../src/components/Picker/PickerProps.ts | 3 ++ 2 files changed, 37 insertions(+) diff --git a/packages/emoji-mart/src/components/Picker/Picker.tsx b/packages/emoji-mart/src/components/Picker/Picker.tsx index cced465b..474fdbf2 100644 --- a/packages/emoji-mart/src/components/Picker/Picker.tsx +++ b/packages/emoji-mart/src/components/Picker/Picker.tsx @@ -48,6 +48,7 @@ export default class Picker extends Component { } this.initGrid() + this.handleSearchValue() if ( this.props.stickySearch == false && @@ -61,6 +62,39 @@ export default class Picker extends Component { } } + // Rewritten + async handleSearchValue() { + const value = this.props.searchValue + const searchResults = await SearchIndex.search(value) + const afterRender = () => { + if (!this.refs.scroll.current) return + this.refs.scroll.current.scrollTop = 0 + } + + if (!searchResults) { + return this.setState({ searchResults, pos: [-1, -1] }, afterRender) + } + + const pos = [0, 0] + const grid = [] + grid.setsize = searchResults.length + let row = null + + for (let emoji of searchResults) { + if (!grid.length || row.length == this.getPerLine()) { + row = [] + row.__categoryId = 'search' + row.__index = grid.length + grid.push(row) + } + + row.push(emoji) + } + + this.ignoreMouse() + this.setState({ searchResults: grid, pos }, afterRender) + } + componentDidMount() { this.register() diff --git a/packages/emoji-mart/src/components/Picker/PickerProps.ts b/packages/emoji-mart/src/components/Picker/PickerProps.ts index 5f1a9af7..ca9a82a7 100644 --- a/packages/emoji-mart/src/components/Picker/PickerProps.ts +++ b/packages/emoji-mart/src/components/Picker/PickerProps.ts @@ -118,4 +118,7 @@ export default { deprecated: true, value: true, }, + + // Rewritten + searchValue: null, }