-
Notifications
You must be signed in to change notification settings - Fork 0
Add react-hook-form to ImageForm #1310
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
Open
Jonas-C
wants to merge
1
commit into
master
Choose a base branch
from
image-form-to-react-hook-form
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,56 @@ | ||
/** | ||
* Copyright (c) 2021-present, NDLA. | ||
* | ||
* This source code is licensed under the GPLv3 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import { createContext, Dispatch, ReactNode, SetStateAction, useContext, useState } from 'react'; | ||
|
||
type EventType = 'reset'; | ||
|
||
interface FormEvent { | ||
id: number; | ||
type: EventType; | ||
} | ||
interface EventState { | ||
events: FormEvent[]; | ||
} | ||
const FormEventContext = createContext< | ||
[EventState, Dispatch<SetStateAction<EventState>>] | undefined | ||
>(undefined); | ||
|
||
interface Props { | ||
children: ReactNode; | ||
} | ||
|
||
export interface FormEventProps { | ||
events: FormEvent[]; | ||
dispatchEvent: (type: EventType) => void; | ||
} | ||
|
||
export const FormEventProvider = ({ children }: Props) => { | ||
const initialValues: EventState = { events: [] }; | ||
const eventContext = useState<EventState>(initialValues); | ||
return <FormEventContext.Provider value={eventContext}>{children}</FormEventContext.Provider>; | ||
}; | ||
|
||
export const useFormEvents = (): FormEventProps => { | ||
const eventContext = useContext(FormEventContext); | ||
if (eventContext === undefined) { | ||
throw new Error('useFormEvents must be used within the context of a FormEventProvider!'); | ||
} | ||
const [state, setState] = eventContext; | ||
|
||
const dispatch = (type: EventType) => { | ||
const newEvent: FormEvent = { id: state.events.length, type }; | ||
const newEvents = state.events.concat([newEvent]); | ||
setState(prev => ({ ...prev, events: newEvents })); | ||
}; | ||
|
||
return { | ||
events: state.events, | ||
dispatchEvent: dispatch, | ||
}; | ||
}; |
This file contains hidden or 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,107 @@ | ||
/** | ||
* Copyright (c) 2021-present, NDLA. | ||
* | ||
* This source code is licensed under the GPLv3 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import styled from '@emotion/styled'; | ||
import { ReactNode } from 'react'; | ||
import { | ||
ControllerFieldState, | ||
FieldValues, | ||
Noop, | ||
RefCallBack, | ||
useController, | ||
UseFormStateReturn, | ||
} from 'react-hook-form'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { Node } from 'slate'; | ||
import { classes } from './'; | ||
import FormFieldDescription from './FormFieldDescription'; | ||
import FormFieldHelp from './FormFieldHelp'; | ||
import FormFieldLabel from './FormFieldLabel'; | ||
import FormRemainingCharacters from './FormRemainingCharacters'; | ||
|
||
const StyledErrorPreLine = styled.span` | ||
white-space: pre-line; | ||
`; | ||
|
||
interface ControllerRenderProps< | ||
TFieldValues extends FieldValues, | ||
TName extends keyof TFieldValues & string | ||
> { | ||
onChange: (...event: any[]) => void; | ||
onBlur: Noop; | ||
value: TFieldValues[TName]; | ||
name: TName; | ||
ref: RefCallBack; | ||
} | ||
|
||
interface Props<TFieldValues extends FieldValues, TName extends keyof TFieldValues & string> { | ||
noBorder?: boolean; | ||
right?: boolean; | ||
title?: boolean; | ||
name: TName; | ||
label?: string; | ||
showError?: boolean; | ||
obligatory?: boolean; | ||
description?: string; | ||
maxLength?: number; | ||
showMaxLength?: boolean; | ||
className?: string; | ||
children: ( | ||
props: ControllerRenderProps<TFieldValues, TName> & | ||
ControllerFieldState & | ||
UseFormStateReturn<TFieldValues>, | ||
) => ReactNode; | ||
placeholder?: string; | ||
} | ||
|
||
const FormField = <TFieldValues extends FieldValues, TName extends keyof TFieldValues & string>({ | ||
children, | ||
className, | ||
label, | ||
name, | ||
maxLength, | ||
showMaxLength, | ||
noBorder = false, | ||
title = false, | ||
right = false, | ||
description, | ||
obligatory, | ||
showError = true, | ||
...rest | ||
}: Props<TFieldValues, TName>) => { | ||
const { t } = useTranslation(); | ||
|
||
const { field, fieldState, formState } = useController<any, any>({ | ||
name: name, | ||
}); | ||
const isSlateValue = Node.isNodeList(field.value); | ||
|
||
return ( | ||
<div {...classes('', { 'no-border': noBorder, right, title }, className)}> | ||
<FormFieldLabel label={label} name={name} noBorder={noBorder} /> | ||
<FormFieldDescription description={description} obligatory={obligatory} /> | ||
{children({ ...field, ...fieldState, ...formState })} | ||
{showMaxLength && maxLength && ( | ||
<FormRemainingCharacters | ||
maxLength={maxLength} | ||
getRemainingLabel={(maxLength, remaining) => | ||
t('form.remainingCharacters', { maxLength, remaining }) | ||
} | ||
value={isSlateValue ? Node.string(field.value[0]) : field.value} | ||
/> | ||
)} | ||
{showError && !!fieldState.error && ( | ||
<FormFieldHelp error={!!fieldState.error.message}> | ||
<StyledErrorPreLine>{fieldState.error.message}</StyledErrorPreLine> | ||
</FormFieldHelp> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default FormField; |
This file contains hidden or 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,49 @@ | ||
/* | ||
* Copyright (c) 2019-present, NDLA. | ||
* | ||
* This source code is licensed under the GPLv3 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import PropTypes from 'prop-types'; | ||
import styled from '@emotion/styled'; | ||
import { css } from '@emotion/core'; | ||
|
||
const StyledFormDescriptionBlock = styled.span` | ||
display: flex; | ||
`; | ||
|
||
const obligatoryDescriptionStyle = css` | ||
background-color: rgba(230, 132, 154, 1); | ||
padding: 0.2em 0.6em; | ||
`; | ||
|
||
const StyledFormDescription = styled.p` | ||
margin: 0.2em 0; | ||
font-size: 0.75em; | ||
${(p: Props) => (p.obligatory ? obligatoryDescriptionStyle : '')}; | ||
`; | ||
|
||
interface Props { | ||
description?: string; | ||
obligatory?: boolean; | ||
} | ||
|
||
const FormFieldDescription = ({ description, obligatory }: Props) => { | ||
if (!description) { | ||
return null; | ||
} | ||
return ( | ||
<StyledFormDescriptionBlock> | ||
<StyledFormDescription obligatory={obligatory}>{description}</StyledFormDescription> | ||
</StyledFormDescriptionBlock> | ||
); | ||
}; | ||
|
||
FormFieldDescription.propTypes = { | ||
obligatory: PropTypes.bool, | ||
description: PropTypes.string, | ||
}; | ||
|
||
export default FormFieldDescription; |
This file contains hidden or 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,38 @@ | ||
/* | ||
* Copyright (c) 2019-present, NDLA. | ||
* | ||
* This source code is licensed under the GPLv3 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import { ReactNode } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import styled from '@emotion/styled'; | ||
import { colors, fonts } from '@ndla/core'; | ||
|
||
interface Props { | ||
error?: boolean; | ||
float?: 'left' | 'right' | 'none' | 'inherit'; | ||
children: ReactNode; | ||
} | ||
|
||
export const StyledHelpMessage = styled.span` | ||
display: block; | ||
font-size: ${fonts.sizes(14, 1.2)}; | ||
color: ${(p: Props) => (p.error ? colors.support.red : 'black')}; | ||
float: ${(p: Props) => p.float || 'none'}; | ||
`; | ||
|
||
const FormFieldHelp = ({ error, float, children }: Props) => ( | ||
<StyledHelpMessage error={error} float={float}> | ||
{children} | ||
</StyledHelpMessage> | ||
); | ||
|
||
FormFieldHelp.propTypes = { | ||
error: PropTypes.bool, | ||
float: PropTypes.oneOf(['left', 'right', 'none', 'inherit']), | ||
}; | ||
|
||
export default FormFieldHelp; |
This file contains hidden or 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,39 @@ | ||
/* | ||
* Copyright (c) 2019-present, NDLA. | ||
* | ||
* This source code is licensed under the GPLv3 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import PropTypes from 'prop-types'; | ||
|
||
interface Props { | ||
name: string; | ||
label?: string; | ||
noBorder?: boolean; | ||
} | ||
|
||
const FormFieldLabel = ({ label, noBorder, name }: Props) => { | ||
if (!label) { | ||
return null; | ||
} | ||
if (!noBorder) { | ||
return <label htmlFor={name}>{label}</label>; | ||
} | ||
return ( | ||
<> | ||
<label className="u-hidden" htmlFor={name}> | ||
{label} | ||
</label> | ||
</> | ||
); | ||
}; | ||
|
||
FormFieldLabel.propTypes = { | ||
noBorder: PropTypes.bool, | ||
name: PropTypes.string.isRequired, | ||
label: PropTypes.string, | ||
}; | ||
|
||
export default FormFieldLabel; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Foreslår å endre navn på typene for å unngå to exports med samme navn.