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

feat(ui-select,ui-simple-select): add support for rendering selected … #1838

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
186 changes: 184 additions & 2 deletions cypress/component/Select.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
* SOFTWARE.
*/
import React from 'react'
import { Select } from '../../packages/ui'
import { IconCheckSolid } from '../../packages/ui-icons'
import { Select } from '@instructure/ui'
import { IconCheckSolid, IconEyeSolid } from '@instructure/ui-icons'
import 'cypress-real-events'
import '../support/component'

Expand All @@ -45,6 +45,44 @@ const getOptions = (
</Select.Option>
))

type ExampleOptionWithContent = 'opt1' | 'op2' | 'opt3'

const optionsWithBeforeContent = [
{
id: 'opt1',
label: 'Text',
renderBeforeLabel: 'XY'
},
{
id: 'opt2',
label: 'Icon',
renderBeforeLabel: 'YY'
},
{
id: 'opt3',
label: 'Colored Icon',
renderBeforeLabel: <IconEyeSolid />
}
]

const optionsWithAfterContent = [
{
id: 'opt1',
label: 'Text',
renderAfterLabel: 'XY'
},
{
id: 'opt2',
label: 'Icon',
renderAfterLabel: 'YY'
},
{
id: 'opt3',
label: 'Colored Icon',
renderAfterLabel: <IconEyeSolid />
}
]

describe('<Select/>', () => {
it('should render dynamically colored icons before option', async () => {
const renderBeforeLabel = (props: any) => {
Expand Down Expand Up @@ -338,4 +376,148 @@ describe('<Select/>', () => {
cy.wrap(onRequestHighlightOption).should('have.been.calledOnce')
cy.wrap(onKeyDown).should('have.been.calledTwice')
})

it("should render the selected option's before content in the input field", async () => {
const MyTestComponent = () => {
const [selectedOptionId, setSelectedOptionId] = React.useState<string>(
optionsWithBeforeContent[0].id
)
const [isShowingOptions, setIsShowingOptions] = React.useState(false)
const [inputValue, setInputValue] = React.useState<string | undefined>(
optionsWithBeforeContent[0].label
)

const getOptionById = (selectedOptionId) => {
return optionsWithBeforeContent.find(
({ id }) => id === selectedOptionId
)
}

const handleShowOptions = () => {
setIsShowingOptions(true)
}

const handleHideOptions = (options) => {
const option = getOptionById(selectedOptionId)?.label
setIsShowingOptions(false)
setInputValue(selectedOptionId ? option : '')
}

const handleSelectOption = (event, data: { id?: string }) => {
const option = data.id ? getOptionById(data.id)?.label : undefined
if (data.id) {
setSelectedOptionId(data.id)
setInputValue(option || '')
}
setIsShowingOptions(false)
}

return (
<Select
renderLabel="Choose an option"
inputValue={optionsWithBeforeContent[0].label}
isOptionContentAppliedToInput={true}
isShowingOptions={isShowingOptions}
onRequestShowOptions={handleShowOptions}
onRequestHideOptions={handleHideOptions}
onRequestSelectOption={handleSelectOption}
>
{optionsWithBeforeContent.map((opt) => (
<Select.Option
id={opt.id}
key={opt.id}
value={opt.label}
renderBeforeLabel={opt.renderBeforeLabel}
isSelected={opt.id === selectedOptionId}
></Select.Option>
))}
</Select>
)
}
cy.mount(<MyTestComponent />)

cy.get('span[class$="-textInput__beforeElement"]').should(
'contain.text',
'XY'
)
cy.get('input').click()
cy.get('ul[role="listbox"]').as('listbox')
cy.get('@listbox')
cy.get('@listbox').find('li').eq(1).click()
cy.get('span[class$="-textInput__beforeElement"]').should(
'contain.text',
'YY'
)
})

it("should render the selected option's after content in the input field", async () => {
const MyTestComponent = () => {
const [selectedOptionId, setSelectedOptionId] = React.useState<string>(
optionsWithAfterContent[0].id
)
const [isShowingOptions, setIsShowingOptions] = React.useState(false)
const [inputValue, setInputValue] = React.useState<string | undefined>(
optionsWithAfterContent[0].label
)

const getOptionById = (selectedOptionId) => {
return optionsWithAfterContent.find(({ id }) => id === selectedOptionId)
}

const handleShowOptions = () => {
setIsShowingOptions(true)
}

const handleHideOptions = (options) => {
const option = getOptionById(selectedOptionId)?.label
setIsShowingOptions(false)
setInputValue(selectedOptionId ? option : '')
}

const handleSelectOption = (event, data: { id?: string }) => {
const option = data.id ? getOptionById(data.id)?.label : undefined
if (data.id) {
setSelectedOptionId(data.id)
setInputValue(option || '')
}
setIsShowingOptions(false)
}

return (
<Select
renderLabel="Choose an option"
inputValue={optionsWithAfterContent[0].label}
isOptionContentAppliedToInput={true}
isShowingOptions={isShowingOptions}
onRequestShowOptions={handleShowOptions}
onRequestHideOptions={handleHideOptions}
onRequestSelectOption={handleSelectOption}
>
{optionsWithAfterContent.map((opt) => (
<Select.Option
id={opt.id}
key={opt.id}
value={opt.label}
renderAfterLabel={opt.renderAfterLabel}
isSelected={opt.id === selectedOptionId}
></Select.Option>
))}
</Select>
)
}
cy.mount(<MyTestComponent />)

cy.get('span[class$="-textInput__afterElement"]').should(
'contain.text',
'XY'
)
cy.get('input').click()
cy.get('ul[role="listbox"]').as('listbox')
cy.get('@listbox')
cy.get('@listbox').find('li').eq(1).click()
cy.get('span[class$="-textInput__afterElement"]').should(
'contain.text',
'YY'
)
})
})
Loading
Loading