-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: swap select options with equal length
list rendering behavior when react keys are index
- Loading branch information
1 parent
4252ce6
commit 026a020
Showing
6 changed files
with
105 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ReactNode } from "react"; | ||
|
||
import { FieldErrors, FieldLabel, Select, SelectProps } from ".."; | ||
import type { SelectField as _SelectField } from "../../hooks"; | ||
|
||
export const SelectField = <Option, Field extends _SelectField>({ | ||
field, | ||
label, | ||
...props | ||
}: { | ||
label: ReactNode; | ||
} & SelectProps<Option, Field>) => ( | ||
<div style={{ margin: "20px 0" }}> | ||
<FieldLabel field={field} label={label} /> | ||
<Select field={field} {...props} /> | ||
<FieldErrors field={field} /> | ||
</div> | ||
); |
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,67 @@ | ||
import { useCallback, useState } from "react"; | ||
import { RenderProp } from "react-render-prop-type"; | ||
|
||
import { SelectField } from "./SelectField.mock"; | ||
import { stringField } from "../../fields"; | ||
import { countryOptions } from "../../scenarios/mocks"; | ||
import { formStory, meta } from "../../scenarios/StoryForm"; | ||
|
||
export default { | ||
...meta, | ||
title: "components/Select", | ||
}; | ||
|
||
const mutatedCountries = [ | ||
...countryOptions.slice(0, countryOptions.length - 1), | ||
{ name: "Austria", key: "AT", flag: "🇦🇹" }, | ||
]; | ||
|
||
export const TwoOptionsOneSelect = formStory({ | ||
parameters: { | ||
docs: { | ||
description: { | ||
story: | ||
"Illustrates behavior when the options change to equal-length array", | ||
}, | ||
}, | ||
}, | ||
args: { | ||
fields: { | ||
country: stringField().optional(), | ||
}, | ||
children: ({ fields }) => ( | ||
<SwapCountries> | ||
{({ countries }) => ( | ||
<SelectField | ||
field={fields.country} | ||
label="Country of Origin" | ||
options={countries} | ||
getValue={({ key }) => key} | ||
getLabel={({ name }) => name} | ||
/> | ||
)} | ||
</SwapCountries> | ||
), | ||
}, | ||
}); | ||
|
||
const SwapCountries = ({ | ||
children, | ||
}: RenderProp<{ countries: typeof countryOptions }>) => { | ||
const [countries, setCountries] = useState(countryOptions); | ||
|
||
const swapCountries = useCallback(() => { | ||
setCountries( | ||
countries === countryOptions ? mutatedCountries : countryOptions | ||
); | ||
}, [countries]); | ||
|
||
return ( | ||
<> | ||
{children({ countries })} | ||
<button type="button" onClick={swapCountries}> | ||
Swap countriess | ||
</button> | ||
</> | ||
); | ||
}; |
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,6 @@ | ||
export const countryOptions = [ | ||
{ name: "Slovak Republic", key: "SK", flag: "🇸🇰" }, | ||
{ name: "Czech Republic", key: "CZ", flag: "🇨🇿" }, | ||
{ name: "Poland", key: "PL", flag: "🇵🇱" }, | ||
{ name: "Hungary", key: "HU", flag: "🇭🇺" }, | ||
]; |
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