|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import React, { useEffect, useRef, useState } from 'react'; |
| 4 | +import clsx from 'clsx'; |
| 5 | + |
| 6 | +import { PromptInput, PromptInputProps, SpaceBetween } from '~components'; |
| 7 | + |
| 8 | +import { SimplePage } from '../app/templates'; |
| 9 | + |
| 10 | +import styles from './style-api-v2.scss'; |
| 11 | + |
| 12 | +const menus: PromptInputProps.MenuDefinition[] = [ |
| 13 | + { |
| 14 | + id: 'mentions', |
| 15 | + trigger: '@', |
| 16 | + filteringType: 'auto', |
| 17 | + options: [ |
| 18 | + { value: 'user-1', label: 'Alice' }, |
| 19 | + { value: 'user-2', label: 'Bob' }, |
| 20 | + { value: 'user-3', label: 'Charlie', disabled: true }, |
| 21 | + ], |
| 22 | + }, |
| 23 | +]; |
| 24 | + |
| 25 | +const referenceTokens: PromptInputProps.InputToken[] = [ |
| 26 | + { type: 'text', value: 'Ask ' }, |
| 27 | + { type: 'reference', id: 'ref-1', label: 'Alice', value: 'user-1', menuId: 'mentions' }, |
| 28 | + { type: 'text', value: ' about the release' }, |
| 29 | +]; |
| 30 | + |
| 31 | +// Reference token ids become element ids, so the second instance on the page needs its own. |
| 32 | +const disabledReferenceTokens: PromptInputProps.InputToken[] = [ |
| 33 | + { type: 'text', value: 'Ask ' }, |
| 34 | + { type: 'reference', id: 'ref-2', label: 'Alice', value: 'user-1', menuId: 'mentions' }, |
| 35 | + { type: 'text', value: ' about the release' }, |
| 36 | +]; |
| 37 | + |
| 38 | +// Owned states (invalid, warning, disabled, read-only) are passed as a different class; the interaction states |
| 39 | +// (:hover, :focus-within on the root, :hover, :focus on the text element) are handled by selectors in the stylesheet. |
| 40 | +function getStyleClassNames({ invalid, warning, disabled, readOnly }: PromptInputProps) { |
| 41 | + const state = invalid ? 'invalid' : warning ? 'warning' : disabled ? 'disabled' : readOnly ? 'readonly' : null; |
| 42 | + return { |
| 43 | + root: state ? styles[`root-${state}`] : styles.root, |
| 44 | + textarea: clsx({ |
| 45 | + [styles['textarea-disabled']]: disabled, |
| 46 | + [styles['textarea-readonly']]: readOnly && !disabled, |
| 47 | + [styles.textarea]: !disabled && !readOnly, |
| 48 | + }), |
| 49 | + actionButton: styles['action-button'], |
| 50 | + token: disabled ? styles['token-disabled'] : styles.token, |
| 51 | + menu: styles.menu, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +function StyledPromptInput({ value: initialValue = '', ...rest }: Omit<PromptInputProps, 'tokens' | 'menus'>) { |
| 56 | + const [value, setValue] = useState(initialValue); |
| 57 | + return ( |
| 58 | + <PromptInput |
| 59 | + actionButtonAriaLabel="Send" |
| 60 | + {...rest} |
| 61 | + value={value} |
| 62 | + onChange={({ detail }) => setValue(detail.value)} |
| 63 | + {...{ styleClassNames: getStyleClassNames(rest) }} |
| 64 | + /> |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +function StyledTokenPromptInput({ |
| 69 | + tokens: initialTokens = [], |
| 70 | + openMenu = false, |
| 71 | + ...rest |
| 72 | +}: Omit<PromptInputProps, 'value' | 'menus'> & { openMenu?: boolean }) { |
| 73 | + const [tokens, setTokens] = useState<readonly PromptInputProps.InputToken[]>(initialTokens); |
| 74 | + const ref = useRef<PromptInputProps.Ref>(null); |
| 75 | + // Opens the menu on mount by typing the trigger character, so the menu slot is visible. |
| 76 | + useEffect(() => { |
| 77 | + if (openMenu) { |
| 78 | + ref.current?.focus(); |
| 79 | + ref.current?.insertText('@', 0, 1); |
| 80 | + } |
| 81 | + }, [openMenu]); |
| 82 | + return ( |
| 83 | + <PromptInput |
| 84 | + ref={ref} |
| 85 | + actionButtonAriaLabel="Send" |
| 86 | + {...rest} |
| 87 | + menus={menus} |
| 88 | + tokens={tokens} |
| 89 | + onChange={({ detail }) => setTokens(detail.tokens ?? [])} |
| 90 | + {...{ styleClassNames: getStyleClassNames(rest) }} |
| 91 | + /> |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +export default function () { |
| 96 | + return ( |
| 97 | + <SimplePage title="Prompt input - Style API v2" screenshotArea={{ disableAnimations: true }} i18n={{}}> |
| 98 | + <SpaceBetween size="m" direction="horizontal"> |
| 99 | + <StyledPromptInput ariaLabel="Default prompt" value="Summarize the release notes" /> |
| 100 | + <StyledPromptInput ariaLabel="Prompt with placeholder" placeholder="Ask a question" /> |
| 101 | + <StyledPromptInput |
| 102 | + ariaLabel="Prompt with action button" |
| 103 | + value="Summarize the release notes" |
| 104 | + actionButtonIconName="send" |
| 105 | + /> |
| 106 | + </SpaceBetween> |
| 107 | + <SpaceBetween size="m" direction="horizontal"> |
| 108 | + <StyledPromptInput ariaLabel="Invalid prompt" value="Too short" invalid={true} actionButtonIconName="send" /> |
| 109 | + <StyledPromptInput |
| 110 | + ariaLabel="Warning prompt" |
| 111 | + value="Almost at the limit" |
| 112 | + warning={true} |
| 113 | + actionButtonIconName="send" |
| 114 | + /> |
| 115 | + <StyledPromptInput |
| 116 | + ariaLabel="Read-only prompt" |
| 117 | + value="Read-only text" |
| 118 | + readOnly={true} |
| 119 | + actionButtonIconName="send" |
| 120 | + /> |
| 121 | + </SpaceBetween> |
| 122 | + <SpaceBetween size="m" direction="horizontal"> |
| 123 | + <StyledPromptInput |
| 124 | + ariaLabel="Disabled prompt" |
| 125 | + value="Cannot be edited" |
| 126 | + disabled={true} |
| 127 | + actionButtonIconName="send" |
| 128 | + /> |
| 129 | + <StyledPromptInput ariaLabel="Disabled prompt with placeholder" placeholder="Ask a question" disabled={true} /> |
| 130 | + </SpaceBetween> |
| 131 | + <SpaceBetween size="m" direction="horizontal"> |
| 132 | + <StyledTokenPromptInput |
| 133 | + ariaLabel="Prompt with references" |
| 134 | + tokens={referenceTokens} |
| 135 | + actionButtonIconName="send" |
| 136 | + /> |
| 137 | + <StyledTokenPromptInput |
| 138 | + ariaLabel="Disabled prompt with references" |
| 139 | + tokens={disabledReferenceTokens} |
| 140 | + disabled={true} |
| 141 | + /> |
| 142 | + <StyledTokenPromptInput ariaLabel="Prompt with references and placeholder" placeholder="Ask a question" /> |
| 143 | + <StyledTokenPromptInput |
| 144 | + ariaLabel="Disabled prompt with references and placeholder" |
| 145 | + placeholder="Ask a question" |
| 146 | + disabled={true} |
| 147 | + /> |
| 148 | + <StyledTokenPromptInput ariaLabel="Prompt with open menu" openMenu={true} /> |
| 149 | + </SpaceBetween> |
| 150 | + </SimplePage> |
| 151 | + ); |
| 152 | +} |
0 commit comments