-
Notifications
You must be signed in to change notification settings - Fork 15
[LW-9151] Edit account name component #746
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7c36ab7
feat(ui): input component
a3ed6d6
feat(extension): edit account drawer
de3223c
feat(extension): add unit test
69f3f66
feat: rename input component
e8d9ff5
feat(ui): use pseudo parameters
2060606
feat(ui): input width
31ad478
feat(ui): input disabled style
582b28d
feat(extension): add ui package
7077818
feat: update lock file
7391a3f
feat(ui): remove redundant styles
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...r-extension-wallet/src/features/account/components/EditAccount/EditAccountDrawer.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import React from 'react'; | ||
| import { render, fireEvent, screen } from '@testing-library/react'; | ||
| import { EditAccountDrawer } from './EditAccountDrawer'; | ||
| import '@testing-library/jest-dom'; | ||
|
|
||
| jest.mock('react-i18next', () => ({ | ||
| useTranslation: () => ({ t: jest.fn() }) | ||
| })); | ||
|
|
||
| describe('EditAccountDrawer', () => { | ||
| const onSaveMock = jest.fn(); | ||
| const hideMock = jest.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('displays default account name', () => { | ||
| render(<EditAccountDrawer name="" index="1" visible onSave={onSaveMock} hide={hideMock} />); | ||
|
|
||
| expect(screen.getByTestId('edit-account')).toBeInTheDocument(); | ||
| expect(screen.getByTestId('drawer-navigation-title')).toHaveTextContent('Account #1'); | ||
| expect(screen.getByTestId('edit-account-name-input')).toHaveValue(''); | ||
| expect(screen.getByTestId('edit-account-save-btn')).toBeDisabled(); | ||
| }); | ||
|
|
||
| it('displays correct account name', () => { | ||
| render(<EditAccountDrawer name="Test Account" index="1" visible onSave={onSaveMock} hide={hideMock} />); | ||
|
|
||
| expect(screen.getByTestId('edit-account')).toBeInTheDocument(); | ||
| expect(screen.getByTestId('drawer-navigation-title')).toHaveTextContent('Test Account'); | ||
| expect(screen.getByTestId('edit-account-name-input')).toHaveValue('Test Account'); | ||
| expect(screen.getByTestId('edit-account-save-btn')).toBeDisabled(); | ||
| }); | ||
|
|
||
| it('updates input value on change and enables save button', () => { | ||
| render(<EditAccountDrawer name="Test Account" index="1" visible onSave={onSaveMock} hide={hideMock} />); | ||
|
|
||
| const input = screen.getByTestId('edit-account-name-input'); | ||
|
|
||
| fireEvent.change(input, { target: { value: 'New Account Name' } }); | ||
| fireEvent.click(screen.getByTestId('edit-account-save-btn')); | ||
|
|
||
| expect(input).toHaveValue('New Account Name'); | ||
| expect(screen.getByTestId('drawer-navigation-title')).toHaveTextContent('Test Account'); | ||
| expect(screen.getByTestId('edit-account-save-btn')).toBeEnabled(); | ||
| expect(onSaveMock).toHaveBeenCalledWith('New Account Name'); | ||
| }); | ||
|
|
||
| it('calls hide function when Cancel button is clicked', () => { | ||
| render(<EditAccountDrawer name="Test Account" index="1" visible onSave={onSaveMock} hide={hideMock} />); | ||
|
|
||
| fireEvent.click(screen.getByTestId('edit-account-cancel-btn')); | ||
|
|
||
| expect(hideMock).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
60 changes: 60 additions & 0 deletions
60
...rowser-extension-wallet/src/features/account/components/EditAccount/EditAccountDrawer.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import React, { useState } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { Box, Flex, Button, Text, TextBox } from '@lace/ui'; | ||
| import { Drawer, DrawerNavigation } from '@lace/common'; | ||
|
|
||
| export type Props = { | ||
| onSave: (name: string) => void; | ||
| visible: boolean; | ||
| hide: () => void; | ||
| name: string; | ||
| index: string; | ||
| }; | ||
|
|
||
| export const EditAccountDrawer = ({ name, index, visible, onSave, hide }: Props): React.ReactElement => { | ||
| const { t } = useTranslation(); | ||
| const [currentName, setCurrentName] = useState(name); | ||
|
|
||
| return ( | ||
| <Drawer | ||
| zIndex={999} | ||
| open={visible} | ||
| navigation={<DrawerNavigation title={name || `Account #${index}`} onCloseIconClick={hide} />} | ||
| footer={ | ||
| <Flex flexDirection="column"> | ||
| <Box mb="$16" w="$fill"> | ||
| <Button.CallToAction | ||
| w="$fill" | ||
| disabled={name === currentName} | ||
| onClick={() => onSave(currentName)} | ||
| data-testid="edit-account-save-btn" | ||
| label={t('account.edit.footer.save')} | ||
| /> | ||
| </Box> | ||
| <Button.Secondary | ||
| w="$fill" | ||
| onClick={hide} | ||
| data-testid="edit-account-cancel-btn" | ||
| label={t('account.edit.footer.cancel')} | ||
| /> | ||
| </Flex> | ||
| } | ||
| > | ||
| <div data-testid="edit-account"> | ||
| <Box mb="$16"> | ||
| <Text.SubHeading weight="$bold">{t('account.edit.title')}</Text.SubHeading> | ||
| </Box> | ||
| <Box mb="$64"> | ||
| <Text.Body.Normal>{t('account.edit.subtitle')}</Text.Body.Normal> | ||
| </Box> | ||
| <TextBox | ||
| data-testid="edit-account-name-input" | ||
| containerStyle={{ width: '100%' }} | ||
| label={t('account.edit.input.label')} | ||
| value={currentName} | ||
| onChange={(e) => setCurrentName(e.target.value)} | ||
| /> | ||
| </div> | ||
| </Drawer> | ||
| ); | ||
| }; |
11 changes: 11 additions & 0 deletions
11
apps/browser-extension-wallet/src/features/account/components/EditAccount/hooks.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { useState } from 'react'; | ||
|
|
||
| export const useEditAccountDrawer = (): { isOpen: boolean; open: () => void; hide: () => void } => { | ||
| const [visible, setVisible] = useState(false); | ||
|
|
||
| return { | ||
| isOpen: visible, | ||
| open: () => setVisible(true), | ||
| hide: () => setVisible(false) | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { TextBox } from './text-box.component'; | ||
| export type { TextBoxProps } from './text-box.component'; |
72 changes: 72 additions & 0 deletions
72
packages/ui/src/design-system/text-box/text-box.component.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import React from 'react'; | ||
|
|
||
| import * as Form from '@radix-ui/react-form'; | ||
| import cn from 'classnames'; | ||
|
|
||
| import { Flex } from '../flex'; | ||
| import * as Typography from '../typography'; | ||
|
|
||
| import * as cx from './text-box.css'; | ||
|
|
||
| export interface TextBoxProps extends Form.FormControlProps { | ||
| required?: boolean; | ||
| disabled?: boolean; | ||
| id?: string; | ||
| label: string; | ||
| name?: string; | ||
| value: string; | ||
| errorMessage?: string; | ||
| onChange?: (event: Readonly<React.ChangeEvent<HTMLInputElement>>) => void; | ||
| containerClassName?: string; | ||
| containerStyle?: React.CSSProperties; | ||
| 'data-testid'?: string; | ||
| } | ||
|
|
||
| export const TextBox = ({ | ||
| required = false, | ||
| disabled = false, | ||
| id, | ||
| label, | ||
| name, | ||
| value, | ||
| errorMessage = '', | ||
| containerClassName = '', | ||
| onChange, | ||
| containerStyle, | ||
| ...rest | ||
| }: Readonly<TextBoxProps>): JSX.Element => { | ||
| return ( | ||
| <Form.Root> | ||
| <Flex justifyContent="space-between" alignItems="center"> | ||
| <Form.Field | ||
| name="field" | ||
| className={cn(cx.container, { | ||
| [containerClassName]: containerClassName, | ||
| })} | ||
| style={containerStyle} | ||
| > | ||
| <Form.Control asChild> | ||
| <input | ||
| type="text" | ||
| required={required} | ||
| placeholder="" | ||
| className={cx.input} | ||
| disabled={disabled} | ||
| name={name} | ||
| value={value} | ||
| onChange={onChange} | ||
| id={id} | ||
| data-testid={rest['data-testid']} | ||
| /> | ||
| </Form.Control> | ||
| <Form.Label className={cn(cx.label)}>{label}</Form.Label> | ||
| </Form.Field> | ||
| </Flex> | ||
| {errorMessage && ( | ||
| <Typography.Label className={cx.errorMessage}> | ||
| {errorMessage} | ||
| </Typography.Label> | ||
| )} | ||
| </Form.Root> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { globalStyle, vars, style } from '../../design-tokens'; | ||
|
|
||
| export const container = style({ | ||
| background: vars.colors.$input_container_bgColor, | ||
| paddingTop: vars.spacing.$12, | ||
| maxHeight: vars.spacing.$52, | ||
| borderRadius: vars.radius.$medium, | ||
| width: 'auto', | ||
| fontWeight: vars.fontWeights.$semibold, | ||
| fontFamily: vars.fontFamily.$nova, | ||
| }); | ||
|
|
||
| export const input = style({ | ||
| width: 'calc(100% - 90px)', | ||
| fontSize: vars.fontSizes.$18, | ||
| padding: `${vars.spacing.$10} ${vars.spacing.$20}`, | ||
| border: 'none', | ||
| outline: 'none', | ||
| background: 'transparent', | ||
| color: vars.colors.$input_value_color, | ||
| }); | ||
|
|
||
| export const label = style({ | ||
| position: 'relative', | ||
| display: 'block', | ||
| left: vars.spacing.$20, | ||
| top: '-40px', | ||
| transitionDuration: '0.2s', | ||
| pointerEvents: 'none', | ||
| color: vars.colors.$input_label_color, | ||
| fontSize: vars.fontSizes.$18, | ||
| }); | ||
|
|
||
| export const errorMessage = style({ | ||
| color: vars.colors.$input_error_message_color, | ||
| marginLeft: vars.spacing.$20, | ||
| }); | ||
|
|
||
| globalStyle( | ||
| `${input}:focus + ${label}, ${input}:not(:placeholder-shown) + ${label}`, | ||
| { | ||
| top: '-52px', | ||
| fontSize: vars.fontSizes.$12, | ||
| }, | ||
| ); | ||
|
|
||
| globalStyle(`${container}:has(${input}:disabled)`, { | ||
| opacity: vars.opacities.$0_5, | ||
| }); | ||
|
|
||
| globalStyle(`${container}:has(${input}:hover:not(:disabled))`, { | ||
| outline: `2px solid ${vars.colors.$input_container_hover_outline_color}`, | ||
| }); | ||
|
|
||
| globalStyle(`${container}:has(${input}:focus)`, { | ||
| outline: `3px solid ${vars.colors.$input_container_focused_outline_color}`, | ||
| }); |
96 changes: 96 additions & 0 deletions
96
packages/ui/src/design-system/text-box/text-box.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import React from 'react'; | ||
|
|
||
| import type { Meta } from '@storybook/react'; | ||
|
|
||
| import { LocalThemeProvider, ThemeColorScheme } from '../../design-tokens'; | ||
| import { page, Section, Variants } from '../decorators'; | ||
| import { Divider } from '../divider'; | ||
| import { Flex } from '../flex'; | ||
| import { Cell, Grid } from '../grid'; | ||
|
|
||
| import { TextBox } from './text-box.component'; | ||
|
|
||
| export default { | ||
| title: 'Input Fields/Text Box', | ||
| component: TextBox, | ||
| decorators: [ | ||
| page({ | ||
| title: 'Text Box', | ||
| subtitle: 'A text input', | ||
| }), | ||
| ], | ||
| } as Meta; | ||
|
|
||
| const MainComponents = (): JSX.Element => ( | ||
| <Variants.Row> | ||
| <Variants.Cell> | ||
| <TextBox label="Label" value="" /> | ||
| </Variants.Cell> | ||
| <Variants.Cell> | ||
| <TextBox label="Label" value="" id="hover" /> | ||
| </Variants.Cell> | ||
| <Variants.Cell> | ||
| <TextBox label="Label" value="Input Text" /> | ||
| </Variants.Cell> | ||
| <Variants.Cell> | ||
| <TextBox label="Label" value="Input Text" errorMessage="Error" /> | ||
| </Variants.Cell> | ||
| <Variants.Cell> | ||
| <TextBox label="Label" value="Input Text" disabled /> | ||
| </Variants.Cell> | ||
| <Variants.Cell> | ||
| <TextBox label="Label" value="" id="focus" /> | ||
| </Variants.Cell> | ||
| </Variants.Row> | ||
| ); | ||
|
|
||
| export const Overview = (): JSX.Element => { | ||
| const [value, setValue] = React.useState(''); | ||
|
|
||
| return ( | ||
| <Grid> | ||
| <Cell> | ||
| <Section title="Copy for use"> | ||
| <Flex flexDirection="column" alignItems="center" w="$fill" my="$32"> | ||
| <TextBox | ||
| value={value} | ||
| label="Text" | ||
| onChange={(event): void => { | ||
| setValue(event.target.value); | ||
| }} | ||
| /> | ||
| </Flex> | ||
| </Section> | ||
|
|
||
| <Divider my="$64" /> | ||
|
|
||
| <Section title="Main components"> | ||
| <Variants.Table | ||
| headers={[ | ||
| 'Rest', | ||
| 'Hover', | ||
| 'Active/Pressed', | ||
| 'Error', | ||
| 'Disabled', | ||
| 'Focused', | ||
| ]} | ||
| > | ||
| <MainComponents /> | ||
| </Variants.Table> | ||
| <LocalThemeProvider colorScheme={ThemeColorScheme.Dark}> | ||
| <Variants.Table> | ||
| <MainComponents /> | ||
| </Variants.Table> | ||
| </LocalThemeProvider> | ||
| </Section> | ||
| </Cell> | ||
| </Grid> | ||
| ); | ||
| }; | ||
|
|
||
| Overview.parameters = { | ||
| pseudo: { | ||
| hover: '#hover', | ||
| focus: '#focus', | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool stuff! :) are we sure that we want to write these tests here when they probably gonna be covered in a similar way by the SDET team with e2e tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they are valid as it's quicker feedback for developers when updating the component, but I'm opened for a deeper discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another useful test would be to combine the hook + drawer to showcase that it can be shown and hidden correctly.