-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(component): add MultiSelect column type to worksheet (#1484)
- Loading branch information
Showing
8 changed files
with
248 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
'@bigcommerce/big-design': minor | ||
--- | ||
|
||
Added MultiSelect column type to Worksheet component |
This file contains 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 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
70 changes: 70 additions & 0 deletions
70
packages/big-design/src/components/Worksheet/editors/MultiSelectEditor/MultiSelectEditor.tsx
This file contains 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,70 @@ | ||
import React, { createRef, useCallback, useEffect } from 'react'; | ||
|
||
import { typedMemo } from '../../../../utils'; | ||
import { MultiSelect } from '../../../MultiSelect'; | ||
import { useWorksheetStore } from '../../hooks'; | ||
import { Cell, WorksheetItem, WorksheetSelectableColumn } from '../../types'; | ||
|
||
import { MultiSelectWrapper } from './styled'; | ||
|
||
export interface MultiSelectEditorProps<Item> { | ||
cell: Cell<Item>; | ||
isEditing: boolean; | ||
options?: WorksheetSelectableColumn<Item>['config']['options']; | ||
onBlur(): void; | ||
onChange(value: unknown): void; | ||
} | ||
|
||
const InternalMultiSelectEditor = <T extends WorksheetItem>({ | ||
cell, | ||
isEditing, | ||
onBlur, | ||
onChange, | ||
options = [], | ||
}: MultiSelectEditorProps<T>) => { | ||
const inputRef = createRef<HTMLInputElement>(); | ||
const { store, useStore } = useWorksheetStore(); | ||
|
||
const setEditingCell = useStore(store, (state) => state.setEditingCell); | ||
|
||
const value = Array.isArray(cell.value) ? cell.value : [cell.value]; | ||
|
||
useEffect(() => { | ||
if (isEditing && inputRef.current) { | ||
inputRef.current.focus(); | ||
} | ||
}, [inputRef, isEditing]); | ||
|
||
const handleChange = useCallback( | ||
(value: unknown[]) => { | ||
onChange(value); | ||
}, | ||
[onChange], | ||
); | ||
|
||
const handleOpen = useCallback(() => { | ||
setEditingCell({ cell }); | ||
}, [cell, setEditingCell]); | ||
|
||
const handleClose = useCallback(() => { | ||
onBlur(); | ||
setEditingCell({ cell: null }); | ||
}, [onBlur, setEditingCell]); | ||
|
||
return ( | ||
<MultiSelectWrapper> | ||
<MultiSelect | ||
disabled={cell.disabled} | ||
filterable={false} | ||
inputRef={inputRef} | ||
onClose={handleClose} | ||
onOpen={handleOpen} | ||
onOptionsChange={handleChange} | ||
options={options} | ||
value={value} | ||
/> | ||
</MultiSelectWrapper> | ||
); | ||
}; | ||
|
||
export const MultiSelectEditor = typedMemo(InternalMultiSelectEditor); |
1 change: 1 addition & 0 deletions
1
packages/big-design/src/components/Worksheet/editors/MultiSelectEditor/index.ts
This file contains 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 @@ | ||
export * from './MultiSelectEditor'; |
35 changes: 35 additions & 0 deletions
35
packages/big-design/src/components/Worksheet/editors/MultiSelectEditor/styled.ts
This file contains 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,35 @@ | ||
import { theme as defaultTheme } from '@bigcommerce/big-design-theme'; | ||
import styled from 'styled-components'; | ||
|
||
export const MultiSelectWrapper = styled.div` | ||
span { | ||
background-color: ${({ theme }) => theme.colors.inherit}; | ||
border: none; | ||
box-shadow: none; | ||
&:hover:not([disabled]) { | ||
border: none; | ||
} | ||
&[disabled] { | ||
background-color: ${({ theme }) => theme.colors.white}; | ||
} | ||
} | ||
input { | ||
font-size: ${({ theme }) => theme.typography.fontSize.small}; | ||
font-weight: ${({ theme }) => theme.typography.fontWeight.regular}; | ||
&[disabled] { | ||
background-color: ${({ theme }) => theme.colors.inherit}; | ||
color: ${({ theme }) => theme.colors.secondary50}; | ||
cursor: default; | ||
} | ||
} | ||
[role='option'] { | ||
font-size: ${({ theme }) => theme.typography.fontSize.small}; | ||
} | ||
`; | ||
|
||
MultiSelectWrapper.defaultProps = { theme: defaultTheme }; |
This file contains 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 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