Skip to content
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

fix: Allow dynamic value change for toggle #1154 #2092

Merged
merged 2 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions ui/src/toggle.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ describe('Toggle.tsx', () => {
expect(queryByTestId(name)).toBeInTheDocument()
})

it('Sets args on click', () => {
const { getByTestId } = render(<XToggle model={toggleProps} />)
fireEvent.click(getByTestId(name))

expect(wave.args[name]).toBe(true)
})

it('Calls sync when trigger is on', () => {
const pushMock = jest.fn()
const { getByTestId } = render(<XToggle model={{ ...toggleProps, trigger: true }} />)
Expand All @@ -51,11 +58,19 @@ describe('Toggle.tsx', () => {
expect(pushMock).toHaveBeenCalledTimes(0)
})

it('Sets args on click', () => {
const { getByTestId } = render(<XToggle model={toggleProps} />)
fireEvent.click(getByTestId(name))

it('Set args when value is updated to different value', () => {
const { rerender } = render(<XToggle model={toggleProps} />)
expect(wave.args[name]).toBe(false)
rerender(<XToggle model={{ name, value: true }} />)
expect(wave.args[name]).toBe(true)
})

it('Set args when value is updated to initial value', () => {
const { getByTestId, rerender } = render(<XToggle model={toggleProps} />)
expect(wave.args[name]).toBe(false)
fireEvent.click(getByTestId(name))
expect(wave.args[name]).toBe(true)
rerender(<XToggle model={{ name, value: false }} />)
expect(wave.args[name]).toBe(false)
})
})
29 changes: 19 additions & 10 deletions ui/src/toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,34 @@ export interface Toggle {

export const
XToggle = ({ model: m }: { model: Toggle }) => {
const onChange = React.useCallback((_e?: React.FormEvent<HTMLElement>, checked?: B) => {
wave.args[m.name] = !!checked
if (m.trigger) wave.push()
}, [m.name, m.trigger])
const
{ name, value = false, label, disabled, trigger } = m,
[checked, setChecked] = React.useState(value),
onChange = React.useCallback((_e?: React.FormEvent<HTMLElement>, checked?: B) => {
wave.args[name] = !!checked
setChecked(!!checked)
m.value = !!checked
if (trigger) wave.push()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [name, trigger])

// eslint-disable-next-line react-hooks/exhaustive-deps
React.useEffect(() => { wave.args[m.name] = !!m.value }, [])
React.useEffect(() => {
wave.args[name] = value
setChecked(value)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value])

return (
<Fluent.Toggle
data-test={m.name}
data-test={name}
styles={{
root: { marginBottom: 0 },
text: { width: 21 } // Prevent jumping when the label changes from 'On' to 'Off'.
}}
label={m.label}
defaultChecked={m.value}
label={label}
checked={checked}
onChange={onChange}
disabled={m.disabled}
disabled={disabled}
onText="On"
offText="Off"
inlineLabel
Expand Down