Skip to content
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

fix: Input now support refs #123

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
abc7936
Allows our Input to be used with Refs the same way as native Antd Inp…
tibuurcio Mar 1, 2024
1de8722
Formatting
tibuurcio Mar 1, 2024
236681b
Merge branch 'main' into fix/input-forward-ref
tibuurcio Mar 14, 2024
f4c6f0e
Remove some unrelated stuff from this branch
tibuurcio Mar 14, 2024
8e808b8
Allows our Input to be used with Refs the same way as native Antd Inp…
tibuurcio Mar 1, 2024
35df00c
Formatting
tibuurcio Mar 1, 2024
65917fc
Remove some unrelated stuff from this branch
tibuurcio Mar 14, 2024
65d3846
Merge remote-tracking branch 'origin/fix/input-forward-ref' into fix/…
tibuurcio Mar 15, 2024
1c82b57
Commit generated CSS
tibuurcio Mar 15, 2024
2f111d5
Removes formatting changes
tibuurcio Mar 15, 2024
273fd0f
Merge branch 'fix/input-forward-ref' of https://github.com/mParticle/…
tibuurcio Mar 15, 2024
746eb3f
Commit generated CSS
tibuurcio Mar 15, 2024
f03f4f4
refactor: changes the usage of Inputs to come from the aquarium
tibuurcio Mar 15, 2024
7cc7e90
Merge branch 'fix/input-forward-ref' of https://github.com/mParticle/…
tibuurcio Mar 15, 2024
f719332
Commit generated CSS
tibuurcio Mar 15, 2024
1d38bff
Merge branch 'main' into fix/input-forward-ref
jared-dickman Mar 19, 2024
3862fe2
Commit generated CSS
jared-dickman Mar 19, 2024
155b0fe
Allows our Input to be used with Refs the same way as native Antd Inp…
tibuurcio Mar 1, 2024
7f7fb5c
Formatting
tibuurcio Mar 1, 2024
5793d41
Remove some unrelated stuff from this branch
tibuurcio Mar 14, 2024
a8b0668
Allows our Input to be used with Refs the same way as native Antd Inp…
tibuurcio Mar 1, 2024
f4a9a95
Remove some unrelated stuff from this branch
tibuurcio Mar 14, 2024
361d2ab
Removes formatting changes
tibuurcio Mar 15, 2024
b956008
refactor: changes the usage of Inputs to come from the aquarium
tibuurcio Mar 15, 2024
d4dbfc2
Merge branch 'fix/input-forward-ref' of https://github.com/mParticle/…
tibuurcio Mar 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 66 additions & 2 deletions src/components/data-entry/Input/Input.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Input } from 'src/components/data-entry/Input/Input'
import { Input, type InputRef } from 'src/components/data-entry/Input'
import { Button } from 'src/components/general/Button/Button'
import { type Meta, type StoryObj } from '@storybook/react'
import { ExampleStory } from 'src/utils/ExampleStory'
import { useRef } from 'react'
import { Space } from 'src/components'
import { expect, userEvent } from '@storybook/test'

const meta: Meta<typeof Input> = {
title: 'Aquarium/Data Entry/Input',
component: Input,
Expand Down Expand Up @@ -124,4 +130,62 @@ export const WithPrefixAndSuffix: Story = {
suffix: '.com',
placeholder: 'Email',
},
}
}

export const WithFocusManagement: Story = {
args: {
value: 'Test value',
},
render: (props, meta) => {
const inputRef = useRef<InputRef>(null)

const focus = (cursor: 'start' | 'end' | 'all' = 'start') => {
inputRef.current?.focus({
cursor,
})
}

return (
<ExampleStory title={meta.name}>
<Space direction="vertical" style={{ width: '100%' }}>
<Space wrap>
<Button
onClick={() => {
focus('start')
}}>
Focus at start
</Button>
<Button
onClick={() => {
focus('end')
}}>
Focus at last
</Button>
<Button
onClick={() => {
focus('all')
}}>
Focus to select all
</Button>
</Space>
<br />
<Input {...props} defaultValue="Welcome to the Aquarium" ref={inputRef} />
</Space>
</ExampleStory>
)
},
play: async story => {
const input = story.canvasElement.querySelector('input')
await expect(input).toBeInTheDocument()

const buttons = story.canvasElement.querySelectorAll('button')
await expect(buttons.length).toBe(3)

for (const button of buttons) {
input?.blur()
await userEvent.click(button)
await expect(input).toHaveFocus()
input?.blur()
}
},
}
13 changes: 5 additions & 8 deletions src/components/data-entry/Input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ import { Input as AntInput } from 'antd'
import { type InputProps as AntInputProps } from 'antd'
import { ConfigProvider } from 'src/components'
import { type InputRef } from 'antd'
import { forwardRef, type Ref } from 'react'

export interface IInputProps extends AntInputProps {}

