import React, { useCallback, useMemo, useRef, useState } from 'react';
import { Search } from 'grommet-icons';
import { Box, Grommet, Image, Text, TextInput } from 'grommet';
import { grommet, ThemeType } from 'grommet/themes';
import { deepMerge } from 'grommet/utils';
// Type annotations can only be used in TypeScript files.
// Remove ': ThemeType' if you are not using Typescript.
const myCustomTheme: ThemeType = deepMerge(grommet, {
global: {
drop: {
background: '#444444',
shadowSize: 'medium',
extend: `
border-bottom-left-radius: 12px;
border-bottom-right-radius: 12px;
overflow: hidden;
`,
},
elevation: {
dark: {
medium: '0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23)',
},
light: {
medium: '0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23)',
},
},
font: {
size: '14px',
family: 'Arial',
},
input: {
weight: 400,
},
},
});
const folks = [
{
name: 'Alan Souza',
imageUrl:
'https://s.gravatar.com/avatar/b226da5c619b18b44eb95c30be393953?s=80',
},
{
name: 'Bryan Jacquot',
imageUrl:
'https://s.gravatar.com/avatar/10d15019166606cfed23846a7f902660?s=80',
},
{
name: 'Chris Carlozzi',
imageUrl:
'https://s.gravatar.com/avatar/56ea1e2ecd0d3cc85479b2d09e31d071?s=80',
},
{
name: 'Eric Soderberg',
imageUrl:
'https://s.gravatar.com/avatar/99020cae7ff399a4fbea19c0634f77c3?s=80',
},
{
name: 'Marlon Parizzotto',
imageUrl:
'https://s.gravatar.com/avatar/e6684969375a4dcc0aa99f0bfae544c3?s=80',
},
{
name: 'Tales Chaves',
imageUrl:
'https://s.gravatar.com/avatar/1f80adca55d9f5d97932ff97f631a4e8?s=80',
},
{
name: 'Tracy Barmore',
imageUrl:
'https://s.gravatar.com/avatar/4ec9c3a91da89f278e4482811caad7f3?s=80',
},
];
const formatSuggestions = (suggestedFolks, value) =>
suggestedFolks
.filter(({ name }) => name.toLowerCase().indexOf(value.toLowerCase()) >= 0)
.map(({ name, imageUrl }, index, list) => ({
label: (
{name}
),
value: name,
}));
export const CustomSuggestions = () => {
const [value, setValue] = useState('');
const [suggestionOpen, setSuggestionOpen] = useState(false);
const [suggestions, setSuggestions] = useState([]);
const boxRef = useRef();
const onChange = useCallback((event) => {
const { value: newValue } = event.target;
setValue(newValue);
if (!newValue.trim()) {
setSuggestions([]);
} else {
// simulate an async call to the backend
setTimeout(() => {
setSuggestions(formatSuggestions(folks, newValue));
}, 300);
}
}, []);
const onSuggestionSelect = useCallback(
(event) => setValue(event.suggestion.value),
[],
);
const onSuggestionsOpen = useCallback(() => {
setSuggestions(formatSuggestions(folks, value));
setSuggestionOpen(true);
}, [value]);
const onSuggestionsClose = useCallback(() => {
setSuggestions([]);
setSuggestionOpen(false);
}, []);
return (
);
};
CustomSuggestions.storyName = 'Custom suggestions';
export default {
title: 'Input/TextInput/Custom Themed/Custom suggestions',
};