Skip to content

Commit

Permalink
Merge pull request #805 from securityscorecard/next
Browse files Browse the repository at this point in the history
🚀 Next release (July 20th)
  • Loading branch information
ajkl2533 authored Jul 20, 2023
2 parents 9b7234e + bb0590b commit 5471b92
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 22 deletions.
18 changes: 13 additions & 5 deletions src/components/Accordion/Accordion.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState } from 'react';
import { Meta, Story } from '@storybook/react/types-6-0';
import { includes } from 'ramda';

import { Inline, Stack } from '../layout';
import { HexGrade } from '../HexGrade';
Expand Down Expand Up @@ -72,7 +73,7 @@ CustomTitleElement.args = {
};

export const AcordionWithExternalManagement: Story<AccordionProps> = () => {
const [openItems, setOpenItems] = useState<string[]>([]);
const [openItems, setOpenItems] = useState<(string | number)[]>([]);
const handleOnClick = (id: string) => {
if (!openItems.includes(id)) {
const newItems = [id];
Expand All @@ -91,16 +92,23 @@ export const AcordionWithExternalManagement: Story<AccordionProps> = () => {
<Inline gap="xl">
<Stack gap="sm" justify="flex-start">
<Button variant="text" onClick={() => handleOnClick('first')}>
First section
{includes('first', openItems) && '->'} First section
</Button>
<Button variant="text" onClick={() => handleOnClick('second')}>
Second section
{includes('second', openItems) && '->'} Second section
</Button>
<Button variant="text" onClick={() => handleOnClick('third')}>
Third section
{includes('third', openItems) && '->'} Third section
</Button>
</Stack>
<Accordion items={localItems} openItems={openItems} />
<Accordion
isCollapsedOnOpen={false}
items={localItems}
openItems={openItems}
onChange={(ids) => {
setOpenItems(ids);
}}
/>
</Inline>
);
};
Expand Down
29 changes: 19 additions & 10 deletions src/components/Accordion/Accordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,37 @@ function filterState(
if (isCollapsedOnOpen) {
return [item];
}

return [...state, item];
}

const Accordion = React.forwardRef<HTMLDivElement, AccordionProps>(
(
{ isCollapsedOnOpen = true, items, openItems, className, ...props },
{
isCollapsedOnOpen = true,
items,
openItems,
className,
onChange,
...props
},
ref,
) => {
const [openIds, setOpenIds] = useState(pickOpen(items));

useEffect(
() =>
openItems?.forEach((item: AccordionItemId) =>
setOpenIds((state) => filterState(state, item, isCollapsedOnOpen)),
),
[openItems, isCollapsedOnOpen],
);
useEffect(() => {
if (openItems !== undefined) {
setOpenIds(isCollapsedOnOpen ? [openItems[0]] : openItems);
}
}, [openItems, isCollapsedOnOpen]);

const handleClick = useCallback(
(id: AccordionItemId) => {
setOpenIds((state) => filterState(state, id, isCollapsedOnOpen));
const nextState = filterState(openIds, id, isCollapsedOnOpen);
setOpenIds(nextState);
onChange?.(nextState);
},
[setOpenIds, isCollapsedOnOpen],
[openIds, setOpenIds, onChange, isCollapsedOnOpen],
);

return (
Expand All @@ -78,6 +86,7 @@ Accordion.propTypes = {
isCollapsedOnOpen: PropTypes.bool,
className: PropTypes.string,
openItems: PropTypes.arrayOf(AccordionItemIdPropType),
onChange: PropTypes.func,
};

export default Accordion;
3 changes: 2 additions & 1 deletion src/components/Accordion/Accordion.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export interface AccordionProps {
isCollapsedOnOpen?: boolean;
items: AccordionItem[];
className?: string;
openItems: AccordionItemId[];
openItems?: AccordionItemId[];
onChange?: (openIds: AccordionItemId[]) => void;
}

export const AccordionItemIdPropType = PropTypes.oneOfType([
Expand Down
1 change: 1 addition & 0 deletions src/components/Datatable/Datatable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ Datatable.propTypes = {
controlsConfig: PropTypes.exact({
onControlToggle: PropTypes.func,
onCancelLoading: PropTypes.func,
onColumnVisibilityChange: PropTypes.func,
hasSearch: PropTypes.bool,
searchConfig: PropTypes.exact({
placeholder: PropTypes.string,
Expand Down
1 change: 1 addition & 0 deletions src/components/Datatable/mocks/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export const controlsConfig = {
onSearch: action('onSearch'),
onClear: action('onClear'),
},
onColumnVisibilityChange: action('columnVisibilityChange'),
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ Playground.args = {
onValueRemove: action('OnValueRemove'),
onValuesChange: action('OnValuesChange'),
onInputChange: action('OnInputChange'),
onPaste: (e) => {
const pastedValue = (e.clipboardData || window.clipboardData).getData(
'text',
);
return pastedValue
.split(';')
.filter((i) => i !== '3')
.join(';');
},
};

export const Filled = MultiValueInputTemplate.bind({});
Expand Down
16 changes: 10 additions & 6 deletions src/components/forms/MultiValueInput/MultiValueInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ const MultiValueInput: React.FC<MultiValueInputProps> = ({
onValueRemove = noop,
onValuesChange = noop,
onInputChange = noop,
onPaste = noop,
placeholder,
pattern,
id,
Expand Down Expand Up @@ -174,10 +175,9 @@ const MultiValueInput: React.FC<MultiValueInputProps> = ({
split(';'),
map(trim),
filter(isNonEmptyString),
uniq,
)(newValue);
const newValues = [...values, ...parsedValues];
setValues(newValues);
setValues(uniq(newValues));
onValueAdd(parsedValues, newValues);
onValuesChange(newValues);
} else {
Expand Down Expand Up @@ -233,10 +233,13 @@ const MultiValueInput: React.FC<MultiValueInputProps> = ({
e,
) => {
e.preventDefault();
const pastedValue = (e.clipboardData || window.clipboardData).getData(
'text',
);
addValue(pastedValue);
const pastedValue = onPaste(e);

if (typeof pastedValue === 'string') {
addValue(pastedValue);
} else {
addValue((e.clipboardData || window.clipboardData).getData('text'));
}
};

const handleInputOnChange: React.ChangeEventHandler<HTMLInputElement> = (
Expand Down Expand Up @@ -363,6 +366,7 @@ MultiValueInput.propTypes = {
onValueRemove: PropTypes.func,
onValuesChange: PropTypes.func,
onInputChange: PropTypes.func,
onPaste: PropTypes.func,
};

export default MultiValueInput;
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ export interface MultiValueInputProps
onValueRemove?: (nextValues: MultiValueInputProps['value']) => void;
onValuesChange?: (nextValues: MultiValueInputProps['value']) => void;
onInputChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;
onPaste?: (event: React.ClipboardEvent<HTMLInputElement>) => void | string;
}

0 comments on commit 5471b92

Please sign in to comment.