-
Notifications
You must be signed in to change notification settings - Fork 842
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EuiInputPopover] Add
panelMinWidth
prop (#7071)
- Loading branch information
Showing
8 changed files
with
190 additions
and
72 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 |
---|---|---|
@@ -1,60 +1,80 @@ | ||
import React, { useState } from 'react'; | ||
|
||
import { EuiInputPopover, EuiFieldText, EuiSpacer } from '../../../../src'; | ||
import { | ||
EuiInputPopover, | ||
EuiInputPopoverProps, | ||
EuiFieldText, | ||
EuiTextArea, | ||
EuiButtonGroup, | ||
EuiFormRow, | ||
EuiSpacer, | ||
} from '../../../../src'; | ||
|
||
export default () => { | ||
const [inputWidth, setInputWidth] = useState(200); | ||
const [isPopoverOpen, setIsPopoverOpen] = useState(false); | ||
const [isPopoverOpenTwo, setIsPopoverOpenTwo] = useState(false); | ||
const toggleIsPopoverOpen = (shouldBeOpen = !isPopoverOpen) => { | ||
setIsPopoverOpen(shouldBeOpen); | ||
}; | ||
const toggleIsPopoverOpenTwo = (shouldBeOpen = !isPopoverOpenTwo) => { | ||
setIsPopoverOpenTwo(shouldBeOpen); | ||
}; | ||
|
||
const input = ( | ||
<EuiFieldText | ||
onFocus={() => toggleIsPopoverOpen()} | ||
aria-label="Popover attached to input element" | ||
/> | ||
); | ||
|
||
const inputTwo = ( | ||
<EuiFieldText | ||
onFocus={() => { | ||
setInputWidth(300); | ||
toggleIsPopoverOpenTwo(); | ||
}} | ||
aria-label="Popover attached to an adjustable sized input element" | ||
/> | ||
); | ||
const [isResizablePopoverOpen, setIsResizablePopoverOpen] = useState(false); | ||
const [anchorPosition, setAnchorPosition] = | ||
useState<EuiInputPopoverProps['anchorPosition']>('downLeft'); | ||
|
||
return ( | ||
<React.Fragment> | ||
<> | ||
<EuiInputPopover | ||
input={input} | ||
isOpen={isPopoverOpen} | ||
closePopover={() => { | ||
toggleIsPopoverOpen(false); | ||
}} | ||
closePopover={() => setIsPopoverOpen(false)} | ||
input={ | ||
<EuiFieldText | ||
onFocus={() => setIsPopoverOpen(true)} | ||
placeholder="Focus me to toggle an input popover" | ||
aria-label="Popover attached to input element" | ||
/> | ||
} | ||
> | ||
Popover content | ||
</EuiInputPopover> | ||
|
||
<EuiSpacer /> | ||
|
||
<EuiInputPopover | ||
input={inputTwo} | ||
isOpen={isPopoverOpenTwo} | ||
style={{ width: inputWidth }} | ||
closePopover={() => { | ||
toggleIsPopoverOpenTwo(false); | ||
setInputWidth(200); | ||
}} | ||
display="inline-block" | ||
isOpen={isResizablePopoverOpen} | ||
closePopover={() => setIsResizablePopoverOpen(false)} | ||
input={ | ||
<EuiTextArea | ||
onKeyDown={(e) => { | ||
if (e.key === 'ArrowDown') { | ||
e.preventDefault(); | ||
setIsResizablePopoverOpen(true); | ||
} | ||
}} | ||
placeholder="Focus me, press the down arrow key, then drag the resize handle" | ||
aria-label="Press the down arrow key to toggle the popover attached to this textarea element." | ||
rows={2} | ||
resize="horizontal" | ||
/> | ||
} | ||
panelMinWidth={300} | ||
anchorPosition={anchorPosition} | ||
> | ||
Popover will adjust in size as the input does | ||
This popover has a minimum width of 300px, and will adjust in size as | ||
the textarea does. | ||
<EuiSpacer size="s" /> | ||
<EuiFormRow label="Anchor position" display="columnCompressed"> | ||
<EuiButtonGroup | ||
buttonSize="compressed" | ||
legend="Anchor position" | ||
name="anchorPosition" | ||
idSelected={anchorPosition!} | ||
onChange={(id) => | ||
setAnchorPosition(id as EuiInputPopoverProps['anchorPosition']) | ||
} | ||
options={[ | ||
{ id: 'downLeft', label: 'Left' }, | ||
{ id: 'downCenter', label: 'Center' }, | ||
{ id: 'downRight', label: 'Right' }, | ||
]} | ||
/> | ||
</EuiFormRow> | ||
</EuiInputPopover> | ||
</React.Fragment> | ||
</> | ||
); | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
/// <reference types="cypress" /> | ||
/// <reference types="cypress-real-events" /> | ||
/// <reference types="../../../cypress/support" /> | ||
|
||
import React from 'react'; | ||
|
||
import { EuiFieldText, EuiTextArea } from '../../components'; | ||
import { EuiInputPopover } from './input_popover'; | ||
|
||
describe('EuiPopover', () => { | ||
// The viewport width matters for position assertions, so ensure it's explicitly defined | ||
beforeEach(() => { | ||
cy.viewport(500, 300); | ||
}); | ||
|
||
const props = { | ||
input: <EuiFieldText />, | ||
closePopover: () => {}, | ||
isOpen: true, | ||
}; | ||
|
||
it('renders a popover with equal width to the input', () => { | ||
cy.mount(<EuiInputPopover {...props}>Popover content</EuiInputPopover>); | ||
cy.get('[data-popover-panel]') | ||
.should('have.css', 'left', '0px') | ||
.invoke('outerWidth') | ||
.should('equal', 400); | ||
}); | ||
|
||
it('respects `panelMinWidth`', () => { | ||
cy.mount( | ||
<EuiInputPopover {...props} panelMinWidth={450}> | ||
Popover content | ||
</EuiInputPopover> | ||
); | ||
cy.get('[data-popover-panel]').invoke('outerWidth').should('equal', 450); | ||
}); | ||
|
||
it('respects `anchorPosition`', () => { | ||
cy.mount( | ||
<div className="eui-textRight"> | ||
<EuiInputPopover | ||
{...props} | ||
display="inline-block" | ||
input={<EuiFieldText controlOnly={true} style={{ width: 150 }} />} | ||
panelMinWidth={300} | ||
anchorPosition="downRight" | ||
> | ||
Popover content | ||
</EuiInputPopover> | ||
</div> | ||
); | ||
cy.get('[data-popover-panel]').should('have.css', 'left', '200px'); | ||
}); | ||
|
||
it('correctly repositions the popover on input resize', () => { | ||
cy.mount( | ||
<div className="eui-textCenter"> | ||
<EuiInputPopover | ||
{...props} | ||
display="inline-block" | ||
input={ | ||
<EuiTextArea rows={1} resize="horizontal" style={{ width: 150 }} /> | ||
} | ||
> | ||
Popover content | ||
</EuiInputPopover> | ||
</div> | ||
); | ||
cy.get('[data-popover-panel]').should('have.css', 'left', '175px'); | ||
cy.wait(100); // Wait a tick, otherwise Cypress returns a false positive | ||
|
||
// Cypress doesn't seem to have a way to mimic manual dragging/resizing, so we'll do it programmatically | ||
cy.get('textarea').then(($el) => ($el[0].style.width = '500px')); | ||
cy.get('[data-popover-panel]').should('have.css', 'left', '50px'); | ||
}); | ||
}); |
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
Oops, something went wrong.