Skip to content

Commit

Permalink
react-easy-edit 2.0 (#202)
Browse files Browse the repository at this point in the history
version 2.0
  • Loading branch information
giorgosart authored Oct 14, 2024
1 parent af1ac5d commit b044d07
Show file tree
Hide file tree
Showing 15 changed files with 1,543 additions and 1,441 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function App() {
onCancel={cancel}
saveButtonLabel="Save Me"
cancelButtonLabel="Cancel Me"
attributes={{ name: "awesome-input", id: 1}}
inputAttributes={{ name: "awesome-input", id: 1}}
instructions="Star this repo!"
/>
);
Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-easy-edit",
"version": "1.17.2",
"version": "2.0.0",
"description": "A react library for inline editing components",
"keywords": [
"content editable",
Expand Down Expand Up @@ -56,17 +56,18 @@
"@babel/preset-env": "^7.22.20",
"@babel/preset-react": "^7.22.15",
"@rollup/plugin-babel": "^5.3.1",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^14.0.0",
"@testing-library/dom": "^10.4.0",
"@testing-library/jest-dom": "^6.5.0",
"@testing-library/react": "^16.0.1",
"@zarconontol/enzyme-adapter-react-18": "^0.7.3",
"enzyme": "3.11.0",
"postcss-flexbugs-fixes": "^5.0.2",
"postcss-preset-env": "^8.0.1",
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-scripts": "^5.0.1",
"rollup": "2.53.3",
"rollup": "^2.79.2",
"rollup-plugin-postcss": "^4.0.2",
"rollup-plugin-terser": "^7.0.2"
}
Expand Down
18 changes: 9 additions & 9 deletions src/demo/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class App extends Component {
onSave={() => {
console.log("saved!")
}}
editMode={this.state.editMode}
isEditing={this.state.editMode}
hideCancelButton
hideSaveButton
/>
Expand All @@ -88,7 +88,7 @@ class App extends Component {
onSave={() => {
console.log("saved!")
}}
editMode={this.state.editMode}
isEditing={this.state.editMode}
instructions={"Toggle me!"}
hideCancelButton
hideSaveButton
Expand All @@ -108,7 +108,7 @@ class App extends Component {
type={Types.FILE}
onSave={App.onTest}
options={App.generateOptions()}
attributes={{accept: "image/png"}}
inputAttributes={{accept: "image/png"}}
instructions="Upload a *.png file"
onValidate={value => {return value != null}}
buttonsPosition='after'
Expand All @@ -121,7 +121,7 @@ class App extends Component {
type="text"
value="Test Input Field"
onSave={App.onTest}
allowEdit={false}
editable={false}
viewAttributes={{style:{'color':'red'}, 'data-test':'test', className: 'test2'}}
/>
<EasyEdit
Expand All @@ -131,7 +131,7 @@ class App extends Component {
onFocus={() => console.log("focus")}
/>
<EasyEdit
attributes={{id:'test'}}
inputAttributes={{id:'test'}}
type="text"
value="Delete Me!"
onSave={App.onTest}
Expand All @@ -141,7 +141,7 @@ class App extends Component {
type="text"
onSave={App.onTest}
onValidate={() => true}
attributes={attributes}
inputAttributes={attributes}
/>
<EasyEdit
type="text"
Expand All @@ -154,7 +154,7 @@ class App extends Component {
<EasyEdit
type={Types.FILE}
onSave={App.onTest}
attributes={{accept: "image/png"}}
inputAttributes={{accept: "image/png"}}
instructions="Upload a *.png file"
onValidate={value => {return value != null}}
buttonsPosition='after'
Expand Down Expand Up @@ -228,7 +228,7 @@ class App extends Component {
<EasyEdit
type={Types.TEXTAREA}
value="Test Textarea"
attributes={{className: 'test'}}
inputAttributes={{className: 'test'}}
onSave={App.onTest}
instructions="Custom instructions"
/>
Expand All @@ -255,7 +255,7 @@ class App extends Component {
options={App.generateOptions()}
onSave={App.onTest}
value={App.generateValues()}
attributes={{className: 'test', checked:'checked'}}
inputAttributes={{className: 'test', checked:'checked'}}
/>
<h3>Hide Buttons</h3>
<EasyEdit
Expand Down
61 changes: 41 additions & 20 deletions src/lib/EasyColor.test.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,51 @@
import React from 'react';
import {configure, shallow} from 'enzyme';
import Adapter from '@zarconontol/enzyme-adapter-react-18';
import React from "react";
import { render, fireEvent, screen } from "@testing-library/react";
import '@testing-library/jest-dom';
import EasyColor from "./EasyColor";

configure({adapter: new Adapter()});
describe("EasyColor component", () => {
it("should render the component with default value", () => {
render(<EasyColor value="#ff0000" cssClassPrefix="test-" />);
const inputElement = screen.getByDisplayValue("#ff0000");
expect(inputElement).toBeInTheDocument();
expect(inputElement).toHaveAttribute("type", "color");
});

it("should apply cssClassPrefix to wrapper div", () => {
const { container } = render(<EasyColor cssClassPrefix="test-" />);
expect(container.firstChild).toHaveClass("test-easy-edit-component-wrapper");
});

describe('EasyColor', () => {
let wrapper;
const onChange = jest.fn();
it("should call onChange when color value changes", () => {
const handleChange = jest.fn();
render(<EasyColor value="#ff0000" onChange={handleChange} />);
const inputElement = screen.getByDisplayValue("#ff0000");

beforeEach(() => {
wrapper = shallow(
<EasyColor
onChange={onChange}
value="#ff00ff"
attributes={{name: 'test'}}
/>
);
fireEvent.change(inputElement, { target: { value: "#00ff00" } });
expect(handleChange).toHaveBeenCalledTimes(1);
});

it('should set the name provided as a prop', () => {
expect(wrapper.find('input[name="test"]')).toHaveLength(1);
it("should call onFocus when input is focused", () => {
const handleFocus = jest.fn();
render(<EasyColor value="#ff0000" onFocus={handleFocus} />);
const inputElement = screen.getByDisplayValue("#ff0000");

fireEvent.focus(inputElement);
expect(handleFocus).toHaveBeenCalledTimes(1);
});

it("should call onBlur when input loses focus", () => {
const handleBlur = jest.fn();
render(<EasyColor value="#ff0000" onBlur={handleBlur} />);
const inputElement = screen.getByDisplayValue("#ff0000");

fireEvent.blur(inputElement);
expect(handleBlur).toHaveBeenCalledTimes(1);
});

it('should call onChange if the value of the input is changed', () => {
wrapper.find('input[name="test"]').simulate('change', {target: {value: '#000000'}});
expect(onChange).toBeCalled();
it("should pass additional attributes to input element", () => {
render(<EasyColor value="#ff0000" attributes={{ "data-testid": "color-input" }} />);
const inputElement = screen.getByTestId("color-input");
expect(inputElement).toBeInTheDocument();
});
});
86 changes: 40 additions & 46 deletions src/lib/EasyCustom.jsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,55 @@
import React, { Component } from 'react';
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';

export default class EasyCustom extends Component {
constructor(props) {
super(props);
this.state = {
value: props.value
};
this.setValue = this.setValue.bind(this);
this.onBlur = this.onBlur.bind(this);
this.onFocus = this.onFocus.bind(this);
}

setValue(value) {
this.setState({
const EasyCustom = ({ children, cssClassPrefix, onBlur, onFocus, onSetValue, value: initialValue }) => {
const [value, setValue] = useState(initialValue);

useEffect(() => {
setValue(initialValue);
}, [initialValue]);

const handleSetValue = (newValue) => {
setValue(newValue);
onSetValue(newValue);
};

const handleBlur = () => {
onBlur(value);
};

const handleFocus = () => {
onFocus();
};

const child = React.cloneElement(
React.Children.only(children),
{
setParentValue: handleSetValue,
onBlur: handleBlur,
onFocus: handleFocus,
value
}, () => this.props.setValue(value));
}

onBlur() {
this.props.onBlur();
}

onFocus() {
this.props.onFocus();
}

render() {
const { value } = this.state;
const { children, cssClassPrefix } = this.props;
const child = React.cloneElement(
React.Children.only(children),
{
setParentValue: this.setValue,
onBlur : this.onBlur,
onFocus : this.onFocus,
value
}
);
return (
<div className={cssClassPrefix + "easy-edit-component-wrapper"}>
{child}
</div>
);
}
}
}
);

return (
<div className={cssClassPrefix + "easy-edit-component-wrapper"}>
{child}
</div>
);
};

EasyCustom.propTypes = {
children: PropTypes.element,
cssClassPrefix: PropTypes.string,
onBlur: PropTypes.func,
onFocus: PropTypes.func,
setValue: PropTypes.func,
onSetValue: PropTypes.func, // Renamed from setValue to onSetValue in propTypes
value: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.array,
PropTypes.object
]),
}
};

export default EasyCustom;
Loading

0 comments on commit b044d07

Please sign in to comment.