export const Input = (props: IInputProps) => {
const Input = (props: IInputProps, ref: Ref<InputRef>) => {
return (
<ConfigProvider>
<AntInput {...props} />
<AntInput {...props} ref={ref} />
</ConfigProvider>
)
}

Input.Group = AntInput.Group
Input.Password = AntInput.Password
Input.Search = AntInput.Search
Input.TextArea = AntInput.TextArea
Comment on lines -18 to -21
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to keep this in the Input file if we can.


export { type InputRef }
const InputWithRef = forwardRef(Input)
export { InputWithRef as InternalInput }
19 changes: 19 additions & 0 deletions src/components/data-entry/Input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { type ForwardRefExoticComponent, type RefAttributes } from 'react'
import { Input as AntInput, type InputRef } from 'antd'
import { InternalInput, type IInputProps } from 'src/components/data-entry/Input/Input'

type CompoundedComponent = ForwardRefExoticComponent<IInputProps & RefAttributes<InputRef>> & {
Group: typeof AntInput.Group
Search: typeof AntInput.Search
TextArea: typeof AntInput.TextArea
Password: typeof AntInput.Password
}

const Input = InternalInput as CompoundedComponent

Input.Group = AntInput.Group
Input.Password = AntInput.Password
Input.Search = AntInput.Search
Input.TextArea = AntInput.TextArea

export { Input, type InputRef, type IInputProps }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious why we need an index for Input only, cant this go in src/components/index?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm testing these to see if we can use the already present index

2 changes: 1 addition & 1 deletion src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export { Slider, type ISliderProps } from './data-entry/Slider/Slider'
export { Cascader, type ICascaderProps } from './data-entry/Cascader/Cascader'
export { DatePicker, type IDatePickerProps } from './data-entry/DatePicker/DatePicker'
export { Checkbox, type ICheckboxProps } from './data-entry/Checkbox/Checkbox'
export { Input, type IInputProps } from './data-entry/Input/Input'
export { Input, type IInputProps, type InputRef } from './data-entry/Input'
export { InputNumber, type IInputNumberProps } from './data-entry/InputNumber/InputNumber'
export { Switch, type ISwitchProps } from './data-entry/Switch/Switch'
export { Upload, type IUploadProps } from './data-entry/Upload/Upload'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type INavigationOrg,
type INavigationWorkspace,
type IWorkspaceSelectorDisplayItem,
type InputRef,
Popover,
} from 'src/components'
import { Flex } from 'src/components'
Expand All @@ -17,10 +18,6 @@ import { useEffect } from 'react'
import { useMemo } from 'react'
import { debounce, hasImageAtSrc } from 'src/utils/utils'
import { getInitials } from 'src/utils/utils'

// TODO: Need to make our Input component comply with forwardRef to be able to import it from src/components
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise TODONE!

// As soon as https://github.com/mParticle/aquarium/pull/123 is merged
import { type InputRef } from 'antd'
import { WorkspaceSelectorContent } from 'src/components/navigation/GlobalNavigation/WorkspaceSelector/WorkspaceSelectorContent'
import { useMount } from 'src/hooks/useMount'
import { PaddingXxs } from 'src/styles/style'
Expand Down Expand Up @@ -144,8 +141,7 @@ export function WorkspaceSelector(props: IWorkspaceSelectorProps) {
signoutOptions={props.signoutOptions}
menuItems={menuItems}
/>
}
>
}>
<div className="globalNavigation__item workspaceSelector__menuItem">
<Avatar {...props.avatarOptions} className="workspaceSelector__avatar">
{getInitialsIfNoImage(hasImage, workspaceInitials)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { type ChangeEvent, type RefObject } from 'react'
import { Input, type InputRef } from 'antd'
import type { IWorkspaceSelectorProps } from 'src/components/navigation/GlobalNavigation/WorkspaceSelector/WorkspaceSelector'
import type { IWorkspaceSelectorDisplayItem } from 'src/components'
import { Input, type InputRef, type IWorkspaceSelectorDisplayItem } from 'src/components'
import { WorkspaceSelectorContentItems } from 'src/components/navigation/GlobalNavigation/WorkspaceSelector/WorkspaceSelectorContentItems'
import { WorkspaceNoResults } from "src/components/navigation/GlobalNavigation/WorkspaceSelector/WorkspaceNoResults";
import { WorkspaceSignout } from "src/components/navigation/GlobalNavigation/WorkspaceSelector/WorkspaceSignout";
import { WorkspaceNoResults } from 'src/components/navigation/GlobalNavigation/WorkspaceSelector/WorkspaceNoResults'
import { WorkspaceSignout } from 'src/components/navigation/GlobalNavigation/WorkspaceSelector/WorkspaceSignout'

type WorkspaceSelectorContentProps = {
onSearch: (e: ChangeEvent<HTMLInputElement>) => void
Expand Down Expand Up @@ -39,4 +38,4 @@ export function WorkspaceSelectorContent(props: WorkspaceSelectorContentProps) {
<WorkspaceSignout signoutOptions={signoutOptions} />
</div>
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,4 @@
.globalNavigation__mpSvg {
width: 28px;
height: var(--size-lg);
}
}
2 changes: 1 addition & 1 deletion src/styles/style.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Do not edit directly
* Generated on Tue, 19 Mar 2024 18:40:59 GMT
* Generated on Tue, 19 Mar 2024 18:51:20 GMT
*/

export const Blue = "#1677ff";
Expand Down