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 checkbox #1154 #2090

Merged
merged 3 commits into from
Aug 1, 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/checkbox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ describe('Checkbox.tsx', () => {
expect(queryByTestId(name)).toBeInTheDocument()
})

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

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

it('Does not display checkbox when visible is false', () => {
const { queryByTestId } = render(<XCheckbox model={{ ...checkboxProps, visible: false }} />)
expect(queryByTestId(name)).toBeInTheDocument()
Expand All @@ -59,11 +66,19 @@ describe('Checkbox.tsx', () => {
expect(pushMock).toHaveBeenCalled()
})

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

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

it('Set args when value is updated to initial value', () => {
const { getByTestId, rerender } = render(<XCheckbox model={{ name, value: true }} />)
expect(wave.args[name]).toBe(true)
fireEvent.click(getByTestId(name))
expect(wave.args[name]).toBe(false)
rerender(<XCheckbox model={{ name, value: true }} />)
expect(wave.args[name]).toBe(true)
})
})
32 changes: 20 additions & 12 deletions ui/src/checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,32 @@ export interface Checkbox {

export const
XCheckbox = ({ model: m }: { model: Checkbox }) => {
const onChange = (_e?: React.FormEvent<HTMLElement>, checked?: B) => {
wave.args[m.name] = checked === null ? null : !!checked
if (m.trigger) wave.push()
}
const
{ name, label, value = false, indeterminate, disabled, trigger } = m,
[checked, setChecked] = React.useState(value),
onChange = (_e?: React.FormEvent<HTMLElement>, checked?: B) => {
wave.args[name] = checked === null ? null : !!checked
setChecked(!!checked)
m.value = !!checked
if (trigger) wave.push()
}

// 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.Checkbox
data-test={m.name}
inputProps={{ 'data-test': m.name } as React.ButtonHTMLAttributes<HTMLButtonElement>}
data-test={name}
inputProps={{ 'data-test': name } as React.ButtonHTMLAttributes<HTMLButtonElement>}
styles={{ checkmark: { display: 'flex' } }} // Fix: Center the checkmark in the checkbox.
label={m.label}
defaultIndeterminate={m.indeterminate}
defaultChecked={m.value}
label={label}
defaultIndeterminate={indeterminate}
checked={checked}
onChange={onChange}
disabled={m.disabled}
disabled={disabled}
/>
)
}