-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(withTableSettings): add the renderControls prop (#1357)
- Loading branch information
1 parent
40ee440
commit b3f0d78
Showing
10 changed files
with
515 additions
and
322 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
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
88 changes: 88 additions & 0 deletions
88
...nents/Table/__stories__/WithTableSettingsCustomActions/WithTableSettingsCustomActions.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,88 @@ | ||
import React from 'react'; | ||
|
||
import {ArrowRotateLeft} from '@gravity-ui/icons'; | ||
import _isEqual from 'lodash/isEqual'; | ||
|
||
import {Button} from '../../../Button'; | ||
import type {ButtonProps} from '../../../Button'; | ||
import {Icon} from '../../../Icon'; | ||
import {Flex} from '../../../layout'; | ||
import type {TableProps} from '../../Table'; | ||
import type {TableSettingsData} from '../../hoc/withTableSettings/withTableSettings'; | ||
import type {DataItem} from '../utils'; | ||
import {TableWithSettings} from '../utils'; | ||
|
||
interface WithTableSettingsCustomActionsShowcaseProps extends TableProps<DataItem> { | ||
defaultSettings: TableSettingsData; | ||
} | ||
|
||
export const WithTableSettingsCustomActionsShowcase = ({ | ||
defaultSettings, | ||
...restTableProps | ||
}: WithTableSettingsCustomActionsShowcaseProps) => { | ||
const [innerSettings, setInnerSettings] = React.useState<TableSettingsData>(defaultSettings); | ||
|
||
const updateSettings = React.useCallback( | ||
(updatedSettings: TableSettingsData) => setInnerSettings(updatedSettings), | ||
[], | ||
); | ||
|
||
const showSelectAllButton = React.useMemo( | ||
() => innerSettings.some(({isSelected}) => !isSelected), | ||
[innerSettings], | ||
); | ||
|
||
const showResetButton = React.useMemo( | ||
() => !_isEqual(innerSettings, defaultSettings), | ||
[defaultSettings, innerSettings], | ||
); | ||
|
||
return ( | ||
<TableWithSettings | ||
{...restTableProps} | ||
settings={innerSettings} | ||
updateSettings={updateSettings} | ||
renderControls={({DefaultApplyButton, onApply}) => ( | ||
<Flex gapRow="1" direction="column"> | ||
{showSelectAllButton && ( | ||
<SelectAllButton | ||
onClick={() => { | ||
onApply(); | ||
setInnerSettings((prevState) => | ||
prevState.map((setting) => ({ | ||
...setting, | ||
isSelected: true, | ||
})), | ||
); | ||
}} | ||
/> | ||
)} | ||
{showResetButton && ( | ||
<ResetButton | ||
onClick={() => { | ||
onApply(); | ||
setInnerSettings(defaultSettings); | ||
}} | ||
/> | ||
)} | ||
<DefaultApplyButton /> | ||
</Flex> | ||
)} | ||
/> | ||
); | ||
}; | ||
|
||
function SelectAllButton<T extends ButtonProps>({onClick}: T) { | ||
return <Button onClick={onClick}>Select All</Button>; | ||
} | ||
|
||
function ResetButton<T extends ButtonProps>({onClick}: T) { | ||
return ( | ||
<Button view="outlined-warning" onClick={onClick}> | ||
<Flex alignItems="center" gap="1"> | ||
<Icon key="icon" data={ArrowRotateLeft} /> | ||
<span>Reset</span> | ||
</Flex> | ||
</Button> | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
src/components/Table/__stories__/WithTableSettingsCustomActions/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 {WithTableSettingsCustomActionsShowcase} from './WithTableSettingsCustomActions'; |
Oops, something went wrong.