Skip to content

Commit

Permalink
MMT-3410: Use widget wrapper for country select, removes test ids
Browse files Browse the repository at this point in the history
  • Loading branch information
macrouch committed Dec 18, 2023
1 parent 596b133 commit 4288459
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import React, { useMemo, useState } from 'react'
import React, {
useEffect,
useMemo,
useRef,
useState
} from 'react'
import PropTypes from 'prop-types'
import Select from 'react-select'
import { startCase } from 'lodash-es'
import countryList from 'react-select-country-list'
import CustomWidgetWrapper from '../CustomWidgetWrapper/CustomWidgetWrapper'
import shouldFocusField from '../../utils/shouldFocusField'

/**
* CustomCountrySelectWidget
Expand All @@ -21,11 +28,26 @@ import countryList from 'react-select-country-list'
const CustomCountrySelectWidget = ({
id,
label,
onBlur,
onChange,
registry,
required,
schema,
uiSchema,
value
}) => {
const selectScrollRef = useRef(null)
const focusRef = useRef(null)

const { description } = schema

const { formContext } = registry

const {
focusField,
setFocusField
} = formContext

// Pull the data from countryList once
const countryData = useMemo(() => countryList().getData(), [])

Expand Down Expand Up @@ -73,6 +95,11 @@ const CustomCountrySelectWidget = ({
})
}

const handleBlur = () => {
setFocusField(null)
onBlur(id)
}

// Uses label for title
let title = startCase(label.split(/-/)[0])

Expand All @@ -81,31 +108,37 @@ const CustomCountrySelectWidget = ({
title = uiSchema['ui:title']
}

const shouldFocus = shouldFocusField(focusField, id)

useEffect(() => {
// This useEffect for shouldFocus lets the refs be in place before trying to use them
if (shouldFocus) {
selectScrollRef.current?.scrollIntoView({ behavior: 'smooth' })
focusRef.current?.focus()
}
}, [shouldFocus])

return (
<div>
<div>
<span>
{title}
</span>

<span>
{/* // TODO This should be an icon */}
{required ? '*' : ''}
</span>
</div>

<div>
<Select
id={id}
isClearable
name={`Select-${label}`}
onChange={handleChange}
options={listOfCountries}
placeholder={`Select ${label}`}
value={selectedCountry}
/>
</div>
</div>
<CustomWidgetWrapper
description={description}
id={id}
label={label}
required={required}
scrollRef={selectScrollRef}
title={title}
>
<Select
id={id}
isClearable
name={`Select-${label}`}
onBlur={handleBlur}
onChange={handleChange}
options={listOfCountries}
placeholder={`Select ${label}`}
ref={focusRef}
value={selectedCountry}
/>
</CustomWidgetWrapper>
)
}

