-
Notifications
You must be signed in to change notification settings - Fork 22
Mise à jour du composant ChatInput avec fonctionnalité d'autocomplétion #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import React from 'react'; | ||
| import { Story, Meta } from '@storybook/react'; | ||
| import AutoCompleteList, { AutoCompleteListProps } from './AutoCompleteList'; | ||
|
|
||
| export default { | ||
| title: 'Components/AutoCompleteList', | ||
| component: AutoCompleteList, | ||
| } as Meta; | ||
|
|
||
| const Template: Story<AutoCompleteListProps> = (args) => ( | ||
| <AutoCompleteList {...args} /> | ||
| ); | ||
Teclit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const suggestions = [ | ||
| "Semper quis lectus nulla at volutpat diam ut.", | ||
| "Lacus sed viverra tellus in hac.", | ||
| "Enim sed faucibus turpis in eu mi.", | ||
| "Ac auctor augue mauris augue.", | ||
| "Odio facilisis mauris sit amet.", | ||
| "Rhoncus urna neque viverra justo.", | ||
| ]; | ||
|
|
||
| export const Default = Template.bind({}); | ||
| Default.args = { | ||
| suggestions, | ||
| onItemClick: (suggestion: string) => { | ||
| console.log(`Clicked on suggestion: ${suggestion}`); | ||
| }, | ||
| inputValue: '', | ||
| }; | ||
|
|
||
| export const WithInputValue = Template.bind({}); | ||
| WithInputValue.args = { | ||
| suggestions, | ||
| onItemClick: (suggestion: string) => { | ||
| console.log(`Clicked on suggestion: ${suggestion}`); | ||
| }, | ||
| inputValue: 'lorem', | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import styled, {StyledComponent} from '@emotion/styled'; | ||
| import React, {DetailedHTMLProps, HTMLAttributes} from 'react'; | ||
| import {prop} from "styled-tools"; | ||
|
|
||
| const AutoCompleteListContainer: StyledComponent< | ||
| DetailedHTMLProps<HTMLAttributes<HTMLUListElement>, HTMLUListElement> | ||
| > = styled.ul` | ||
| list-style: none; | ||
| width: 100%; | ||
| position:absolute; | ||
Teclit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| padding: 0; | ||
| margin: 0; | ||
| max-height: 100px; | ||
| overflow-y: auto; | ||
| overflow-x: hidden; | ||
| bottom: 2.3rem; | ||
| left: 0; | ||
| border: 1px solid #ccc; | ||
| border-radius: 5px; | ||
| background: ${prop<any>('theme.palette.background.input')}; | ||
| color: ${prop<any>('theme.palette.text.input')}; | ||
| `; | ||
|
|
||
| const AutoCompleteItem: StyledComponent< | ||
| DetailedHTMLProps<HTMLAttributes<HTMLLIElement>, HTMLLIElement> | ||
| > = styled.li` | ||
| border-bottom: 1px solid #ccc; | ||
| padding: 0.5em 1em; | ||
| cursor: pointer; | ||
| width: 100%; | ||
| &:hover { | ||
| background: #D7D7D7; | ||
| } | ||
| .bold { | ||
| font-weight: bold; | ||
| } | ||
Teclit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| `; | ||
| export interface AutoCompleteListProps { | ||
| suggestions: string[]; | ||
| onItemClick: (suggestion: string) => void; | ||
| inputValue: string; | ||
| } | ||
|
|
||
| const highlightMatch = (suggestion: string, inputValue: string) => { | ||
| const parts = suggestion.split(new RegExp(`(${inputValue})`, 'gi')); | ||
|
||
| return parts.map((part, index) => | ||
| part.toLowerCase() === inputValue.toLowerCase() ? ( | ||
|
||
| <span key={index} className="bold"> | ||
| {part} | ||
| </span> | ||
| ) : ( | ||
| <span key={index}>{part}</span> | ||
| ) | ||
Teclit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
| }; | ||
|
|
||
| const AutoCompleteList: React.FC<AutoCompleteListProps> = ({ | ||
|
||
| suggestions, | ||
| onItemClick, | ||
| inputValue | ||
| }) => ( | ||
| <AutoCompleteListContainer> | ||
| {suggestions.map((suggestion, index) => ( | ||
| <AutoCompleteItem key={index} onClick={() => onItemClick(suggestion)}> | ||
Teclit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| {highlightMatch(suggestion, inputValue)} | ||
| </AutoCompleteItem> | ||
| ))} | ||
| </AutoCompleteListContainer> | ||
| ); | ||
|
|
||
| export default AutoCompleteList; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import AutoCompleteList from './AutoCompleteList'; | ||
|
|
||
| export * from './AutoCompleteList'; | ||
| export default AutoCompleteList; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ export interface ChatProps { | |
| disableSse?: boolean; | ||
| accessibility?: TockAccessibility; | ||
| localStorageHistory?: TockLocalStorage; | ||
| autoCompletionEndPoint?: string; | ||
|
||
| } | ||
|
|
||
| const Chat: (props: ChatProps) => JSX.Element = ({ | ||
|
|
@@ -37,6 +38,7 @@ const Chat: (props: ChatProps) => JSX.Element = ({ | |
| disableSse = false, | ||
| accessibility = {}, | ||
| localStorageHistory = {}, | ||
| autoCompletionEndPoint, | ||
| }: ChatProps) => { | ||
| const { | ||
| messages, | ||
|
|
@@ -57,6 +59,7 @@ const Chat: (props: ChatProps) => JSX.Element = ({ | |
| extraHeadersProvider, | ||
| disableSse, | ||
| localStorageHistory, | ||
| autoCompletionEndPoint | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
|
|
@@ -100,6 +103,8 @@ const Chat: (props: ChatProps) => JSX.Element = ({ | |
| onSubmit={sendMessage} | ||
| accessibility={accessibility} | ||
| clearMessages={clearMessages} | ||
| autoCompletionEndPoint={autoCompletionEndPoint as string} | ||
| minValueLength={3} | ||
Teclit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /> | ||
| </Container> | ||
| ); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.