1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-22 21:14:18 +00:00
pds-2021-g2-nest/nest_frontend/components/interactive/FormInlineText.js

67 lines
1.9 KiB
JavaScript
Raw Normal View History

import React, { useState } from "react"
import FormInline from "../base/FormInline"
import InputWithIcon from "../base/InputWithIcon"
import { faFont, faPlus } from "@fortawesome/free-solid-svg-icons"
import ButtonIconOnly from "../base/ButtonIconOnly"
import Style from "./FormInlineText.module.css"
2021-05-23 03:03:41 +00:00
/**
* A {@link FormInline} allowing the user to enter a string.
*
* @param textIcon - The icon to display in the text field.
* @param buttonIcon - The icon to display on the submit button.
* @param buttonColor - The color of the submit button.
* @param placeholder - The placeholder of the text field.
* @param validate - Function <string -> string> called to set the value of the text field.
* @param submit - Function <string> called when the submit button is pressed.
* @param props - Additional props to pass to the form.
* @returns {JSX.Element}
* @constructor
*/
export default function FormInlineText(
{
textIcon = faFont,
buttonIcon = faPlus,
buttonColor = "Green",
placeholder = "",
validate = val => val,
submit,
...props
},
) {
const [value, setValue] = useState("")
const _onSubmit = event => {
event.preventDefault()
2021-05-23 14:20:53 +00:00
if(!value) {
return
}
submit(value)
setValue("")
}
const _onChange = event => {
setValue(validate(event.target.value))
}
return (
<FormInline onSubmit={_onSubmit} {...props}>
<InputWithIcon
className={Style.Input}
icon={textIcon}
value={value}
onChange={_onChange}
placeholder={placeholder}
/>
<ButtonIconOnly
className={Style.Button}
icon={buttonIcon}
color={buttonColor}
onClick={_onSubmit}
disabled={!value}
/>
</FormInline>
)
}