8

I have both a text input and a dropdown that allows additions (both use the Form.xxx version). For both of these, I would like to add an x icon on the right, that when clicked, will either call a handler or will clear the input's value.

Is this possible in semantic-ui-react?

Thank you

mdebeus
  • 1,928
  • 3
  • 18
  • 27

3 Answers3

12

I did find a solution, which I will share, but this means I can no longer have my lock icon on the left hand side, because an input can only have one icon.

What I've done is to use an Icon element, and add an onClick handler to that, as follows:

<Input ...
  icon={<Icon name='delete' link onClick={this.handleDeleteClick}/>}/>
jarmod
  • 71,565
  • 16
  • 115
  • 122
mdebeus
  • 1,928
  • 3
  • 18
  • 27
2

(Updated) To clear the field, there is no "semantic-ui-react" shortcut as far as I know.

However, you can do this manually using your component state.

Here would be an example of this:

class ExampleClearField extends Component {
  state = {}

  handleChange = (e, { name, value }) => this.setState({ [name]: value })

  handleClear = () => this.setState({ email: ''})

  render() {
    const { email } = this.state

    return (
     <Form.Input iconPosition='left' name="email />
        <Icon name='x' link onClick={this.handleClear} />
        <input/>
     </Form.Input>

    )
  }
 }

** Notice the link, which is needed for Icon to accept onClick.

Also, dont't forget about (you might need to change it's place depending on iconPostion)

morinx
  • 635
  • 7
  • 19
  • morinx - thank you for your input, but I was hoping to have the delete button embedded in the input field, instead of to the right of it. Semantic-ui-react does not do a good job with these action buttons - when the focus is set, or the input is set to error, a bounding box is displayed around the input (blue or red or whatever) but the button remains gray, which just doesn't look good. Any other thoughts? – mdebeus Mar 05 '18 at 21:29
1

As of Semantic UI React 0.83.0, it is possible to do this with Dropdowns using clearable. You cannot add your own event handler to the "x" by using this. Clicking the "x" will simply clear the selected value and call onChange with the new empty value.

Example from their docs:

const DropdownExampleClearable = () => <Dropdown clearable options={options} selection />

See the example output on their docs page here

Burak
  • 667
  • 1
  • 11
  • 19