Skip to content

Commit

Permalink
refactor(examples): simplify code (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahdayan authored Sep 22, 2021
1 parent 8c1eede commit d6d6ba9
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/twitter-compose-with-typeahead/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import './App.css';
export function App() {
return (
<div className="container">
<Autocomplete placeholder="What's up?" defaultActiveItemId={0} />
<Autocomplete placeholder="What's up?" />
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function Autocomplete(
const { autocomplete, state } = useAutocomplete({
...props,
id: 'twitter-autocomplete',
defaultActiveItemId: 0,
getSources({ query }) {
const cursorPosition = inputRef.current?.selectionEnd || 0;
const activeToken = getActiveToken(query, cursorPosition);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function getActiveToken(query: string, cursorPosition: number) {
const tokenizedQuery = query.split(/[\s\n]/).reduce((acc, word, index) => {
export function getActiveToken(input: string, cursorPosition: number) {
const tokenizedQuery = input.split(/[\s\n]/).reduce((acc, word, index) => {
const previous = acc[index - 1];
const start = index === 0 ? index : previous.range[1] + 1;
const end = start + word.length;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
export function isValidTwitterUsername(username: string) {
return (
username.startsWith('@') &&
username.length > 1 &&
username.length <= 15 &&
/^@\w+$/.test(username)
);
return /^@\w{1,15}$/.test(username);
}

0 comments on commit d6d6ba9

Please sign in to comment.