From 220697c455229125e9c22c3e0a20f2f535230b5f Mon Sep 17 00:00:00 2001 From: Justin Kahn Date: Thu, 22 Aug 2019 11:41:42 +0800 Subject: [PATCH] fix: static method inheritance --- src/index.js | 15 +++++---------- test/index.spec.js | 24 ++++++++++++++++++++---- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/index.js b/src/index.js index eb1d4e3..e06308e 100644 --- a/src/index.js +++ b/src/index.js @@ -19,21 +19,16 @@ const initMeta = { class Field { static create(com, options = {}) { - return new Field(com, options); + return new this(com, options); } static getUseField({ useState, useMemo }) { - return function(options = {}) { + return (options = {}) => { const [, setState] = useState(); - const field = useMemo(() => { - return new Field( - { - setState, - }, - options - ); - }, [setState]); + const field = useMemo(() => this.create({ setState }, options), [ + setState, + ]); return field; }; diff --git a/test/index.spec.js b/test/index.spec.js index 29a653d..5c2e665 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -14,12 +14,28 @@ const FormItem = Form.Item; /* eslint-disable react/jsx-filename-extension */ /*global describe it afterEach */ describe('field', () => { - it('should create new field with `Field.create`', function() { - const field = Field.create(this); + describe('Field.create', function() { + it('should create new field', function() { + const field = Field.create(this); + + assert(!!field); + assert(Field.prototype.isPrototypeOf(field)); + }) + + it('should have subclass prototype', function() { + class myField extends Field { + constructor(com, options = {}) { + super(com, options); + } + } + const field = myField.create(this); + + assert(!!field); + assert(myField.prototype.isPrototypeOf(field)); + }) - assert(!!field); - assert(Field.prototype.isPrototypeOf(field)); }) + describe('render', () => { it('should support Form', function(done) { class Demo extends React.Component {