-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I18N settings modal, creation (#10749)
- Loading branch information
Marvin Frachet
committed
Sep 8, 2021
1 parent
81aa2d8
commit b77feb9
Showing
18 changed files
with
403 additions
and
296 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
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
75 changes: 75 additions & 0 deletions
75
packages/core/helper-plugin/lib/src/components/Form/Form.stories.mdx
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,75 @@ | ||
<!--- EmptyStateLayout.stories.mdx ---> | ||
|
||
import { Meta, ArgsTable, Canvas, Story } from '@storybook/addon-docs'; | ||
import { Main, Row } from '@strapi/parts'; | ||
import { TextInput } from '@strapi/parts/TextInput'; | ||
import { Button } from '@strapi/parts/Button'; | ||
import { Box } from '@strapi/parts/Box'; | ||
import { Text } from '@strapi/parts/Text'; | ||
import { Formik } from 'formik'; | ||
import * as Yup from 'yup'; | ||
import Form from './'; | ||
|
||
<Meta title="components/Form" /> | ||
|
||
# Form | ||
|
||
This component is used to display a Formik form with additionnal error handling on fields | ||
|
||
## Per field error | ||
|
||
<Canvas> | ||
<Story name="Per field error"> | ||
<Main> | ||
<Formik initialValues={{ email: '' }} validationSchema={Yup.object().shape({ email: Yup.string().email('Invalid email').required('Required')})}> | ||
{({ handleSubmit, values, errors, handleChange }) => ( | ||
<Form onSubmit={handleSubmit}> | ||
<TextInput | ||
name="email" | ||
label="Email" | ||
hint="Enter a valid email" | ||
value={values.email} | ||
error={errors.email} | ||
value={values.displayName} | ||
onChange={handleChange} | ||
/> | ||
<Button type="submit">Submit</Button> | ||
</Form> | ||
)} | ||
</Formik> | ||
</Main> | ||
</Story> | ||
</Canvas> | ||
|
||
## Per form error | ||
|
||
Make sure to run this story in detached mode. The embeded iframe breaks the focus behaviour. | ||
|
||
<Canvas> | ||
<Story name="Per form error"> | ||
<Main> | ||
<Formik initialValues={{ email: '' }} validationSchema={Yup.object().shape({ email: Yup.string().email('Invalid email').required('Required')})} validateOnChange={false}> | ||
{({ handleSubmit, values, errors, handleChange }) => ( | ||
<Form onSubmit={handleSubmit} noValidate> | ||
{errors.email ? <Box padding={3} hasRadius background="danger100" color="danger600" tabIndex={-1} id="global-form-error"> | ||
<Text>The email is invalid</Text> | ||
</Box> : null} | ||
<TextInput | ||
name="email" | ||
label="Email" | ||
hint="Enter a valid email" | ||
value={values.email} | ||
value={values.displayName} | ||
onChange={handleChange} | ||
/> | ||
<Button type="submit">Submit</Button> | ||
</Form> | ||
)} | ||
</Formik> | ||
</Main> | ||
</Story> | ||
</Canvas> | ||
|
||
### Props | ||
|
||
<ArgsTable of={EmptyStateLayout} /> |
37 changes: 37 additions & 0 deletions
37
packages/core/helper-plugin/lib/src/components/Form/index.js
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,37 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
import { Form, useFormikContext } from 'formik'; | ||
|
||
const FormWithFocus = props => { | ||
const formRef = useRef(null); | ||
const { isSubmitting, isValidating, errors, touched } = useFormikContext(); | ||
|
||
useEffect(() => { | ||
if (isSubmitting && !isValidating) { | ||
const errorsInForm = formRef.current.querySelectorAll('[data-strapi-field-error]'); | ||
|
||
if (errorsInForm.length > 0) { | ||
const firstError = errorsInForm[0]; | ||
const describingId = firstError.getAttribute('id'); | ||
const formElementInError = formRef.current.querySelector( | ||
`[aria-describedby="${describingId}"]` | ||
); | ||
|
||
if (formElementInError) { | ||
formElementInError.focus(); | ||
} | ||
} | ||
} | ||
|
||
if (!isSubmitting && !isValidating && Object.keys(errors).length) { | ||
const el = document.getElementById('global-form-error'); | ||
|
||
if (el) { | ||
el.focus(); | ||
} | ||
} | ||
}, [errors, isSubmitting, isValidating, touched]); | ||
|
||
return <Form ref={formRef} {...props} noValidate />; | ||
}; | ||
|
||
export default FormWithFocus; |
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
37 changes: 0 additions & 37 deletions
37
packages/core/helper-plugin/lib/src/old/components/Form/index.js
This file was deleted.
Oops, something went wrong.
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
88 changes: 88 additions & 0 deletions
88
packages/plugins/i18n/admin/src/components/LocaleSelect/index.js
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,88 @@ | ||
import React from 'react'; | ||
import styled from 'styled-components'; | ||
import { Select, Option } from '@strapi/parts/Select'; | ||
import { Loader } from '@strapi/parts/Loader'; | ||
import { useIntl } from 'react-intl'; | ||
import PropTypes from 'prop-types'; | ||
import useLocales from '../../hooks/useLocales'; | ||
import useDefaultLocales from '../../hooks/useDefaultLocales'; | ||
import { getTrad } from '../../utils'; | ||
|
||
const SmallLoader = styled(Loader)` | ||
img { | ||
height: 1rem; | ||
width: 1rem; | ||
} | ||
`; | ||
|
||
/** | ||
* The component is memoized and needs a useCallback over the onLocaleChange and | ||
* onClear props to prevent the Select from re-rendering N times when typing on a specific | ||
* key in a formik form | ||
*/ | ||
const LocaleSelect = React.memo(({ value, onLocaleChange, error, onClear }) => { | ||
const { formatMessage } = useIntl(); | ||
const { defaultLocales, isLoading } = useDefaultLocales(); | ||
const { locales } = useLocales(); | ||
|
||
const options = (defaultLocales || []) | ||
.map(locale => ({ | ||
label: locale.name, | ||
value: locale.code, | ||
})) | ||
.filter(({ value: v }) => { | ||
const foundLocale = locales.find(({ code }) => code === v); | ||
|
||
return !foundLocale; | ||
}); | ||
|
||
const computedValue = value || ''; | ||
|
||
return ( | ||
<Select | ||
startIcon={isLoading ? <SmallLoader>Loading the locales...</SmallLoader> : undefined} | ||
aria-busy={isLoading} | ||
label={formatMessage({ | ||
id: getTrad('Settings.locales.modal.locales.label'), | ||
defaultMessage: 'Locales', | ||
})} | ||
onClear={value ? onClear : undefined} | ||
clearLabel={formatMessage({ | ||
id: 'clearLabel', | ||
defaultMessage: 'Clear', | ||
})} | ||
error={error} | ||
value={computedValue} | ||
onChange={selectedLocaleKey => { | ||
const selectedLocale = options.find(locale => locale.value === selectedLocaleKey); | ||
|
||
if (selectedLocale) { | ||
onLocaleChange({ code: selectedLocale.value, displayName: selectedLocale.label }); | ||
} | ||
}} | ||
> | ||
{isLoading | ||
? null | ||
: options.map(option => ( | ||
<Option value={option.value} key={option.value}> | ||
{option.label} | ||
</Option> | ||
))} | ||
</Select> | ||
); | ||
}); | ||
|
||
LocaleSelect.defaultProps = { | ||
error: undefined, | ||
value: undefined, | ||
onClear: () => undefined, | ||
}; | ||
|
||
LocaleSelect.propTypes = { | ||
error: PropTypes.string, | ||
onClear: PropTypes.func, | ||
onLocaleChange: PropTypes.func.isRequired, | ||
value: PropTypes.string, | ||
}; | ||
|
||
export default LocaleSelect; |
Oops, something went wrong.