BearStudio / start-ui-web

🚀 Start UI [web] is an opinionated UI starter with 🟦 TypeScript, ⚛️ React, ⚫️ NextJS, ⚡️ Chakra UI, 🟦 tRPC, ▲ Prisma, 🏖️ TanStack Query, 📕 Storybook, 🎭 Playwright,📋 React Hook Form,◽From the 🐻 BearStudio Team
https://demo.start-ui.com
MIT License
1.36k stars 127 forks source link

Code to implement : Editable component #31

Open FabienEssid opened 3 years ago

FabienEssid commented 3 years ago

I suggest you my whole component : Editable I think we will need to make some adjustments like using Typescript. Here is the entire code :

Editable.js ```jsx import React, { Fragment, useState } from 'react'; import { ButtonGroup, Flex, IconButton, Text, Textarea, } from '@chakra-ui/react'; import PropTypes from 'prop-types'; import { useTranslation } from 'react-i18next'; import { HiCheck, HiPencilAlt, HiX } from 'react-icons/hi'; import TextareaAutosize from 'react-textarea-autosize'; export const Editable = ({ value, onSubmit, onChange, onCancel, isSubmitDisabled, ...rest }) => { const { t } = useTranslation(); const [isEditing, setIsEditing] = useState(false); const [content, setContent] = useState(value); const handleEdit = () => { if (isEditing) { setIsEditing(false); onSubmit(content.trim()); return; } setIsEditing(true); setContent(value); }; const handleCancel = () => { setContent(value); setIsEditing((x) => !x); onCancel(value); }; const handleChange = (e) => { setContent(e.target?.value); onChange(e); }; return ( {isEditing ? (