-
Notifications
You must be signed in to change notification settings - Fork 14.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add action bar for the bulk operation on variable list (#45392)
* add action bar snippet * initial setup * separate * improve * add close trigger * fix * fix skeleton width * add tooltip for upcoming implementation * move to datatable
- Loading branch information
1 parent
013401b
commit a562a85
Showing
5 changed files
with
253 additions
and
5 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,87 @@ | ||
/*! | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { useState, useCallback, useMemo } from "react"; | ||
|
||
type UseRowSelectionProps<T> = { | ||
data?: Array<T>; | ||
getKey: (item: T) => string; | ||
}; | ||
|
||
export type GetColumnsParams = { | ||
allRowsSelected: boolean; | ||
onRowSelect: (key: string, isChecked: boolean) => void; | ||
onSelectAll: (isChecked: boolean) => void; | ||
selectedRows: Map<string, boolean>; | ||
}; | ||
|
||
export const useRowSelection = <T>({ data = [], getKey }: UseRowSelectionProps<T>) => { | ||
const [selectedRows, setSelectedRows] = useState<Map<string, boolean>>(new Map()); | ||
|
||
const handleRowSelect = useCallback((key: string, isChecked: boolean) => { | ||
setSelectedRows((prev) => { | ||
const isAlreadySelected = prev.has(key); | ||
|
||
if (isChecked && !isAlreadySelected) { | ||
return new Map(prev).set(key, true); | ||
} else if (!isChecked && isAlreadySelected) { | ||
const newMap = new Map(prev); | ||
|
||
newMap.delete(key); | ||
|
||
return newMap; | ||
} | ||
|
||
return prev; | ||
}); | ||
}, []); | ||
|
||
const handleSelectAll = useCallback( | ||
(isChecked: boolean) => { | ||
setSelectedRows((prev) => { | ||
const newMap = new Map(prev); | ||
|
||
if (isChecked) { | ||
data.forEach((item) => newMap.set(getKey(item), true)); | ||
} else { | ||
data.forEach((item) => newMap.delete(getKey(item))); | ||
} | ||
|
||
return newMap; | ||
}); | ||
}, | ||
[data, getKey], | ||
); | ||
|
||
const allRowsSelected = useMemo( | ||
() => data.length > 0 && data.every((item) => selectedRows.has(getKey(item))), | ||
[data, selectedRows, getKey], | ||
); | ||
|
||
const clearSelections = useCallback(() => { | ||
setSelectedRows(new Map()); | ||
}, []); | ||
|
||
return { | ||
allRowsSelected, | ||
clearSelections, | ||
handleRowSelect, | ||
handleSelectAll, | ||
selectedRows, | ||
}; | ||
}; |
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,39 @@ | ||
/*! | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { ActionBar, Portal } from "@chakra-ui/react"; | ||
import { forwardRef } from "react"; | ||
|
||
type ActionBarContentProps = { | ||
portalled?: boolean; | ||
portalRef?: React.RefObject<HTMLElement>; | ||
} & ActionBar.ContentProps; | ||
|
||
export const Content = forwardRef<HTMLDivElement, ActionBarContentProps>((props, ref) => { | ||
const { children, portalled = true, portalRef, ...rest } = props; | ||
|
||
return ( | ||
<Portal container={portalRef} disabled={!portalled}> | ||
<ActionBar.Positioner> | ||
<ActionBar.Content ref={ref} {...rest} asChild={false}> | ||
{children} | ||
</ActionBar.Content> | ||
</ActionBar.Positioner> | ||
</Portal> | ||
); | ||
}); |
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,28 @@ | ||
/*! | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { ActionBar } from "@chakra-ui/react"; | ||
import { forwardRef } from "react"; | ||
|
||
import { CloseButton } from "./../CloseButton"; | ||
|
||
export const CloseTrigger = forwardRef<HTMLButtonElement, ActionBar.CloseTriggerProps>((props, ref) => ( | ||
<ActionBar.CloseTrigger {...props} asChild ref={ref}> | ||
<CloseButton size="sm" /> | ||
</ActionBar.CloseTrigger> | ||
)); |
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,28 @@ | ||
/*! | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { ActionBar as ChakraActionBar } from "@chakra-ui/react"; | ||
|
||
import { Content } from "./BarContent"; | ||
import { CloseTrigger } from "./CloseTrigger"; | ||
|
||
export const ActionBar = { | ||
...ChakraActionBar, | ||
CloseTrigger, | ||
Content, | ||
}; |
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