-
Notifications
You must be signed in to change notification settings - Fork 78
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
LF-4388 animal details sub components handle edit and readonly modes #3485
Open
kathyavini
wants to merge
15
commits into
integration
Choose a base branch
from
LF-4388-animal-details-sub-components-handle-edit-and-readonly-modes
base: integration
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
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ba8d09f
LF-4388 Add disabled state to SexDetails component
kathyavini 1b62899
LF-4388 Create readonly General card
kathyavini 0c9a2e8
LF-4388 Add isDisabled prop to AnimalTypeSelect and AnimalBreedSelect
kathyavini 9a3023f
LF-4388 Use AnimalTypeSelect and AnimalBreedSelect in General card
kathyavini 886cbb4
LF-4388 Use type onChange handler instead of useEffect for clearing b…
kathyavini 7d55f57
LF-4388 Also use onChange handler in Basics Card, as it's a better so…
kathyavini 33acf4b
LF-4388 Fix non-clickable Clear text by increasing gap in Edit mode
kathyavini b8d8b94
LF-4388 Add string for LiteFarm ID
kathyavini de15282
LF-4388 Add Readonly and Edit modes to Origin
kathyavini 92d7da1
LF-4388 Add Reaonly and Edit modes to Unique
kathyavini 41b382d
LF-4388 Add disabled state to ImagePicker
kathyavini 5a9ba61
LF-4388 Add readonly and edit modes to Other card
kathyavini a6b7673
LF-4388 Separate out defaultValues for add and readonly/edit modes
kathyavini 40de62f
LF-4388 Add new disabled Figma styles for ImagePicker (image present)
kathyavini 06d2999
LF-4388 Add a story for the disabled SexDetails
kathyavini 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 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
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
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 |
---|---|---|
|
@@ -13,8 +13,10 @@ | |
* GNU General Public License for more details, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { useMemo } from 'react'; | ||
import { Controller, useFormContext } from 'react-hook-form'; | ||
import { useMemo, useRef } from 'react'; | ||
import { Controller, get, useFormContext } from 'react-hook-form'; | ||
import { SelectInstance } from 'react-select'; | ||
import clsx from 'clsx'; | ||
import Input, { getInputErrors } from '../../Form/Input'; | ||
import RadioGroup from '../../Form/RadioGroup'; | ||
import ReactSelect from '../../Form/ReactSelect'; | ||
|
@@ -33,12 +35,21 @@ import { | |
hookFormMinValidation, | ||
hookFormMaxCharsValidation, | ||
} from '../../Form/hookformValidationUtils'; | ||
import LockedInput from '../../Form/LockedInput'; | ||
import { | ||
AnimalTypeSelect, | ||
Option as AnimalSelectOption, | ||
AnimalBreedSelect, | ||
} from '../AddAnimalsFormCard/AnimalSelect'; | ||
|
||
export type GeneralDetailsProps = CommonDetailsProps & { | ||
sexOptions: Option[DetailsFields.SEX][]; | ||
useOptions: Option[DetailsFields.USE][]; | ||
animalOrBatch: AnimalOrBatchKeys; | ||
sexDetailsOptions?: SexDetailsType; | ||
typeOptions?: AnimalSelectOption[]; | ||
breedOptions?: AnimalSelectOption[]; | ||
onTypeChange?: (Option: AnimalSelectOption | null) => void; | ||
}; | ||
|
||
const GeneralDetails = ({ | ||
|
@@ -48,6 +59,10 @@ const GeneralDetails = ({ | |
animalOrBatch, | ||
sexDetailsOptions, | ||
namePrefix = '', | ||
mode = 'add', | ||
typeOptions = [], | ||
onTypeChange, | ||
breedOptions = [], | ||
}: GeneralDetailsProps) => { | ||
const { | ||
control, | ||
|
@@ -61,6 +76,11 @@ const GeneralDetails = ({ | |
const watchBatchCount = watch(`${namePrefix}${DetailsFields.COUNT}`) || 0; | ||
const watchedUse = watch(`${namePrefix}${DetailsFields.USE}`) as Option[DetailsFields.USE][]; | ||
|
||
const watchAnimalType = watch(`${namePrefix}${DetailsFields.TYPE}`); | ||
const filteredBreeds = breedOptions.filter(({ type }) => type === watchAnimalType?.value); | ||
|
||
const breedSelectRef = useRef<SelectInstance>(null); | ||
|
||
const isOtherUseSelected = !watchedUse ? false : watchedUse.some((use) => use.key === 'OTHER'); | ||
|
||
const sexInputs = useMemo(() => { | ||
|
@@ -75,6 +95,7 @@ const GeneralDetails = ({ | |
radios={sexOptions} | ||
hookFormControl={control} | ||
row | ||
disabled={mode === 'readonly'} | ||
/> | ||
</div> | ||
</> | ||
|
@@ -99,6 +120,7 @@ const GeneralDetails = ({ | |
min: hookFormMinValidation(1), | ||
}} | ||
onChange={() => trigger(`${namePrefix}${DetailsFields.COUNT}`)} | ||
disabled={mode === 'readonly'} | ||
/> | ||
<Controller | ||
name={`${namePrefix}${DetailsFields.SEX_DETAILS}`} | ||
|
@@ -116,6 +138,7 @@ const GeneralDetails = ({ | |
initialDetails={value || sexDetailsOptions} | ||
maxCount={watchBatchCount} | ||
onConfirm={(details) => onChange(details)} | ||
isDisabled={mode === 'readonly'} | ||
/> | ||
); | ||
}} | ||
|
@@ -125,7 +148,16 @@ const GeneralDetails = ({ | |
}, [animalOrBatch, t, sexOptions, control, watchBatchCount]); | ||
|
||
return ( | ||
<div className={styles.sectionWrapper}> | ||
<div className={clsx(styles.sectionWrapper, mode === 'edit' && styles.edit)}> | ||
{(mode === 'readonly' || mode === 'edit') && ( | ||
<> | ||
{/* @ts-ignore */} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not getting any errors when removing this, do we need it? |
||
<LockedInput | ||
label={t('ANIMAL.ATTRIBUTE.LITEFARM_ID')} | ||
placeholder={getValues(`${namePrefix}${DetailsFields.ID}`)} | ||
/> | ||
</> | ||
)} | ||
{animalOrBatch === AnimalOrBatchKeys.BATCH && ( | ||
<> | ||
{/* @ts-ignore */} | ||
|
@@ -139,33 +171,29 @@ const GeneralDetails = ({ | |
optional | ||
placeholder={t('ADD_ANIMAL.PLACEHOLDER.BATCH_NAME')} | ||
errors={getInputErrors(errors, `${namePrefix}${DetailsFields.BATCH_NAME}`)} | ||
disabled={mode === 'readonly'} | ||
/> | ||
</> | ||
)} | ||
<Controller | ||
control={control} | ||
<AnimalTypeSelect | ||
name={`${namePrefix}${DetailsFields.TYPE}`} | ||
render={({ field: { onChange, value } }) => ( | ||
<ReactSelect | ||
label={t('ANIMAL.ANIMAL_TYPE')} | ||
value={value} | ||
onChange={onChange} | ||
isDisabled | ||
/> | ||
)} | ||
/> | ||
<Controller | ||
control={control} | ||
typeOptions={typeOptions} | ||
onTypeChange={(option) => { | ||
trigger(`${namePrefix}${DetailsFields.TYPE}`); | ||
onTypeChange?.(option); | ||
breedSelectRef?.current?.clearValue(); | ||
}} | ||
error={get(errors, `${namePrefix}${DetailsFields.TYPE}`)} | ||
isDisabled={mode !== 'edit'} | ||
/> | ||
<AnimalBreedSelect | ||
breedSelectRef={breedSelectRef} | ||
name={`${namePrefix}${DetailsFields.BREED}`} | ||
render={({ field: { onChange, value } }) => ( | ||
<ReactSelect | ||
label={t('ANIMAL.ANIMAL_BREED')} | ||
optional | ||
value={value} | ||
onChange={onChange} | ||
isDisabled | ||
/> | ||
)} | ||
control={control} | ||
breedOptions={filteredBreeds} | ||
isTypeSelected={!!watchAnimalType} | ||
isDisabled={mode !== 'edit'} | ||
/> | ||
{sexInputs} | ||
<Controller | ||
|
@@ -180,6 +208,7 @@ const GeneralDetails = ({ | |
onChange={onChange} | ||
options={useOptions} | ||
style={{ paddingBottom: '12px' }} // accomodate "Clear all" button space | ||
isDisabled={mode === 'readonly'} | ||
/> | ||
)} | ||
/> | ||
|
@@ -195,6 +224,7 @@ const GeneralDetails = ({ | |
optional | ||
placeholder={t('ADD_ANIMAL.PLACEHOLDER.OTHER_USE')} | ||
errors={getInputErrors(errors, `${namePrefix}${DetailsFields.OTHER_USE}`)} | ||
disabled={mode === 'readonly'} | ||
/> | ||
</> | ||
)} | ||
|
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.
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.
We possibly don't need a ref here at all,
seems to do the trick.
Also, not related to this PR, but do we need a ref for the UUID value? Couldn't we set it in the
useEffect
hook without storing it in a ref?