From 7ca132eca84181db6eb65fe1da5ba51afb9685e9 Mon Sep 17 00:00:00 2001 From: SimaQ Date: Sun, 25 Oct 2015 11:31:48 +0800 Subject: [PATCH] feat: add mixin for form. --- components/form/ValueMixin.jsx | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 components/form/ValueMixin.jsx diff --git a/components/form/ValueMixin.jsx b/components/form/ValueMixin.jsx new file mode 100644 index 0000000000..bb815acf5d --- /dev/null +++ b/components/form/ValueMixin.jsx @@ -0,0 +1,32 @@ +function merge() { + const ret = {}; + const args = [].slice.call(arguments, 0); + args.forEach((a)=> { + Object.keys(a).forEach((k)=> { + ret[k] = a[k]; + }); + }); + return ret; +} + +const ValueMixin = { + setValue(field, e) { + let v = e; + const target = e && e.target; + if (target) { + if ((target.nodeName + '').toLowerCase() === 'input' && + target.type === 'checkbox') { + v = target.checked; + } else { + v = e.target.value; + } + } + const newFormData = {}; + newFormData[field] = v; + this.setState({ + formData: merge(this.state.formData, newFormData), + }); + }, +}; + +export default ValueMixin;