Skip to content

Commit

Permalink
feat: Allow dynamic value change for checkbox #1154 (#2090)
Browse files Browse the repository at this point in the history
  • Loading branch information
marek-mihok authored Aug 1, 2023
1 parent 23cb43a commit 2e8b48b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
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}
/>
)
}

0 comments on commit 2e8b48b

Please sign in to comment.