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

WrapperWidgetUI support externally controlled expanded attribute #375

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Not released

- WrapperWidgetUI support externally controlled `expanded` attribute [#375](https://github.com/CartoDB/carto-react/pull/375)

## 1.3

### 1.3.0-alpha.2 (2022-04-11)
Expand Down
47 changes: 47 additions & 0 deletions packages/react-ui/__tests__/widgets/WrapperWidgetUI.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,53 @@ describe('WrapperWidgetUI', () => {
});
});

describe('externally controlled `expanded` state', () => {
const onExpandedChange = jest.fn();
test('should obey `expanded=false` with `onExpandedChange` set', () => {
render(
<WrapperWidgetUI
expanded={false}
title={TITLE}
onExpandedChange={onExpandedChange}
>
<div>CONTENT</div>
</WrapperWidgetUI>
);
expect(screen.queryByText('CONTENT')).not.toBeInTheDocument();
expect(onExpandedChange).not.toBeCalled();
});

test('should attempt to close using `onExpandedChange`', async () => {
render(
<WrapperWidgetUI
expanded={true}
title={TITLE}
onExpandedChange={onExpandedChange}
>
<div>CONTENT</div>
</WrapperWidgetUI>
);
expect(screen.getByText('CONTENT')).toBeInTheDocument();
fireEvent.click(screen.getByText(TITLE));
expect(onExpandedChange).toBeCalledWith(false);
});

test('should attempt to open using `onExpandedChange', async () => {
render(
<WrapperWidgetUI
expanded={false}
title={TITLE}
onExpandedChange={onExpandedChange}
>
<div>CONTENT</div>
</WrapperWidgetUI>
);
expect(screen.queryByText('CONTENT')).not.toBeInTheDocument();
fireEvent.click(screen.getByText(TITLE));
expect(onExpandedChange).toBeCalledWith(true);
});
});

describe('with options', () => {
const NUMBER_OF_OPTIONS = 3;
const OPTIONS = [...Array(NUMBER_OF_OPTIONS)].map((_, idx) => ({
Expand Down
2 changes: 2 additions & 0 deletions packages/react-ui/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export type WrapperWidgetUI = {
title: string;
isLoading?: boolean;
expandable?: boolean;
expanded?: boolean;
onExpandedChange?: (v: boolean) => void;
actions?: { id: string; name: string; icon: React.ReactElement; action: Function }[];
options?: { id: string; name: string; action: Function }[];
children?: React.ReactNode;
Expand Down
12 changes: 11 additions & 1 deletion packages/react-ui/src/widgets/WrapperWidgetUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,14 @@ const useStyles = makeStyles((theme) => ({

function WrapperWidgetUI(props) {
const wrapper = createRef();
const [expanded, setExpanded] = useState(true);

const [expandedInt, setExpandedInt] = useState(true);
const externalExpanded =
typeof props.expanded === 'boolean' && typeof props.onExpandedChange === 'function';
const expanded =
props.expandable !== false ? (externalExpanded ? props.expanded : expandedInt) : true;
const setExpanded = externalExpanded ? props.onExpandedChange : setExpandedInt;

const [anchorEl, setAnchorEl] = useState(null);
const classes = useStyles({ ...props, expanded });
const open = Boolean(anchorEl);
Expand Down Expand Up @@ -244,13 +251,16 @@ function WrapperWidgetUI(props) {
}

WrapperWidgetUI.defaultProps = {
expanded: true,
expandable: true,
isLoading: false
};

WrapperWidgetUI.propTypes = {
title: PropTypes.string.isRequired,
expandable: PropTypes.bool,
expanded: PropTypes.bool,
onExpandedChange: PropTypes.func,
isLoading: PropTypes.bool,
actions: PropTypes.arrayOf(
PropTypes.shape({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export const Expandable = Template.bind({});
const ExpandableProps = { title: 'Expandable', expandable: true };
Expandable.args = ExpandableProps;

export const NotExpanded = Template.bind({});
const NotExpandedProps = { title: 'Not expanded/collapsed', expanded: false };
NotExpanded.args = NotExpandedProps;

export const NotExpandable = Template.bind({});
const NotExpandableProps = { title: 'Not Expandable', expandable: false };
NotExpandable.args = NotExpandableProps;
Expand Down