Skip to content

Commit

Permalink
feat(Input): add clear button event "on_clear"
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Nov 14, 2023
1 parent 62411f5 commit 05043d2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ showTabs: true
| `on_key_down` | _(optional)_ will be called on key down by the user. Returns `{ value, event }`. |
| `on_blur` | _(optional)_ will be called on blur set by the user. Returns `{ value, event }`. |
| `on_submit` | _(optional)_ will be called on submit button click. Returns `{ value, event }`. |
| `on_clear` | _(optional)_ will be called on a clear button click. Returns `{ value, previousValue, event }`. |

### Manipulate the input value during typing

Expand Down
1 change: 1 addition & 0 deletions packages/dnb-eufemia/src/components/input/Input.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export interface InputProps
on_submit_focus?: (...args: any[]) => any;
on_submit_blur?: (...args: any[]) => any;
on_state_update?: (...args: any[]) => any;
on_clear?: (...args: any[]) => any;
}
export default class Input extends React.Component<InputProps, any> {
static defaultProps: object;
Expand Down
8 changes: 8 additions & 0 deletions packages/dnb-eufemia/src/components/input/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export const inputPropTypes = {
on_submit_focus: PropTypes.func,
on_submit_blur: PropTypes.func,
on_state_update: PropTypes.func,
on_clear: PropTypes.func,
}

export default class Input extends React.PureComponent {
Expand Down Expand Up @@ -191,6 +192,7 @@ export default class Input extends React.PureComponent {
on_submit_focus: null,
on_submit_blur: null,
on_state_update: null,
on_clear: null,
}

static getDerivedStateFromProps(props, state) {
Expand Down Expand Up @@ -319,9 +321,15 @@ export default class Input extends React.PureComponent {
}
}
clearValue = (event) => {
const previousValue = this.state.value
const value = ''
this.setState({ value })
dispatchCustomElementEvent(this, 'on_change', { value, event })
dispatchCustomElementEvent(this, 'on_clear', {
value,
previousValue,
event,
})
this._ref.current.focus({ preventScroll: true })
}
render() {
Expand Down
21 changes: 21 additions & 0 deletions packages/dnb-eufemia/src/components/input/__tests__/Input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,28 @@ describe('Input with clear button', () => {
.querySelector('svg')
).toBeInTheDocument()
})

it('should emit on_clear event on clear button click', () => {
const on_clear = jest.fn()
const on_change = jest.fn()

render(
<Input value="123" clear on_clear={on_clear} on_change={on_change} />
)

fireEvent.click(document.querySelector('.dnb-input__clear-button'))

expect(on_clear).toHaveBeenCalledTimes(1)
expect(on_clear).toHaveBeenCalledWith(
expect.objectContaining({ value: '', previousValue: '123' })
)
expect(on_change).toHaveBeenCalledTimes(1)
expect(on_change).toHaveBeenCalledWith(
expect.objectContaining({ value: '' })
)
})
})

describe('Input ARIA', () => {
it('should validate with ARIA rules as a search input with a label', async () => {
const Comp = render(
Expand Down

0 comments on commit 05043d2

Please sign in to comment.