import * as React from 'react'; import { useState } from 'react'; import TextField, { HelperText, Input, Props } from '@material/react-text-field/dist/index'; import MaterialIcon from '@material/react-material-icon'; import { IconDialog } from './dialog'; import icons from '../icons'; export interface IIconPickerProps { textfield?: Partial> & Omit & { editIcon?: string, helperTextValue?: string }, onChange: (icon: string) => void; }; const IconPicker: React.SFC = (props: IIconPickerProps) => { const [state, setState] = useState({ isOpen: false, icon: '' }); const { isOpen, icon } = state; const { helperTextValue = undefined, label = null, outlined = false, editIcon = "edit", ...rest } = props.textfield || {}; const { onChange } = props; React.useEffect(() => { // only call onChange if value of inputField is a valid icon if (icons.indexOf(icon) !== -1) { onChange(icon); } }, [icon]); return (<> {helperTextValue} : undefined} leadingIcon={} onTrailingIconSelect={() => setState({ icon, isOpen: true })} trailingIcon={} outlined={true} label={(label || "Icon").toString()} {...rest} >) => { setState({ icon: e.currentTarget.value, isOpen: false }); }} /> { if (icon !== "close") { setState({ icon: icon.substr(10), isOpen: false }); } else { setState({ isOpen: false, icon: "" }) } }} selectedIcon={icon} /> ); }; IconPicker.defaultProps = {}; export default IconPicker;