Expand All @@ -117,8 +150,18 @@ CustomCountrySelectWidget.defaultProps = {
CustomCountrySelectWidget.propTypes = {
id: PropTypes.string.isRequired,
label: PropTypes.string,
onBlur: PropTypes.func.isRequired,
onChange: PropTypes.func.isRequired,
registry: PropTypes.shape({
formContext: PropTypes.shape({
focusField: PropTypes.string,
setFocusField: PropTypes.func
}).isRequired
}).isRequired,
required: PropTypes.bool.isRequired,
schema: PropTypes.shape({
description: PropTypes.string
}).isRequired,
uiSchema: PropTypes.shape({
'ui:title': PropTypes.string
}).isRequired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ import CustomCountrySelectWidget from '../CustomCountrySelectWidget'

const setup = (overrideProps = {}) => {
const props = {
id: 'the-widget',
id: 'mock-id',
label: 'MyTestDataLabel',
required: false,
onBlur: jest.fn(),
onChange: jest.fn(),
value: 'TZ',
registry: {
formContext: {
focusField: '',
setFocusField: jest.fn()
}
},
required: false,
schema: {
description: 'Test Description'
},
uiSchema: {},
value: 'TZ',
...overrideProps
}

Expand Down Expand Up @@ -78,4 +88,20 @@ describe('CustomCountrySelectWidget', () => {
expect(props.onChange).toHaveBeenCalledWith('US')
})
})

describe('when the field should be focused', () => {
test('focuses the field', async () => {
setup({
registry: {
formContext: {
focusField: 'mock-id'
}
}
})

const field = screen.getByRole('combobox')

expect(field).toHaveFocus()
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ const CustomFieldTemplate = ({
errors,
children
}) => (
<div
data-testid="custom-field-template"
className={`custom-field-template ${classNames}`}
>
<div className={`custom-field-template ${classNames}`}>
{children}
{errors}
{help}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import CustomWidgetWrapper from '../CustomWidgetWrapper/CustomWidgetWrapper'

/**
* CustomMultiSelectWidget
* @typedef {Object} CustomArrayFieldTemplate
* @typedef {Object} CustomMultiSelectWidget
* @property {String} id The id of the widget.
* @property {String} label The label of the widget.
* @property {Boolean} onBlur Should blur a field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import useControlledKeywords from '../../hooks/useControlledKeywords'

/**
* CustomSelectWidget
* @typedef {Object} CustomArrayFieldTemplate
* @typedef {Object} CustomSelectWidget
* @property {Boolean} disable A boolean value to disable the select field.
* @property {String} label The label of the widget.
* @property {String} id The id of the widget.
Expand All @@ -32,7 +32,7 @@ import useControlledKeywords from '../../hooks/useControlledKeywords'

/**
* Renders Custom Select Widget
* @param {CustomArrayFieldTemplate} props
* @param {CustomSelectWidget} props
*/
const CustomSelectWidget = ({
disabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,24 +233,6 @@ describe('CustomSelectWidget', () => {
})
})

describe('when the field is focused', () => {
test('shows the field description', async () => {
setup()

const field = screen.getByRole('combobox')

await waitFor(async () => {
field.focus()
})

expect(field).toHaveFocus()

expect(CustomWidgetWrapper).toHaveBeenCalledWith(expect.objectContaining({
description: 'Test Description'
}), {})
})
})

describe('when the field is changed', () => {
test('calls onChange', async () => {
const { props, user } = setup()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,6 @@ describe('CustomTextWidget', () => {
})
})

describe('when the field is focused', () => {
test('shows the field description', async () => {
setup()

const field = screen.getByRole('textbox')

await waitFor(async () => {
field.focus()
})

expect(field).toHaveFocus()

expect(CustomWidgetWrapper).toHaveBeenCalledWith(expect.objectContaining({
description: 'Test Description'
}), {})
})
})

describe('when the field is blurred', () => {
test('clears the focusField and calls onBlur', async () => {
const { props } = setup()
Expand Down
1 change: 0 additions & 1 deletion static/src/js/components/DraftPreview/DraftPreview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ const DraftPreview = () => {
<Col md={12}>
<Button
className="eui-btn--blue display-modal"
data-testid="detailed-progress-view-publish-draft-btn"
onClick={
() => {
// TODO MMT-3411
Expand Down
3 changes: 1 addition & 2 deletions static/src/js/components/GridField/GridField.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { kebabCase } from 'lodash-es'
import React from 'react'
import PropTypes from 'prop-types'

Expand Down Expand Up @@ -136,7 +135,7 @@ class GridField extends ObjectField {
const UIComponent = render

return (
<span data-testid={`layout-grid-field__schema-field--${kebabCase(layoutName)}`}>
<span>
<UIComponent
{...props}
errorSchema={errorSchema}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
.navigation-item-error {
&__item {
// width: 330px;
// padding: 0;
// border: none;
// margin: 0;
// // background-color: #ebf2f6;
// background-color: var(--bs-secondary);
// font-size: 17px;

&--isFocused {
// background-color: #f5f5f5;
background-color: var(--bs-light);
}
}
Expand Down

0 comments on commit 4288459

Please sign in to comment.