-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add validation details and string-list control (#2825)
add validation details and string-list control --------- Co-authored-by: hoppe <hoppewang@microsoft.com>
- Loading branch information
Showing
14 changed files
with
311 additions
and
18 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
61 changes: 61 additions & 0 deletions
61
packages/bonito-ui/src/components/form/__tests__/string-list.spec.tsx
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,61 @@ | ||
import { StringListParameter } from "@azure/bonito-core/lib/form"; | ||
import { initMockBrowserEnvironment } from "../../../environment"; | ||
import { createParam } from "../../../form"; | ||
import { StringList } from "../string-list"; | ||
import { render, screen } from "@testing-library/react"; | ||
import React from "react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import { runAxe } from "../../../test-util"; | ||
|
||
describe("StringList form control", () => { | ||
beforeEach(() => initMockBrowserEnvironment()); | ||
|
||
test("renders a list of strings", async () => { | ||
const { container } = render( | ||
<> | ||
<StringList | ||
param={createStringListParam()} | ||
id="StringList" | ||
></StringList> | ||
</> | ||
); | ||
const inputs = screen.getAllByRole("textbox"); | ||
expect(inputs.length).toBe(3); | ||
expect((inputs[0] as HTMLInputElement).value).toBe("foo"); | ||
expect((inputs[1] as HTMLInputElement).value).toBe("bar"); | ||
expect((inputs[2] as HTMLInputElement).value).toBe(""); | ||
|
||
const deleteButtons = screen.getAllByRole("button"); | ||
expect(deleteButtons.length).toBe(2); | ||
expect(await runAxe(container)).toHaveNoViolations(); | ||
}); | ||
|
||
it("adds a new string when the last one is edited", async () => { | ||
const onChange = jest.fn(); | ||
render( | ||
<StringList param={createStringListParam()} onChange={onChange} /> | ||
); | ||
const inputs = screen.getAllByRole("textbox"); | ||
const input = inputs[inputs.length - 1]; | ||
input.focus(); | ||
await userEvent.type(input, "baz"); | ||
expect(onChange).toHaveBeenCalledWith(null, ["foo", "bar", "baz"]); | ||
}); | ||
|
||
it("deletes a string when the delete button is clicked", async () => { | ||
const onChange = jest.fn(); | ||
render( | ||
<StringList param={createStringListParam()} onChange={onChange} /> | ||
); | ||
const deleteButton = screen.getAllByRole("button")[1]; | ||
await userEvent.click(deleteButton); | ||
expect(onChange).toHaveBeenCalledWith(null, ["foo"]); | ||
}); | ||
}); | ||
|
||
function createStringListParam() { | ||
return createParam(StringListParameter, { | ||
label: "String List", | ||
value: ["foo", "bar"], | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
form: | ||
delete: Delete | ||
buttons: | ||
apply: Apply | ||
discardChanges: Discard changes | ||
|
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,175 @@ | ||
import { FormValues, ParameterName } from "@azure/bonito-core/lib/form"; | ||
import { IconButton } from "@fluentui/react/lib/Button"; | ||
import { Stack } from "@fluentui/react/lib/Stack"; | ||
import { TextField } from "@fluentui/react/lib/TextField"; | ||
import * as React from "react"; | ||
import { useCallback, useMemo } from "react"; | ||
import { useFormParameter, useUniqueId } from "../../hooks"; | ||
import { FormControlProps } from "./form-control"; | ||
import { useAppTheme } from "../../theme"; | ||
import { translate } from "@azure/bonito-core"; | ||
|
||
export interface StringListValidationDetails { | ||
[key: number]: string; | ||
} | ||
|
||
export function StringList<V extends FormValues, K extends ParameterName<V>>( | ||
props: FormControlProps<V, K> | ||
): JSX.Element { | ||
const { className, style, param, onChange } = props; | ||
|
||
const id = useUniqueId("form-control", props.id); | ||
const validationDetails = useFormParameter(param) | ||
.validationDetails as StringListValidationDetails; | ||
|
||
const items = useMemo<string[]>(() => { | ||
const items: string[] = []; | ||
if (param.value && Array.isArray(param.value)) { | ||
for (const item of param.value) { | ||
items.push(item); | ||
} | ||
} | ||
// Add an empty item at the end | ||
items.push(""); | ||
return items; | ||
}, [param.value]); | ||
|
||
const onItemChange = useCallback( | ||
(index: number, value: string) => { | ||
const newItems = [...items]; | ||
if (index === items.length - 1) { | ||
// Last item, add a new one | ||
newItems.push(""); | ||
} | ||
newItems[index] = value; | ||
param.value = newItems.slice(0, newItems.length - 1) as V[K]; | ||
onChange?.(null, param.value); | ||
}, | ||
[items, param, onChange] | ||
); | ||
|
||
const onItemDelete = useCallback( | ||
(index: number) => { | ||
const newItems = [...items]; | ||
newItems.splice(index, 1); | ||
param.value = newItems.slice(0, newItems.length - 1) as V[K]; | ||
onChange?.(null, param.value); | ||
}, | ||
[items, param, onChange] | ||
); | ||
|
||
return ( | ||
<Stack key={id} style={style} className={className}> | ||
{items.map((item, index) => { | ||
const errorMsg = validationDetails?.[index]; | ||
return ( | ||
<StringListItem | ||
key={index} | ||
index={index} | ||
value={item} | ||
label={param.label} | ||
errorMsg={errorMsg} | ||
placeholder={param.placeholder} | ||
onChange={onItemChange} | ||
onDelete={onItemDelete} | ||
disableDelete={index === items.length - 1} | ||
></StringListItem> | ||
); | ||
})} | ||
</Stack> | ||
); | ||
} | ||
|
||
interface StringListItemProps { | ||
index: number; | ||
value: string; | ||
label?: string; | ||
onChange: (index: number, value: string) => void; | ||
onDelete: (index: number) => void; | ||
placeholder?: string; | ||
disableDelete?: boolean; | ||
errorMsg?: string; | ||
} | ||
|
||
function StringListItem(props: StringListItemProps) { | ||
const { | ||
index, | ||
value, | ||
label, | ||
onChange, | ||
onDelete, | ||
disableDelete, | ||
errorMsg, | ||
placeholder, | ||
} = props; | ||
const styles = useStringListItemStyles(props); | ||
const ariaLabel = `${label || ""} ${index + 1}`; | ||
return ( | ||
<Stack | ||
key={index} | ||
horizontal | ||
verticalAlign="center" | ||
styles={styles.container} | ||
> | ||
<Stack.Item grow={1}> | ||
<TextField | ||
styles={styles.input} | ||
value={value} | ||
ariaLabel={ariaLabel} | ||
placeholder={placeholder} | ||
errorMessage={errorMsg} | ||
onChange={(_, newValue) => { | ||
onChange(index, newValue || ""); | ||
}} | ||
/> | ||
</Stack.Item> | ||
<IconButton | ||
styles={styles.button} | ||
iconProps={{ iconName: "Delete" }} | ||
ariaLabel={`${translate("bonito.ui.form.delete")} ${ariaLabel}`} | ||
onClick={() => { | ||
onDelete(index); | ||
}} | ||
disabled={disableDelete} | ||
/> | ||
</Stack> | ||
); | ||
} | ||
|
||
function useStringListItemStyles(props: StringListItemProps) { | ||
const theme = useAppTheme(); | ||
const { disableDelete } = props; | ||
|
||
return React.useMemo(() => { | ||
const itemPadding = { | ||
padding: "11px 8px 11px 12px", | ||
}; | ||
return { | ||
container: { | ||
root: { | ||
":hover": { | ||
backgroundColor: theme.palette.neutralLighter, | ||
}, | ||
}, | ||
}, | ||
input: { | ||
root: { | ||
...itemPadding, | ||
}, | ||
field: { | ||
height: "24px", | ||
}, | ||
fieldGroup: { | ||
height: "24px", | ||
"box-sizing": "content-box", | ||
}, | ||
}, | ||
button: { | ||
root: { | ||
...itemPadding, | ||
visibility: disableDelete ? "hidden" : "initial", | ||
}, | ||
}, | ||
}; | ||
}, [theme.palette.neutralLighter, disableDelete]); | ||
} |
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.